Android将图像保存到图库中不是面向右侧的

时间:2014-01-14 11:09:28

标签: android camera orientation photo exif

Android将图像保存到图库中并未正确定位 int getOrientationFromExif()之后获得的值始终相同: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;
}       

2 个答案:

答案 0 :(得分:0)

ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
    int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
            ExifInterface.ORIENTATION_NORMAL);

这里的问题始终是1,这意味着不需要旋转:(

答案 1 :(得分:0)

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

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

不确定此行为是否依赖于设备,这是运行4.1.2的Galaxy S2上的行为。