Need to scale image for canvas in Android

Problems with Canvas, OpenGL, etc...

Need to scale image for canvas in Android

Postby CatalystDroid » Tue Sep 24, 2013 3:50 pm

I was able to put the following code together:

Test.java

Code: Select all
public class Test extends Activity implements OnClickListener, OnTouchListener {

   ImageView choosenImageView;
   Button choosePicture;
   Button savePicture;

    Bitmap bmp;
    Bitmap alteredBitmap;
    Canvas canvas;
    Paint paint;
    Matrix matrix;
    float downx = 0;
     float downy = 0;
     float upx = 0;
      float upy = 0;

     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

       choosenImageView = (ImageView) this.findViewById(R.id.ChoosenImageView);
       choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
       savePicture = (Button) this.findViewById(R.id.SavePictureButton);

       savePicture.setOnClickListener(this);
       choosePicture.setOnClickListener(this);
        choosenImageView.setOnTouchListener(this);
       }

    public void onClick(View v) {

     if (v == choosePicture) {
     Intent choosePictureIntent = new Intent(
      Intent.ACTION_PICK,
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      startActivityForResult(choosePictureIntent, 0);
      } else if (v == savePicture) {

      if (alteredBitmap != null) {
        ContentValues contentValues = new ContentValues(3);
        contentValues.put(Media.DISPLAY_NAME, "Draw On Me");

        Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contentValues);
        try {
         OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri);
         alteredBitmap.compress(CompressFormat.JPEG, 90, imageFileOS);
          Toast t = Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT);
          t.show();

         } catch (Exception e) {
         Log.v("EXCEPTION", e.getMessage());
                  }
            }
           }
        }

       protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
       super.onActivityResult(requestCode, resultCode, intent);

         if (resultCode == RESULT_OK) {
         Uri imageFileUri = intent.getData();
          try {
          BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
          bmpFactoryOptions.inJustDecodeBounds = true;
           bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
            imageFileUri), null, bmpFactoryOptions);

            bmpFactoryOptions.inJustDecodeBounds = false;
            bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
            imageFileUri), null, bmpFactoryOptions);

            alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
           .getHeight(), bmp.getConfig());
            canvas = new Canvas(alteredBitmap);
            paint = new Paint();
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(5);
            matrix = new Matrix();
            canvas.drawBitmap(bmp, matrix, paint);

            choosenImageView.setImageBitmap(alteredBitmap);
            choosenImageView.setOnTouchListener(this);

              } catch (Exception e) {
              Log.v("ERROR", e.toString());
               }
            }
         }

          public boolean onTouch(View v, MotionEvent event) {
          int action = event.getAction();
           switch (action) {
           case MotionEvent.ACTION_DOWN:
           downx = event.getX();
           downy = event.getY();
           break;
           case MotionEvent.ACTION_MOVE:
            upx = event.getX();
            upy = event.getY();
            canvas.drawLine(downx, downy, upx, upy, paint);
            choosenImageView.invalidate();
            downx = upx;
            downy = upy;
             break;
              case MotionEvent.ACTION_UP:
               upx = event.getX();
               upy = event.getY();
               canvas.drawLine(downx, downy, upx, upy, paint);
               choosenImageView.invalidate();
               break;
               case MotionEvent.ACTION_CANCEL:
                break;
                default:
                break;
                       }
                return true;
                }
             }


main.xml

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
      android:layout_width="fill_parent"
       android:layout_height="fill_parent"
      >
   <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ChoosenImageView">
  </ImageView>

  <Button
    android:id="@+id/ChoosePictureButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Choose Picture" />

  <Button
    android:id="@+id/SavePictureButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Save Picture" />

    </LinearLayout>


It has a "Choose Picture Button" and "Save Picture Button".

What is happening is that if I choose a tmp.png I get the following screen where I am able to sketch on it as you can see, but the buttons no longer show for saving or choosing a picture. Image

Now if I select an image like Image

I get the following screen where I am able to sketch on it and all the buttons are visible.
Image

Any help would be greatly appreciated. I think the issue must be with scaling the image. Please Help.
User avatar
CatalystDroid
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Jan 12, 2011 3:36 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests