简体   繁体   English

调整android大小后图像变得偏斜

[英]Image is getting skewed after resizing android

Hello i am resizing the image after capturing from native camera.. image resizing is working fine but i have only one problem and that is the image is getting skewed after resizing. 您好,我正在从本机相机捕获后调整图像大小。图像调整大小工作正常,但我只有一个问题,那就是图像在调整大小后变得偏斜。

here is the code that i am using to resize the image. 这是我用来调整图像大小的代码。

 File f = new File(Environment.getExternalStorageDirectory() + File.separator + "Bavel/" + imageName);
 FileOutputStream fos = null;
 try {
    fos = new FileOutputStream(f);
    if (orientation == ExifInterface.ORIENTATION_UNDEFINED) {
        saveByteArray(fos, getBytesFromBitmap(loadedImage));
    } else {
        saveByteArrayWithOrientation(fos, getBytesFromBitmap(loadedImage), orientation);
    }

} catch (FileNotFoundException e) {
    Log.e("Error", "File not found: " + e.getMessage());
} catch (IOException e) {
    Log.e("Error", "File write failure: " + e.getMessage());
}


private void saveByteArray(FileOutputStream fos, byte[] data) throws IOException {
        long time = System.currentTimeMillis();
        fos.write(data);
        Log.e("saveByteArray: %1dms", "" + (System.currentTimeMillis() - time));
    }

private void saveByteArrayWithOrientation(FileOutputStream fos, byte[] data, int orientation) {

        long totalTime = System.currentTimeMillis();
        long time = System.currentTimeMillis();

        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        Log.e("decodeByteArray: %1dms", "" + (System.currentTimeMillis() - time));

        time = System.currentTimeMillis();
        if (orientation != 0 && bitmap.getWidth() > bitmap.getHeight()) {
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);

            int newWidth;
            int newHeight;

            if (bitmap.getWidth() > bitmap.getHeight()) {
                newHeight = Helper.BITMAP_HEIGHT;
                newWidth = Helper.BITMAP_HEIGHT * bitmap.getWidth() / bitmap.getHeight();
            } else {
                newWidth = Helper.BITMAP_HEIGHT;
                newHeight = Helper.BITMAP_HEIGHT * bitmap.getHeight() / bitmap.getWidth();
            }

            bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }
        time = System.currentTimeMillis();
        bitmap.compress(Bitmap.CompressFormat.JPEG, Helper.COMPRESS_QUALITY, fos);
        Log.d("compress: %1dms", "" + (System.currentTimeMillis() - time));
        Log.d("bitmap height ", "" + bitmap.getHeight());
        Log.d("bitmap witdh ", "" + bitmap.getWidth());

        bitmap.recycle();

        Log.d("saveByte: %1dms", "" + (System.currentTimeMillis() - totalTime));
    }

public byte[] getBytesFromBitmap(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, Helper.COMPRESS_QUALITY, stream);
        return stream.toByteArray();
    }

Here is the output after resizing the image 这是调整图像大小后的输出

调整大小后的图像

But i want the out put like this 但是我想要这样的输出

在此处输入图片说明

Please help and thank for help in advance. 请帮助,并提前感谢您的帮助。

I have solved this by using below code. 我已经通过使用下面的代码解决了这个问题。 i am adding the answer so it might be helpful to someone else. 我正在添加答案,因此可能会对其他人有所帮助。

ExifInterface exif = new ExifInterface(imagePath);
final int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
File root = new File(Environment.getExternalStorageDirectory(), "/Bavel/");
if (!root.exists()) {
    root.mkdirs();
}
File f = new File(Environment.getExternalStorageDirectory()
        + File.separator + "Bavel/" + imageName);
resizeNewImage(loadedImage, f, orientation);



private void resizeNewImage(Bitmap bm, File file, int orientation) {
    try {
        if (bm.getWidth() > bm.getHeight()) {
            int newWidth;
            int newHeight;

            if (bm.getWidth() > bm.getHeight()) {
                newHeight = Helper.BITMAP_HEIGHT;
                newWidth = Helper.BITMAP_HEIGHT * bm.getWidth() / bm.getHeight();
            } else {
                newWidth = Helper.BITMAP_HEIGHT;
                newHeight = Helper.BITMAP_HEIGHT * bm.getHeight() / bm.getWidth();
            }

            Matrix matrix = new Matrix();
            switch (orientation) {
                case ExifInterface.ORIENTATION_NORMAL:
                    matrix.postRotate(90);
                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:
                    matrix.postRotate(90);
            }
            bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
            bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
//                file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.JPEG, Helper.COMPRESS_QUALITY, ostream);

            ostream.close();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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