【问题标题】:Android Saved Images into Gallery not Right OrientedAndroid将图像保存到图库中的方向不正确
【发布时间】:2014-01-14 11:09:27
【问题描述】:

Android 将图像保存到库中的方向不正确 intgetOrientationFromExif()之后得到的值总是一样的:1... 不知道怎么解决...... 请帮帮我!

你能帮我解决吗?问题在里面

private PictureCallback mPicture = new PictureCallback()
{
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @Override
    public void onPictureTaken(byte[] data, Camera camera){

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);    

        try
        {
            FileOutputStream fos = new FileOutputStream(pictureFile);

            fos.write(data);
            fos.close(); 

            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "MyCameraApp");

            Bitmap myBitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            myBitmap = rotateImage(getOrientationFromExif(pictureFile), myBitmap); 
            myBitmap.compress(Bitmap.CompressFormat.JPEG, 70 /*ignored for PNG*/, bos);
            byte[] bitmapdata = bos.toByteArray();

            fos = new FileOutputStream(pictureFile);
            fos.write(bitmapdata);
            fos.close(); 

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
            Uri.parse("file://"+ mediaStorageDir)));
        }

        catch(FileNotFoundException e)
        {
            Log.d(TAG, "File not found: "+e.getMessage());
        }

        catch(IOException e)
        {
            Log.d(TAG, "Error accessing file: "+e.getMessage());
        }
    }
};

public Bitmap rotateImage(int angle, Bitmap bitmapSrc) 
{
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(bitmapSrc, 0, 0, 
    bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}

public int getOrientationFromExif(File imagePath) 
{
    int orientation = -1;

    try 
    {   
        ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                ExifInterface.ORIENTATION_NORMAL);
        System.out.println("yuri"+exifOrientation);

        switch (exifOrientation) 
        {
            case ExifInterface.ORIENTATION_ROTATE_270:      
                orientation = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                orientation = 180;

                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                orientation = 90;

                break;

            case ExifInterface.ORIENTATION_NORMAL:
                orientation = 0;

                break;
            default:
                break;
        }
    } catch (IOException e) {
        Log.e(TAG, "Unable to get image exif orientation", e);
    }

    return orientation;
}

public static boolean isSdPresent() {   
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}


private static File getOutputMediaFile(int type){
    File mediaFile = null; 
    if(isSdPresent() == false)
    {
        Log.d(TAG, "There is no Sd card. Cannot use the camera");
    }

   else
    {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),".");

        if(!mediaStorageDir.exists())
        {
            if(!mediaStorageDir.mkdirs())
            {
                Log.d("WorldCupApp", "failed to create directory");
                return null;
            }
        }

         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());       
         if (type == MEDIA_TYPE_IMAGE)
         {        
             mediaFile = new File(mediaStorageDir.getPath() + "IMG_"+ timeStamp + ".JPEG");    
         } 
         else 
         {       
             return null;  
         }          
    }
    return mediaFile;
}       

【问题讨论】:

标签: android camera orientation photo exif


【解决方案1】:
ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
    int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
            ExifInterface.ORIENTATION_NORMAL);

这里的问题总是 1 表示不需要旋转的方向:(

【讨论】:

    【解决方案2】:

    当您将活动固定为横向模式以实现自定义相机时,EXIF 数据将始终为 TAG_ORIENTATION 的 ORIENTATION_NORMAL。这与 activity.getWindowManager().getDefaultDisplay().getRotation() 总是返回 ROTATION_90 的副作用相同。

    我通过使用 OrientationEventListener 检测设备旋转,然后将我的 onPictureTaken 回调中的 EXIF 方向标记重写为旋转的正确值来解决此问题。

    不确定此行为是否与设备相关,这是运行 4.1.2 的 Galaxy S2 上的行为。

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 2017-06-25
      • 2016-04-16
      相关资源
      最近更新 更多