Exif方向标签返回0

时间:2013-11-20 20:42:17

标签: android camera exif

我正在开发自定义相机应用程序,我面临以下问题。当我尝试使用ExifInterface检索方向时,它始终返回0(ORIENTATION_UNDEFINED)。这可以防止我将图像旋转到正确的状态,以便正确显示。

我使用示例代码设置相机旋转(包含此代码的方法在startPreview()之前调用):

android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(0, info);

        screenOrientation = getWindowManager().getDefaultDisplay().getOrientation();


        Log.d(TAG, "Screen orientation: " + screenOrientation);

        screenOrientation = (screenOrientation + 45) / 90 * 90;

        int rotation = 0;

        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            rotation = (info.orientation - screenOrientation + 360) % 360;
        } 
        else{  
            //back-facing camera
            rotation = (info.orientation + screenOrientation) % 360;
            Log.d("Test", "Calculated Rotation" + rotation);
        }
        params.setRotation(rotation);

        camera.setParameters(params);

我保存拍摄照片的方式:

@Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File photoFileDir = getFileDir();

        if (!photoFileDir.exists() && !photoFileDir.mkdirs()){
            Toast.makeText(context, "Cannot create directory for the taken picture", Toast.LENGTH_LONG).show();
            return;
        }

        String pictureName = "photo_" + getPictureDate() + ".jpg";
        String picturePath = photoFileDir.getPath() + File.separator + pictureName;

        File pictureFile = new File(picturePath);

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            try {
                fos.write(data);
                fos.close();

                sendPictureSavedBroadcast(context, picturePath);
                Toast.makeText(context, "Picture saved!", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(context, "Picture could not be saved", Toast.LENGTH_LONG).show();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    } 

我尝试检索图片方向的方式:

public static int getRotation(String absolutePath){
        try{

            ExifInterface exif = new ExifInterface(absolutePath);

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

            Log.d(TAG, "Toolkit, orientation: " + orientation);

            switch(orientation){

                case ExifInterface.ORIENTATION_NORMAL:
                    Log.d(TAG, "Image is properly rotated");
                    return 0;

                case ExifInterface.ORIENTATION_ROTATE_90:
                    return 90;

                case ExifInterface.ORIENTATION_ROTATE_180:
                    return 180;

                case ExifInterface.ORIENTATION_ROTATE_270:

                    return 270;

                case ExifInterface.ORIENTATION_UNDEFINED:
                    Log.d(TAG, "Error getting orientation");
                    return -1;

                default:
                    Log.d(TAG, "Error getting orientation");
                    return -1;
            }

        }
        catch(Exception e){
            e.printStackTrace();
        }

        return -1;

    }

我在stackoverflow上经历了所有类似的主题,但没有找到解决方案。我很感激任何建议!

0 个答案:

没有答案