位图不读取解码时的EXIF数据 [英] Bitmap Not Reading EXIF Data on Decode

查看:54
本文介绍了位图不读取解码时的EXIF数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题

我想以正确的方向加载一系列位图.

I have a series of Bitmaps that I would like to load up in the correct orientation.

保存图像时,我进入并使用 ExifInterface

When I save the image I go in and set the orientation attribute using the ExifInterface

            ExifInterface exif = new ExifInterface(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
            int rotation = CCDataUtils.exifToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL));
            Log.v("PhotoManager", "Rotation:"+rotation);
            if (rotation > 0) {
                exif.setAttribute(ExifInterface.TAG_ORIENTATION,String.valueOf(0));

这正常工作,如果我要将图像从设备上拉下来,它将处于正确的方向.但是,当我随后在行下方解码我的位图时,即使图像是纵向拍摄的,它也保持在相机默认的左水平方向上?

This works fine and if I was to pull this image off of my device it would be in the correct orientation. However, when I then decode my Bitmap later down the line it stays in the camera's default orientation of left-horizontal even if the image was taken in portrait?

我的问题

如何解码位图并考虑其EXIF信息?

How can I decode the bitmap and take into account its EXIF information?

我不想在每次解码后都旋转图像,因为我必须创建另一个位图,而这是我没有的内存.

I don't want to have to rotate the image after I decode it every time as I would have to create another Bitmap and that is memory I don't have.

谢谢.

推荐答案

对于那些在处理多个位图时也遇到此问题并且存在oom问题的人,这是我的解决方案.

For those that are also stuck on this and have oom issues when manipulating multiple bitmaps here is my solution.

请勿像我最初在问题中所想的那样更改exif数据-我们稍后需要此信息.

Do not change the exif data like I originally thought in the question - We need this later down the line.

在解码要查看的图像时,无需解码完整尺寸的图像,只需解码缩小到所需大小的图像即可.下面的代码示例包含将位图解码为设备屏幕大小的内容,然后还为您处理位图的旋转.

When it comes to decoding the image to view, instead of decoding the full size image just decode the image scaled down to what you need. The following code example contains both the decoding of the bitmap to the device screen size and then it also handles the rotation of the bitmap for you.

public static Bitmap decodeFileForDisplay(File f){

    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);
        DisplayMetrics metrics = MyApplication.getAppContext().getResources().getDisplayMetrics();

        //The new size we want to scale to  
        //final int REQUIRED_SIZE=180;

        int scaleW =  o.outWidth / metrics.widthPixels;
        int scaleH =  o.outHeight / metrics.heightPixels;
        int scale = Math.max(scaleW,scaleH);
        //Log.d("CCBitmapUtils", "Scale Factor:"+scale);
        //Find the correct scale value. It should be the power of 2.

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        Bitmap scaledPhoto = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        try {
            ExifInterface exif = new ExifInterface(f.getAbsolutePath());
            int rotation = CCDataUtils.exifToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL));
            if (rotation > 0)
                scaledPhoto = CCBitmapUtils.convertBitmapToCorrectOrientation(scaledPhoto, rotation);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return scaledPhoto;

    } catch (FileNotFoundException e) {}
    return null;
    }

public static Bitmap convertBitmapToCorrectOrientation(Bitmap photo,int rotation) {
    int width = photo.getWidth();
    int height = photo.getHeight();


    Matrix matrix = new Matrix();
    matrix.preRotate(rotation);

    return Bitmap.createBitmap(photo, 0, 0, width, height, matrix, false);

}

因此,调用 decodeFileForDisplay(File f); 后返回的图像位图的方向和大小正确,可以节省大量内存问题.

So the image Bitmap thats returned after calling decodeFileForDisplay(File f); is in the correct orientation and the correct size for you screen saving you tons of memory problems.

我希望它能对某人有所帮助

这篇关于位图不读取解码时的EXIF数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆