在横向模式下拍摄时将图像颠倒(Android)

时间:2014-11-18 10:46:55

标签: android image landscape

在横向模式下拍照时,我遇到了一个奇怪的问题。使用以下代码解码文件以使其处于纵向模式时,我在警告对话框中显示时将图片颠倒:

public static Bitmap decodeFile(String path) {

    int orientation;

       try {

           if(path==null){
               return null;
           }
           // decode image size
           BitmapFactory.Options o = new BitmapFactory.Options();
           o.inJustDecodeBounds = true;

           // Find the correct scale value. It should be the power of 2.
           final int REQUIRED_SIZE = 70;
           int width_tmp = o.outWidth, height_tmp = o.outHeight;
           int scale = 4;
           while (true) {
               if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                   break;
               width_tmp /= 2;
               height_tmp /= 2;
               scale++;
           }
           // decode with inSampleSize
           BitmapFactory.Options o2 = new BitmapFactory.Options();
           o2.inSampleSize=scale;
           Bitmap bm = BitmapFactory.decodeFile(path,o2);

           Bitmap bitmap = bm;

           ExifInterface exif = new ExifInterface(path);
           orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
           //Log.e("orientation",""+orientation);
           Matrix m=new Matrix();

           if((orientation==3)){
               m.postRotate(180);
               m.postScale((float)bm.getWidth(), (float)bm.getHeight());

               Log.e("in orientation",""+orientation);

               try{
                   bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
               }
               catch (Exception ex){
                   Log.e("Bitmap Size","NULO");
               }

               return  bitmap;
           }
           else if(orientation==6){
                m.postRotate(90);

                Log.e("in orientation",""+orientation);

                bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                Log.e("Bitmap Size",String.valueOf(bitmap.getRowBytes() * bitmap.getHeight()));
               return  bitmap;
           }

           else if(orientation==8){ 
                m.postRotate(270);

                Log.e("in orientation",""+orientation);

                bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
               return  bitmap;
           }
           return bitmap;
       }
       catch (Exception e) {
       }
       return null;
   }

发生此问题时的方向值是何时为3值。除了我必须放一个try catch子句,因为在这种情况下,图像没有显示在警告对话框中。

有人遇到过这个问题吗?

此致

1 个答案:

答案 0 :(得分:1)

我找到了一个尝试解决方法的解决方案。以下代码正确显示图像:

public static Bitmap decodeFile(String path) {

    int orientation;

       try {

           if(path==null){
               return null;
           }

           final BitmapFactory.Options options = new BitmapFactory.Options();
           options.inJustDecodeBounds = true;
           BitmapFactory.decodeFile(path, options);

           options.inSampleSize = calculateInSampleSize(options, 400, 350);

           // Decode bitmap with inSampleSize set
           options.inJustDecodeBounds = false;
           Bitmap bmp = BitmapFactory.decodeFile(path, options);
           Bitmap bitmap = null;

           ExifInterface exifReader = new ExifInterface(path);

           orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);

           if (orientation ==ExifInterface.ORIENTATION_NORMAL) {

           // Do nothing. The original image is fine.
           } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {

                  bitmap = RotateBitmap(bmp, 90);

           } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {

                  bitmap = RotateBitmap(bmp, 180);

           } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {

                  bitmap = RotateBitmap(bmp, 270);

           }

           return bitmap;
       }
       catch (Exception e) {
       }
       return null;
   }

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
         }
     }
     return inSampleSize;
    }

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}