【问题标题】:Decoded Images Some Times are Rotated by 90degree解码后的图像有时会旋转 90 度
【发布时间】:2014-09-21 17:51:09
【问题描述】:

嘿,我正在尝试使用此代码将大图像加载到我的应用程序的背景:

        final Drawable drawable =new BitmapDrawable(colorResource,decodeFile(new File(LMApplication.sharedpreferences.getString(path,""))));    

                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                            if(LMApplication.CurrentSDK < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                                view.setBackgroundDrawable(drawable);
                            }else{
                                view.setBackground(drawable);
                            }
                        }
                });


public Bitmap decodeFile(File input){
            Bitmap bmpCompressed = null;
            try {
                //Decode image size

                BitmapFactory.Options o11 = new BitmapFactory.Options();
                o11.inJustDecodeBounds = true;

                BitmapFactory.decodeStream(new FileInputStream(input),null,o11);

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

                //Find the correct scale value. It should be the power of 2.
                int scale=1;
                while(o11.outWidth/scale/2>=REQUIRED_SIZE && o11.outHeight/scale/2>=REQUIRED_SIZE)
                    scale*=2;

                //Decode with inSampleSize
                BitmapFactory.Options o22 = new BitmapFactory.Options();
                o22.inSampleSize=scale;
                BitmapFactory.decodeStream(new FileInputStream(input), null, o22);
                bmpCompressed = BitmapFactory.decodeFile(input.toString(), o22);

                //    FileOutputStream out = null;
                try {
             //      out = new FileOutputStream(file);
              //     bmpCompressed.compress(CompressFormat.JPEG, 100, out); 

                } catch (Exception e) {
                e.printStackTrace();
                } finally {
                   try{
                 //      out.close();
                   } catch(Throwable ignore) {}
                }

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

但有些图像在解码后向左旋转了 90 度,这里有什么问题吗?或者有什么办法可以防止这种情况发生吗?

更新:

像这样更改了代码,现在为每一件事做好了准备:

    public Bitmap decodeFile(File input){
            Bitmap bmpCompressed = null;
            try {
                //Decode image size

                BitmapFactory.Options o11 = new BitmapFactory.Options();
                o11.inJustDecodeBounds = true;

                BitmapFactory.decodeStream(new FileInputStream(input),null,o11);

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

               int width = getDimensions.WIDTH;

               int height = getDimensions.HEIGHT;
                //Find the correct scale value. It should be the power of 2.
                int scale=1;
                while(o11.outWidth/scale/2>=width && o11.outHeight/scale/2>=height)
                    scale*=2;

                //Decode with inSampleSize
                BitmapFactory.Options o22 = new BitmapFactory.Options();
                o22.inSampleSize=scale;
                BitmapFactory.decodeStream(new FileInputStream(input), null, o22);
                bmpCompressed = BitmapFactory.decodeFile(input.toString(), o22);


                ExifInterface exif = new ExifInterface(input.getAbsolutePath());

               int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

               if(orientation != ExifInterface.ORIENTATION_NORMAL){

                   int rotatesize;

                   switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotatesize = -90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotatesize = -180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotatesize = -270;
                    break;

                default:
                    break;
                }

               Matrix matrix = new Matrix();
               matrix.postRotate(90);
               bmpCompressed = Bitmap.createBitmap(bmpCompressed, 0, 0, bmpCompressed.getWidth(), bmpCompressed.getHeight(), matrix, true);

               }
                //    FileOutputStream out = null;
                try {
             //      out = new FileOutputStream(file);
              //     bmpCompressed.compress(CompressFormat.JPEG, 100, out); 

                } catch (Exception e) {
                e.printStackTrace();
                } finally {
                   try{
                 //      out.close();
                   } catch(Throwable ignore) {}
                }

               } catch (IOException e) {}
              return bmpCompressed;
              }

【问题讨论】:

    标签: android bitmap drawable


    【解决方案1】:

    使用 ExifInterface 像这样旋转

    picturePath = getIntent().getStringExtra("path");
    Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
    ExifInterface exif = new ExifInterface(picturePath);
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    
    Matrix matrix = new Matrix();
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        matrix.postRotate(90);
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        matrix.postRotate(180);
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        matrix.postRotate(270);
        break;
    default:
        break;
    }
    
    myImageView.setImageBitmap(bitmap);
    bitmap.recycle();
    

    注意:这里picturePath是从Gallery中选择的图像路径

    【讨论】:

    • 是的,但问题是 exifinterface 从转换中创建了 1 个位图 + 位图,所以无论如何都会抛出 OOM
    • 不,它不适用于ExifInterface。它用于旋转matrix。尝试在使用后回收所有位图,就像我在回答中向你展示的那样。
    • 如果发生OOM,请尝试inSample那些图像。
    • 我当然做了,但有些图片实在是太大了
    • 让你尝试缩放这些位图
    猜你喜欢
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多