Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android image orientation issue with custom camera activity

I wrote a custom camera activity to handle some issues I've been having with certain android devices when calling intent image capture. The user is able to either select save image or just use the data returned back from the OnPictureTakenCallback.

The problem I'm having is displaying the image correctly with respect to the orientation it was taken. I force the activity to be displayed in portrait by calling SetRequestedOrientation.

How would I know the correct Orientation the camera was in when the user took the picture? i.e. The user could take the picture at a rotation of 90 (portrait).

I've tried to get to use the getRotation() on the window manager's default display, but with setting the requested orientation to portrait that only returns Surface.ROTATION_0.

Update: To clarify my other issue, how could I determine the orientation from just the byte[] data in the picture callback if the user were to not save the image?

Update: After trying the answers below with this code all I'm getting is ExifInterface.ORIENTATION_NORMAL. I've also changed my code to just save the file returned from the camera as I'm not sure there is an easy way to determine the orientation with just having the byte[] data.

    private PictureCallback mPicture = new PictureCallback() 
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) 
        {
            File directory = new File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES),
                    "MyApp");
            if(!directory.exists())
            {
                if(!directory.mkdirs())
                {
                    Log.d("CAMERA", "Unable to create directory to save photos.");
                    return;
                }
            }
            File file = new File(directory.getPath() + file.separator + "IMG_" + SimpleDateFormat.getDateTimeInstance().toString() + ".jpg");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(data);
            fos.close();
            ExifInterface exif = new ExifInterface(file.getCanonicalPath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            int rotate = 0;

            switch (orientation) 
            {
                case ExifInterface.ORIENTATION_ROTATE_90:
                   rotate = 90;
                   break; 
                case ExifInterface.ORIENTATION_ROTATE_180:
                   rotate = 180;
                   break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                   rotate = 270;
                   break;
                case default:
                   break;
             }
        }
    };
like image 301
Bryan Avatar asked Jul 26 '12 17:07

Bryan


People also ask

How do I fix the camera orientation on my Android?

There is a property in class Camera. CameraInfo named as orientation . It returns the integer. You can get the current orientation and then changed accordingly.

Why do my pictures turn sideways when I upload them?

Photos taken with a smartphone or digital camera contain “Exif data,” all sorts of information about where the photo was taken, when it was taken, and even how the camera was oriented. When uploaded to File Manager, this data is preserved, and that can often cause the orientation of the picture to be rotated.

Why do my pictures upload sideways Android?

The reason your photo would appear this way is because the photo was taken that way (either with the phone sideways or upside down) and the image file itself is in this orientation. For example, if you hold your phone upright and take a photo, the photo is saved in portrait mode or "sideways".

Why does my camera rotate my pictures?

The feature that's causing your photos to rotate and remain oriented the same way at all times is called Auto-Rotate, and it's actually a very good feature because it ensures that you'll be able to read whatever text is on the screen regardless of how you choose to hold your phone.


2 Answers

public void surfaceCreated(SurfaceHolder holder) {
    try {
        mCamera.setPreviewDisplay(holder);
        Camera.Parameters parameters = mCamera.getParameters();
        if (this.getResources().getConfiguration().orientation !=
                Configuration.ORIENTATION_LANDSCAPE)
        {
            parameters.set("orientation", "portrait"); <----THis gets the job done!!!
            // For Android Version 2.2 and above
            mCamera.setDisplayOrientation(90);
            // For Android Version 2.0 and above
            parameters.setRotation(90);
        }


        // End Effects for Android Version 2.0 and higher
        mCamera.setParameters(parameters);
    }
    catch (IOException exception)
    {
        mCamera.release();
    }

}
like image 166
Nikola Sokolov Avatar answered Oct 12 '22 01:10

Nikola Sokolov


SO you are facing some issue with the orientation of the camera.

This link shows an example app of a simple camera capture activity : http://labs.makemachine.net/2010/03/simple-android-photo-capture/

Maybe you should try fixing the orientation by doing something like this :

          ExifInterface exif = new ExifInterface(_path);
          int exifOrientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION,
          ExifInterface.ORIENTATION_NORMAL);

          int rotate = 0;

          switch (exifOrientation) {
          case ExifInterface.ORIENTATION_ROTATE_90:
          rotate = 90;
          break; 

         case ExifInterface.ORIENTATION_ROTATE_180:
         rotate = 180;
         break;

         case ExifInterface.ORIENTATION_ROTATE_270:
         rotate = 270;
         break;
         }

           if (rotate != 0) {
          int w = bitmap.getWidth();
          int h = bitmap.getHeight();

// Setting pre rotate
          Matrix mtx = new Matrix();
          mtx.preRotate(rotate);

         // Rotating Bitmap & convert to ARGB_8888, required by tess
         bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
         bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
       }
like image 27
Android2390 Avatar answered Oct 11 '22 23:10

Android2390