0

here is my code for surfaceholder callback class

    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();
            }
        }

    }

and here is my code to store taken image on sdcard.

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();
            }
        }
        }; 

I am not getting why image is storing in different orientation then the taken image's orientation.

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • It's a bug with various phones, including some HTC phones and my Droid X2. It is a hardware issue so android can't be patched to correct it. You will just have to rotate the image in code. – Bill Gary Jan 20 '12 at 20:52

1 Answers1

0

Try using setRotation of the camera

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

since all you are doing in your code is rotating the display with setDisplayOrientation. Might depend on the version of your device too, see this thread:

How to set Android camera orientation properly?

Community
  • 1
  • 1