简体   繁体   中英

My bitmap gets rotated in Android after scaling it

This is my code. This question is somewhat related from this question: Trying to scale down a Bitmap in Android not working

I commented out the options.inSampleSize and I still get the rotation (counter-clockwise 90 degrees seemingly). This seems like a fairly simple scaling down of an image, from Google documentation, and I'm not sure how I'm getting a rotated image.

Bitmap myBitmap = null;
        @Override
        protected byte[] doInBackground(Object... params) {


            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            //myImageByteArray is 4016 wide
            myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);
            if (options.outHeight > options.outWidth) {
                //options.inSampleSize = calculateInSampleSize(options, 640, 960);
            } else {
                //options.inSampleSize = calculateInSampleSize(options, 960, 640);
            }

            options.inJustDecodeBounds = false;

            //myImageByteArray is 4016 wide
            myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);

            //This log statement outputs around 1000 now. 
            Log.d("bitmap", myBitmap.getWidth()+"");
            ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
            myBitmap.compress(CompressFormat.JPEG, 50, bAOS);

}

The byte[] is originally from Commonsware CWAC Camera library , and I've tried taking a look at: Android Reduce Size Of Camera Picture

UPDATE:

I have started pulling away more code to try to make it more apparent where this rotation could be coming from. I have it narrowed down to this. (This code still causes rotation)

Bitmap myBitmap = null;
        @Override
        protected byte[] doInBackground(Object... params) {
            final BitmapFactory.Options options = new BitmapFactory.Options();

            myBitmap = BitmapFactory.decodeByteArray(myImageByteArray, 0, myImageByteArray.length, options);      
            ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
            myBitmap.compress(CompressFormat.JPEG, 50, bAOS);

}

It's probable that the issue here is that the image is not being rotated per-se, but that the image is being displayed with a rotation and your transform is clearing (or at least not carrying forward) the metadata the OS would use to show it oriented correctly. There's not a lot you can do about that, because the OS doesn't put that data in the image itself

If that's not the case, then what you have is byte-order issue. One format is giving you data left-to-right and the other is top-to-bottom. You need to reorder the bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM