Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rotate a bitmap in Android?

I know there are threads on this question already but the solutions seem to use methods from the Matrix class that don't seem to work anymore. Even after imports the methods cannot be resolved. I'm basically trying to rotate a bitmap 90 degrees because it comes out sideways when I take a picture vertically. Here's my code for the activity:

@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);



        }
    }
like image 504
Thanh Le Tran Avatar asked May 01 '15 06:05

Thanh Le Tran


2 Answers

This is the correct way to rotate 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;
    }
like image 83
Yau Avatar answered Sep 28 '22 02:09

Yau


try this:

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 your bitmap or in angle what you want to show your bitmap like 90 180 etc.it will change bitmap screen using postRotate() method of class Matrix and again create bitmap and revert you

like image 40
Hitesh Singh Avatar answered Sep 28 '22 01:09

Hitesh Singh