Android相机图像保存后旋转

Android相机图像保存后旋转,android,android-camera,android-orientation,Android,Android Camera,Android Orientation,我正在开发一个应用程序,当我按下一个按钮时,启动一个活动(获取结果),它将拍摄并存储照片。之前的活动将显示照片。 我正在使用一个CameraPreview类和一个CameraActivity活动来实现它。 一切都必须在肖像模式下,一切都很完美:我开始新的活动与预览,采取肖像模式的照片,并返回到前一个活动与图片显示在一个框中 唯一的问题是图片显示(和存储)逆时针旋转90°。我怎样才能避免呢 以下是我的活动: public class CameraActivity extends Activity

我正在开发一个应用程序,当我按下一个按钮时,启动一个活动(获取结果),它将拍摄并存储照片。之前的活动将显示照片。 我正在使用一个
CameraPreview
类和一个
CameraActivity
活动来实现它。 一切都必须在肖像模式下,一切都很完美:我开始新的活动与预览,采取肖像模式的照片,并返回到前一个活动与图片显示在一个框中

唯一的问题是图片显示(和存储)逆时针旋转90°。我怎样才能避免呢

以下是我的活动:

public class CameraActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        mCamera.setDisplayOrientation(90);
        Parameters params = mCamera.getParameters();
        params.setRotation(90);
        params.setPictureSize(100, 200);

        mCamera.setParameters(params);

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }

    private PictureCallback mPicture = new PictureCallback() {

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

            File pictureFile = getOutputMediaFile();
            if (pictureFile == null){
                Log.d(TAG, "Error creating media file, check storage permissions");
                Intent intent = getIntent();
                setResult(RESULT_CANCELED, intent);
                finish();

                if (mCamera != null){
                    mCamera.release(); // release the camera for other applications
                    mCamera = null;
                }
                return;
            }

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

                Intent intent = getIntent();
                setResult(RESULT_OK, intent);
                finish();

                if (mCamera != null){
                    mCamera.release(); // release the camera for other applications
                    mCamera = null;
                }
                return;

            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
            Intent intent = getIntent();
            setResult(RESULT_CANCELED, intent);
            finish();

            if (mCamera != null){
                mCamera.release(); // release the camera for other applications
                mCamera = null;
            }
            return;
        }
    };

    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }

    public void scatta(View v) {

        mCamera.takePicture(null, null, mPicture);
    }

    private File getOutputMediaFile(){

        HERE I RETURN A VALID FILE.

        return file;
    }
}
以下是预览:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        mCamera.setDisplayOrientation(90);
        Parameters params = mCamera.getParameters();
        params.setRotation(90);

        mCamera.setParameters(params);

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

谢谢

是的,我找到了解决方案:

这是我的相机活动:

package it......utils;

import it.......R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

public class CameraActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cam);

        // Create an instance of Camera
        mCamera = getCameraInstance();
        Parameters params = mCamera.getParameters();
        params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
        params.setPictureFormat(ImageFormat.JPEG);

        Camera.Size size = getSmallestPictureSize(params);

        params.setPictureSize(size.width, size.height);
        mCamera.setParameters(params);

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }

    private Camera.Size getSmallestPictureSize(Camera.Parameters parameters) {
        List<Size> list = parameters.getSupportedPictureSizes();
        Collections.sort(list, new AreasComparator());

        return list.get(1); // I choose the second one becasue the first one is too small.
    }


    private PictureCallback mPicture = new PictureCallback() {

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

            File pictureFile = getOutputMediaFile();
            if (pictureFile == null){
                Intent intent = getIntent();
                setResult(RESULT_CANCELED, intent);
                finish();

                if (mCamera != null){
                    mCamera.release(); // release the camera for other applications
                    mCamera = null;
                }
                System.gc();
                return;
            }

            try {

                FileOutputStream fos = new FileOutputStream(pictureFile);

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

                System.gc();

                if (mCamera != null){
                    mCamera.release(); // release the camera for other applications
                    mCamera = null;
                }

                Intent intent = getIntent();
                setResult(RESULT_OK, intent);
                finish();

                System.gc();
                return;

            } catch (FileNotFoundException e) {
                //TODO
            } catch (IOException e) {
                //TODO
            }
            Intent intent = getIntent();
            setResult(RESULT_CANCELED, intent);
            finish();

            if (mCamera != null){
                mCamera.release(); // release the camera for other applications
                mCamera = null;
            }
            System.gc();
            return;
        }
    };

    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }

    public void scatta(View v) {

        mCamera.autoFocus(new AutoFocusCallback() {

            @Override
            public void onAutoFocus(boolean success, Camera camera) {

                if (success) {
                    mCamera.takePicture(null, null, mPicture);
                }
            }
        });

    }

    private File getOutputMediaFile(){
        File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        String filePath = downloads.getParent() + File.separator + "PicturesTaken";
        File path = new File(filePath);
        path.mkdirs();
        File file;

        file = new File(path, "pic.jpg");

        if (file.exists()) {
            file.delete();
          }

        return file;
    }

    @Override
    public void onBackPressed() {
        if (mCamera != null){
            mCamera.release(); // release the camera for other applications
            mCamera = null;
        }
        mPreview = null;
        System.gc();
        super.onBackPressed();
    }

    public class AreasComparator implements Comparator<Camera.Size> {
        @Override
        public int compare(Camera.Size s1, Camera.Size s2) {

            int resultArea=s1.width * s1.height;
            int newArea=s2.width * s2.height;

            if (newArea < resultArea) {
                return 1;
            }

            return -1;
        }
    }
}
最后,这是我在我的主要活动中所做的,即调用相机并显示拍摄的照片:

private void startCamera() {
    Intent intent = new Intent(Pag4Activity.this, CameraActivity.class);
    startActivityForResult(intent, CODE_CAM);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode==RESULT_OK) {
        if(requestCode == CODE_CAM){

            File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            String filePath = downloads.getParent() + File.separator + "PicturesTaken";
            File path = new File(filePath);
            path.mkdirs();

            File file = new File(path, "pic.jpg");
            ImageView iv = (ImageView)findViewById(R.id.imageTaken);
            Bitmap img = decodeScaledBitmapFromSdCard(file.getPath(), 170, 210);

            if(img!=null) {
                iv.setImageBitmap(img);
            }

        }
    }
}

public static Bitmap decodeScaledBitmapFromSdCard(String filePath,
        int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}

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) {

        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}
private void startCamera(){
意向意向=新意向(Pag4Activity.this,CameraActivity.class);
startActivityForResult(意图、代码);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(resultCode==RESULT\u OK){
if(requestCode==代码){
文件下载=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u下载);
字符串filePath=downloads.getParent()+File.separator+“PicturesTaken”;
文件路径=新文件(文件路径);
path.mkdirs();
File File=新文件(路径“pic.jpg”);
ImageView iv=(ImageView)FindView YID(R.id.ImageTaked);
位图img=decodeScaledBitmapFromSdCard(file.getPath(),170210);
如果(img!=null){
iv.设置图像位图(img);
}
}
}
}
公共静态位图decodeScaledBitmapFromSdCard(字符串文件路径,
输入要求宽度,输入要求高度){
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码文件(文件路径,选项);
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeFile(文件路径,选项);
}
公共静态int-calculateInSampleSize(BitmapFactory.Options选项、int-reqWidth、int-reqHeight){
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}

嘿@aveschini,您找到解决方案了吗?可能重复使用相机意图拍摄的图像为什么会在某些设备上旋转,这些设备上的答案是可复制的,请在发布答案时解释您的更改。什么改变了,它为什么起作用,等等。
package it........utils;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            //TODO
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        mCamera.setDisplayOrientation(90);
        Parameters params = mCamera.getParameters();
        params.setRotation(90);

        mCamera.setParameters(params);

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            //TODO
        }
    }
}
private void startCamera() {
    Intent intent = new Intent(Pag4Activity.this, CameraActivity.class);
    startActivityForResult(intent, CODE_CAM);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode==RESULT_OK) {
        if(requestCode == CODE_CAM){

            File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            String filePath = downloads.getParent() + File.separator + "PicturesTaken";
            File path = new File(filePath);
            path.mkdirs();

            File file = new File(path, "pic.jpg");
            ImageView iv = (ImageView)findViewById(R.id.imageTaken);
            Bitmap img = decodeScaledBitmapFromSdCard(file.getPath(), 170, 210);

            if(img!=null) {
                iv.setImageBitmap(img);
            }

        }
    }
}

public static Bitmap decodeScaledBitmapFromSdCard(String filePath,
        int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}

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) {

        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}