Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

android - camera intent auto rotate to 90 degree

In my code below, I am trying to take photo using native camera and upload to server but when I take it as portrait and view it in gallery as landscape which means, its rotated to 90 degree. Pls help :-

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, REQUEST_CAMERA);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {

            handleCameraPhoto();
}
private void handleCameraPhoto() {
    Intent mediaScanIntent = new Intent(
            "android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);

    mediaScanIntent.setData(contentUri);
    getActivity().sendBroadcast(mediaScanIntent);
}

How can I rotate the image before saving to SD card?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I also faced this kind of problem while showing the images in listview. But using the EXIF data I was able to get a work around to set the images in proper orientation.

This is were the bitmap object for display is prepared :

  Matrix matrix = new Matrix();
  matrix.postRotate(getImageOrientation(url));
  Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
  bitmap.getHeight(), matrix, true);

This is the method used, in the 2nd line of above code, to rotate the images orientation.

 public static int getImageOrientation(String imagePath){
     int rotate = 0;
     try {

         File imageFile = new File(imagePath);
         ExifInterface exif = new ExifInterface(
                 imageFile.getAbsolutePath());
         int orientation = exif.getAttributeInt(
                 ExifInterface.TAG_ORIENTATION,
                 ExifInterface.ORIENTATION_NORMAL);

         switch (orientation) {
         case ExifInterface.ORIENTATION_ROTATE_270:
             rotate = 270;
             break;
         case ExifInterface.ORIENTATION_ROTATE_180:
             rotate = 180;
             break;
         case ExifInterface.ORIENTATION_ROTATE_90:
             rotate = 90;
             break;
         }
     } catch (IOException e) {
         e.printStackTrace();
     }
    return rotate;
 }

This may not be the precise answer to your question, it worked for me and hope it will be useful for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...