Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How rotate picture from "onPictureTaken" without Out of memory Exception?

I read many posts there? But i don't find correctly answer.

I try do something this:

@Override
public void onPictureTaken(byte[] paramArrayOfByte, Camera paramCamera) {
    try {


        Bitmap bitmap = BitmapFactory.decodeByteArray(paramArrayOfByte, 0,
        paramArrayOfByte.length);

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        FileOutputStream os = new ileOutputStream(Singleton.mPushFilePath);

        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
            height, matrix, false);

        resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 95, os);
        os.close();
        ...

Is there a way to rotate picture, without using BitmapFactory? I want rotate picture without loss of quality!

like image 741
ArtKorchagin Avatar asked Oct 06 '22 23:10

ArtKorchagin


1 Answers

Perhaps you can take the picture already rotated as you desire using Camera.setDisplayOrientation? Check Android camera rotate. Further, investigate Camera.Parameters.setRotation(). One of these techniques should do the trick for you.

Otherwise your code looks fine except for using parameter 95 on Bitmap.compress, you need to use 100 for lossless compression.

To avoid out-of-memory exception, use Camera.Parameters.setPictureSize() to take a lower resolution picture (e.g. 3Mpx). i.e. do you really need an 8Mpx photo? Make sure to use Camera.Parameters.getSupportedPictureSizes() to determine the supported sizes on your device.

like image 115
CSmith Avatar answered Oct 13 '22 01:10

CSmith