单击图片完美,但当我从SD卡打开它时,它将以横向模式打开

时间:2012-01-20 18:48:56

标签: android surfaceview

这是我的表面所有者回调类

的代码
    class Preview implements SurfaceHolder.Callback {
        SurfaceHolder mHolder;


        Preview(Context context) {
            // InstaImageButtonll a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            try {
                mHolder = sview.getHolder();
                mHolder.addCallback(this);
                mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Preview  "+e.toString()+"\n"+e.getMessage(), Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }

        public void surfaceCreated(SurfaceHolder holder) {
            // The Surface has been created, acquire the camera and tell it where
            // to draw.
        try {
            mCamera = Camera.open();
            mCamera.setDisplayOrientation(90);
            mCamera.setPreviewDisplay(holder);
            Camera.Parameters params= mCamera.getParameters();
            params.set("jpeg-quality", 72);
            params.setPictureFormat(PixelFormat.JPEG);
            params.setPictureSize(480, 640);
            mCamera.setParameters(params);



        } catch (Exception e) {
            Toast.makeText(TestCameraOverlay.this,
                    " surfaceCreated" + e.getMessage(), Toast.LENGTH_LONG)
                    .show();
            e.printStackTrace();
        }
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            // Surface will be destroyed when we return, so stop the preview.
            // Because the CameraDevice object is not a shared resource, it's very
            // important to release it when the activity is paused.
            try {
                mCamera.stopPreview();
                mCamera.setPreviewCallback(null); 
                mCamera.release();
                mCamera = null;
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),"surfaceDestroyed  "+ e.toString()+"\n"+e.getMessage(), Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            // Now that the size is known, set up the camera parameters and begin
            // the preview.
            try {
                Camera.Parameters parameters = mCamera.getParameters();
                parameters.setPreviewSize(w, h);
                mCamera.setParameters(parameters);
                mCamera.startPreview();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),"surfaceChanged "+ e.toString()+"\n"+e.getMessage(), Toast.LENGTH_LONG).show();

                e.printStackTrace();
            }
        }

    }

这是我在SD卡上存储拍摄图像的代码。

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback(){

        public void onPictureTaken(byte[] imageData, Camera c) {
            try {

                File fileOnSD=Environment.getExternalStorageDirectory();
                File file=new File(fileOnSD.getAbsolutePath(), "testImage.jpeg");
                Toast.makeText(getApplicationContext(),file.getAbsolutePath()+"\n"+imageData, Toast.LENGTH_LONG).show();
                FileOutputStream fout=new FileOutputStream(file, false);
                fout.write(imageData);
                Thread.sleep(2000);
                mCamera.startPreview();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),"onPictureTaken " +e.toString(), Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
        }; 

我不知道为什么图像以不同的方向存储,然后是拍摄的图像的方向。

1 个答案:

答案 0 :(得分:0)

尝试使用相机的setRotation

http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setRotation%28int%29

因为您在代码中所做的只是使用setDisplayOrientation旋转显示。可能还取决于您的设备版本,请参阅此主题:

How to set Android camera orientation properly?