How to rotate a bitmap in Android?

I know there are topics on this already, but the solutions seem to use methods from the Matrix class that no longer work. Even after import, the methods cannot be resolved. I'm basically trying to rotate a bitmap 90 degrees because it comes out from the side when I take a picture vertically. Here is my activity code:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera); this.imageView = (ImageView)this.findViewById(R.id.imageView1); Button photoButton = (Button) this.findViewById(R.id.button1); photoButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { //Check that request code matches ours: if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE) { //Get our saved file into a bitmap object: File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700); Intent intent = new Intent(this, EditActivity.class); ByteArrayOutputStream bs = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 50, bs); intent.putExtra("byteArray", bs.toByteArray()); startActivity(intent); } } 
+8
source share
3 answers

try the following:

 public static Bitmap RotateBitmap(Bitmap source, float angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); } 

Here, please pass the bitmap or at an angle that you want to display the bitmap, for example, 90,180, etc., will change the bitmap using the postRotate () method of the Matrix class and create the bitmap again and return you.

+13
source

You can add a TextView to your layout and set Bitmap for it.

 ImageView yourView = (ImageView)findViewById(imageviewid); Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700); yourView.setImageBitmap(bitmap); 

Perhaps you can use RotateAnimation in the view (ImageView installed in Bitmap) that you want to rotate, and remember to set the animation to fillAfter=true and duration=0 .

 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="90" android:toDegrees="180" android:pivotX="50%" android:pivotY="50%" android:duration="0" android:startOffset="0" /> 

Now you only need to inflate the animation in the view

 Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation); yourView.startAnimation(rotation); 

Or you can just do it yourView.setRotation(angle) using API >= 11 .

0
source

This is the correct way to rotate a bitmap: D

 public Bitmap rotateBitmap(Bitmap original, float degrees) { Matrix matrix = new Matrix(); matrix.preRotate(degrees); Bitmap rotatedBitmap = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true); original.recycle(); return rotatedBitmap; } 
0
source

All Articles