DarkRockman
DarkRockman

Reputation: 3

Android bitmap drawed incorrectly either from canvas or from BitmapFactory.createBitmap(src,x,y,w,h)

On Android, i'm trying to crop an image bitmap taken from camera to a fixed width / width which is smaller than the original photo taken, but when I create the cropped image the result always gets rotated (even if no matrix is defined) see https://i.sstatic.net/9okuX.jpg as original and https://i.sstatic.net/9kues.jpg as the cropped Image, which is being rotated and shows up incorrectly.

Why does the createBitmap method rotates the bitmap to be drawn in the destination bitmap.

Relevant code as follows

try {       int dstWidth = params[0].intValue();
            int dstHeight = params[1].intValue();
            int targetSize = Math.min(dstWidth, dstHeight);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(mPhotoUri), null, options); 
//calculate min inSampleSize for avoiding memory problems
            options.inSampleSize = calculateImageInSampleSize(options, targetSize, targetSize);
            options.inJustDecodeBounds = false;
            Bitmap originalPhotoBitmap = BitmapFactory.decodeStream(getActivity().getContentResolver()
                    .openInputStream(mPhotoUri), null, options);
            mOriginalBitmap = Bitmap.createBitmap(originalPhotoBitmap, originalPhotoBitmap.getWidth() - targetSize,
                                                        originalPhotoBitmap.getHeight() - targetSize, targetSize, targetSize);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

//codefor inSampleSize calculation from http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap

private int calculateImageInSampleSize(BitmapFactory.Options originalSize, int targetWidth, int targetHeight) {
    final int width = originalSize.outWidth;
    final int height = originalSize.outHeight;
    int inSampleSize = 1;

    if (height > targetHeight || width > targetWidth) {
        final int halfHeight = height /2;
        final int halfWidth = width /2;

        while ((halfHeight /inSampleSize) > targetHeight &&
                (halfWidth / inSampleSize) > targetWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

Upvotes: 0

Views: 257

Answers (1)

Thirumoorthy
Thirumoorthy

Reputation: 589

every device followed different orientation in camera so you followed this example solve your problem

private Bitmap imageRotating(String filePath){
    try{
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

        return rotateBitmap(decodeSampledBitmapFromResource(filePath,150,224), orientation);
    }
    catch(Exception e){
        e.printStackTrace();
        return null;
    }
}

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    try{
        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
    catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

Upvotes: 1

Related Questions