Android ImageView to Bitmap after rotating

763 views Asked by At

I want to create a bitmap from an imageView, but I first need to rotate my imageView before converting the bitmap. I use the following code to convert the imageView to Bitmap.

 BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
 Bitmap photo = drawable.getBitmap();

The imageView implements a rotation gesture. I want to be able to rotate the imageView and then create a Bitmap using the rotated imageView. However, it seems my Bitmap is still getting the original imageView instead of the rotated position. The following is the complete code. Thank you.

 public class MainActivity extends AppCompatActivity implements RotationGestureDetector.OnRotationGestureListener{

private ImageView imgPhoto, imgBackground, imgCombine;
private RotationGestureDetector mRotationDetector;
private Button btnCombine;
private float angle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRotationDetector = new RotationGestureDetector(this);

    imgPhoto = (ImageView) findViewById(R.id.imgPhoto);
    imgBackground = (ImageView) findViewById(R.id.imgBackground);
    collageImage = (ImageView) findViewById(R.id.imgCombine);

    btnCombine = (Button)findViewById(R.id.btnCombineImage);
    btnCombine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BitmapDrawable drawable = (BitmapDrawable) imgPhoto.getDrawable();
            Bitmap photo = drawable.getBitmap();

        }
    });
}

@Override
public boolean onTouchEvent(MotionEvent event){
    mRotationDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

@Override
public void OnRotation(RotationGestureDetector rotationDetector) {
    angle = rotationDetector.getAngle();
    Log.d("RotationGestureDetector", "Rotation: " + Float.toString(angle));
    imgPhoto.setRotation(imgPhoto.getRotation() + (-angle));
}

 }
3

There are 3 answers

1
Aidan Laing On

Try using this code after the onClick. It should provide a rotated bitmap with the angle specified.

Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotatedPhoto = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
0
ThomasLothbrok On

To rotate ImageView:

Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);

Source: https://stackoverflow.com/a/10104318/7639113

Then you can create the bitmap by using DrawingCache,

imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();
0
Zain Ali On

The rotate effect is actually applied at the Canvas level. So, what you can do is get the current Canvas augmentations and then draw your Bitmap into a new one with the changes.

           Matrix transformMatrix =  imgPhoto.getImageMatrix()

            Bitmap original = photo;

            Bitmap adjusted = Bitmap.createBitmap(original.getWidth(),
                    original.getHeight(),
                    original.getConfig());
            Canvas canvas = new Canvas(adjusted);
            canvas.setMatrix(transformMatrix);
            canvas.drawBitmap(original, 0, 0, null);

            //at this point adjusted bitmap contains the rotated image