0

i have camera activity. and i capture picture but in camera preview not show picture portrait mode . how to possible in android . my code below .

public class CameraActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mCameraPreview;
    protected static final int MEDIA_TYPE_IMAGE = 0;
    static String FilePAth = "";
    Button takePicture;
    static String base64string="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_preview);

        mCamera = getCameraInstance();
        mCameraPreview = new CameraPreview(CameraActivity.this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);

        takePicture = (Button) findViewById(R.id.btnTakePicture);
        takePicture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                mCamera.takePicture(null, null, mPicture);

            }
        });
    }

    private Camera getCameraInstance() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }

    private static File getOutputMediaFile() {
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "MyCameraApp");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());

        FilePAth = mediaStorageDir.getPath() + File.separator + "IMG_"
                + timeStamp + ".jpg";

        Log.v("log", " FilePAth " + FilePAth);

        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");

        return mediaFile;
    }

    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {

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

                Intent returnIntent = new Intent();
                returnIntent.putExtra("data", data);
                setResult(RESULT_OK, returnIntent);
                finish();

            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }
        }
    };

    public void onBackPressed() {
        Intent returnIntent = new Intent();
        returnIntent.putExtra("path", FilePAth);
        setResult(RESULT_OK, returnIntent);
        finish();
    };
}

and get data :

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

        Log.v("log", " data --> " + data.getByteArrayExtra("data"));
        if (requestCode == 1) {

            if (data.hasExtra("data")) {

                Log.v("log", " request if ");

                Bitmap b = BitmapFactory.decodeByteArray(
                        data.getByteArrayExtra("data"), 0,
                        data.getByteArrayExtra("data").length);



                imgStorePicture.setImageBitmap(b);
                /*imgStorePicture.setScaleType(ScaleType.FIT_XY);*/

                base64string = Base64.encodeBytes(data
                        .getByteArrayExtra("data"));
                Log.v("log", "base64string " + base64string);





            }
        }
    }
crickpatel0024
  • 1,999
  • 6
  • 30
  • 47
  • possible duplicate of [Rotate byte array of JPEG after onPictureTaken](http://stackoverflow.com/questions/16651831/rotate-byte-array-of-jpeg-after-onpicturetaken) – Alex Cohn Mar 24 '14 at 11:35

1 Answers1

0

The short answer is, you cannot. The picture is always returned the way it is. You can change preview orientation, but not the orientation of the JPEG image returned to your onPictureTaken).

But you can set JPEG rotation via Exif header without decoding it. This is the most efficient method, but some viewers may still show a rotated image.

Alternatively, you can use JPEG lossless rotation. The Android port is on GitHub.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307