39

I have a camera app in portrait mode which takes pictures from both front and back end cameras.I am saving the image in my sd card and try to find the corresponding exif value which gives 0 always.But i am getting the the expected exif orientation value for the other images stored in the device(like downloaded pictures).

How can i fix this ? Can anyone help me out ?

Here is the code used to save the picture and the find the orientation

PictureCallback myPictureCallback_JPG = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub


                try {
                    File APP_FILE_PATH = new File(Environment.getExternalStorageDirectory()
                            .getPath() + "/Myapp/");
                    if (!APP_FILE_PATH.exists()) {
                        APP_FILE_PATH.mkdirs();
                    }
                    File file = new File(APP_FILE_PATH, "image.jpg");

                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(arg0);
                    fos.close();
imageFileUri=Uri.fromfile(file);                           getApplicationContext().getContentResolver().notifyChange(
                          imageFileUri, null);
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                            Uri.parse("file://"
                                    + Environment.getExternalStorageDirectory())));
                    ExifInterface exif = new ExifInterface(file.getAbsolutePath());
                    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);



                } catch (Exception e) {

                }



    }
};

Following is the code for surphace created and changed functions

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, width, height);
parameters.setPreviewSize(optimalSize.width, optimalSize.height);

camera.setParameters(parameters);
camera.startPreview();
 startPreview();

}
 @Override
 public void surfaceCreated(SurfaceHolder holder) {


 try {

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
          Camera.CameraInfo info=new Camera.CameraInfo();

          for (int i=0; i < Camera.getNumberOfCameras(); i++) {
            Camera.getCameraInfo(i, info);

            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
              camera=Camera.open(i);
              defaultCameraId = i;
            }
          }
        }

        if (camera == null) {
          camera=Camera.open();
        }




        try {
            camera.setPreviewDisplay(surfaceHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Camera.Parameters parameters = camera.getParameters();
        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(defaultCameraId, info);
        int rotation = this.getWindowManager().getDefaultDisplay()
                .getRotation();
        if (Integer.parseInt(Build.VERSION.SDK) >= 8)
        {

             int degrees = 0;
             switch (rotation) {
                 case Surface.ROTATION_0: 
                     degrees = 0; break;
                 case Surface.ROTATION_90: 
                     degrees = 90; break;
                 case Surface.ROTATION_180: 
                     degrees = 180; break;
                 case Surface.ROTATION_270: 
                     degrees = 270; break;
             }
             int result;
             if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                 result = (info.orientation + degrees) % 360;
                 result = (360 - result) % 360;  
             } else {  // back-facing
                 result = (info.orientation - degrees + 360) % 360;
             }
             camera.setDisplayOrientation(result);

        }
        else
        {
            parameters.set("orientation", "portrait");
        }


        camera.setParameters(parameters);



} catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 


}
 private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
            final double ASPECT_TOLERANCE = 0.1;
            double targetRatio = (double) w / h;
            if (sizes == null) return null;

            Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;

            int targetHeight = h;


            for (Size size : sizes) {
                double ratio = (double) size.width / size.height;
                if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }


            if (optimalSize == null) {
                minDiff = Double.MAX_VALUE;
                for (Size size : sizes) {
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                    }
                }
            }
            return optimalSize;
        }
hacker
  • 8,731
  • 12
  • 55
  • 106
  • What device are you using? I had this bug with my samsung galaxy S1. The only solution was to have a factory reset on my phone. – meh Nov 07 '12 at 09:03
  • samsung galaxy ace+..Also i tried with samsung galaxy pro, that also same.. – hacker Nov 07 '12 at 09:17
  • can you post some sample code of how you are taking the photo and then how you are saving it? – Maks Nov 08 '12 at 23:52
  • @Maks i have updated my question... – hacker Nov 09 '12 at 05:57
  • Actually this looks like its come up before http://stackoverflow.com/questions/8450539/images-taken-with-action-image-capture-always-returns-1-for-exifinterface-tag-or/8864367#8864367 – Maks Nov 09 '12 at 06:12
  • @Maks i tried that too..but never works – hacker Nov 09 '12 at 06:24
  • 1
    It cannot work, the solution in the link works for images taken by another app (camera app) not the ones taken by you with Camera API. – ALiGOTec Nov 10 '12 at 20:54
  • @hacker you want to maintain images in portrait only? if so, I achieved to save images in portrait mode,whether its taken in any mode. – RobinHood Nov 12 '12 at 07:33
  • @RobinHood yes, it's in portrait mode..can you pls help me ? – hacker Nov 12 '12 at 07:46
  • can you please post a logcat? – avimak Nov 14 '12 at 11:02
  • can any one help http://stackoverflow.com/questions/28379130/how-to-set-camera-image-orientation –  Feb 07 '15 at 13:32

4 Answers4

24

I also faced same issue in Samsung devices, later I implemented ExifInterface and solved successfully.

In any mode images will be shot it will always store in portrait mode only, and while fetching too returning in portrait mode. below of code I used to achieve my goal, I implemented within back camera, not sure about from camera.

Camera Intent@

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, 1212);   

onActivityResult@

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

        if (requestCode == 1212) {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
        Bitmap bitmap;
        //bitmap=GlobalMethods.decodeSampledBitmapFromResource(_path, 80, 80);
        bitmap=GlobalMethods.decodeFile(_path);
        if (bitmap == null) {
            imgMed.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.add_photo));
        } 
        else {
            imgMed.setImageBitmap(bitmap);
            imgMed.setScaleType(ScaleType.FIT_XY);

        }
    }
        }

decodeFile@

    public static Bitmap decodeFile(String path) {

          int orientation;

             try {

                 if(path==null){

                     return null;
                 }
                 // decode image size
                 BitmapFactory.Options o = new BitmapFactory.Options();
                 o.inJustDecodeBounds = true;

                 // Find the correct scale value. It should be the power of 2.
                 final int REQUIRED_SIZE = 70;
                 int width_tmp = o.outWidth, height_tmp = o.outHeight;
                 int scale = 4;
                 while (true) {
                     if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                         break;
                     width_tmp /= 2;
                     height_tmp /= 2;
                     scale++;
                 }
                 // decode with inSampleSize
                 BitmapFactory.Options o2 = new BitmapFactory.Options();
                 o2.inSampleSize=scale;
                 Bitmap bm = BitmapFactory.decodeFile(path,o2);


                 Bitmap bitmap = bm;

                 ExifInterface exif = new ExifInterface(path);
                 orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                 Log.e("orientation",""+orientation);
                 Matrix m=new Matrix();

                 if((orientation==3)){

                 m.postRotate(180);
                 m.postScale((float)bm.getWidth(), (float)bm.getHeight());

//               if(m.preRotate(90)){
                 Log.e("in orientation",""+orientation);

                 bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                 return  bitmap;
                 }
                 else if(orientation==6){

                  m.postRotate(90);

                  Log.e("in orientation",""+orientation);

                  bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                     return  bitmap;
                 }

                 else if(orientation==8){

                  m.postRotate(270);

                  Log.e("in orientation",""+orientation);

                  bitmap = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(),bm.getHeight(), m, true);
                     return  bitmap;
                 }
                 return bitmap;
             }
             catch (Exception e) {
             }
             return null;
         }
RobinHood
  • 10,464
  • 4
  • 44
  • 92
3

The problem is that you must put the exif information manually in your onPictureTaken function.

After you save the picture (jpg) you must create an Exif interface an put the parameters yourself:

....
exif = new ExifInterface(file.getAbsolutePath());
exif.setAttribute(ExifInterface.TAG_ORIENTATION, orientation_detected_by_you_application);
exif.saveAttributes();

The other pictures you have in your phone are made with the an application that puts the exif information in the pictures it takes.

To detect orientation:

public void enableOrientationListener(){

    if (mOrientationEventListener == null) {            
        mOrientationEventListener = new OrientationEventListener(getContext(), SensorManager.SENSOR_DELAY_NORMAL) {

            @Override
            public void onOrientationChanged(int orientation) {

                // determine our orientation based on sensor response
                int lastOrientation = mOrientation;

                Display display = null;
                if(parentActivity == null){
                    display = ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                }else{
                    display = parentActivity.getWindowManager().getDefaultDisplay();
                }


                if (display.getOrientation() == Surface.ROTATION_0) {   // landscape oriented devices
                    if (orientation >= 315 || orientation < 45) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {                         
                            mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                        }
                    } else if (orientation < 315 && orientation >= 225) {
                        if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                            mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                        }                       
                    } else if (orientation < 225 && orientation >= 135) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                            mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                        }                       
                    } else if (orientation <135 && orientation > 45) { 
                        if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                            mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                        }                       
                    }                       
                } else {  // portrait oriented devices
                    if (orientation >= 315 || orientation < 45) {
                        if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {                          
                            mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                        }
                    } else if (orientation < 315 && orientation >= 225) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                            mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                        }                       
                    } else if (orientation < 225 && orientation >= 135) {
                        if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                            mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                        }                       
                    } else if (orientation <135 && orientation > 45) { 
                        if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                            mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                        }                       
                    }
                }
            }
        };
    }
    if (mOrientationEventListener.canDetectOrientation()) {
        mOrientationEventListener.enable();
    }
}


private static final int ORIENTATION_PORTRAIT_NORMAL    =  1;
private static final int ORIENTATION_PORTRAIT_INVERTED  =  2;
private static final int ORIENTATION_LANDSCAPE_NORMAL   =  3;
private static final int ORIENTATION_LANDSCAPE_INVERTED =  4;


public void disableOrientationListener(){
    if(mOrientationEventListener != null){
        mOrientationEventListener.disable();
    }
}

And you should set mOrientation as orientation attribute for you image.

ALiGOTec
  • 337
  • 2
  • 15
  • orientation detected from my camera app for any picture taken is 0.So as per your instructions it must be something like exif = new ExifInterface(file.getAbsolutePath()); exif.setAttribute(ExifInterface.TAG_ORIENTATION,0); exif.saveAttributes(); isn't it ? – hacker Nov 11 '12 at 05:45
  • if i set like this, the orientation value will be 1,2,3 or 4 right ? But orientation values we wanted to be like 3,6 and 8 for different images..How can this happen ? – hacker Nov 12 '12 at 06:15
  • Sorry for the messy code, it is used to set rotation for a view, but you can you can start from the code above and correct based on [link](http://developer.android.com/reference/android/media/ExifInterface.html) – ALiGOTec Nov 12 '12 at 08:25
2

This problem was solved a long time ago but I encountered some difficulties to make camera work correctly so here is my final solution, without using exif. I hope this will help others :

public void startPreview() {
        try {
            Log.i(TAG, "starting preview: " + started);

            // ....
            Camera.CameraInfo camInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(cameraIndex, camInfo);
            int cameraRotationOffset = camInfo.orientation;
            // ...

            Camera.Parameters parameters = camera.getParameters();
            List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
            Camera.Size previewSize = null;
            float closestRatio = Float.MAX_VALUE;

            int targetPreviewWidth = isLandscape() ? getWidth() : getHeight();
            int targetPreviewHeight = isLandscape() ? getHeight() : getWidth();
            float targetRatio = targetPreviewWidth / (float) targetPreviewHeight;

            Log.v(TAG, "target size: " + targetPreviewWidth + " / " + targetPreviewHeight + " ratio:" + targetRatio);
            for (Camera.Size candidateSize : previewSizes) {
                float whRatio = candidateSize.width / (float) candidateSize.height;
                if (previewSize == null || Math.abs(targetRatio - whRatio) < Math.abs(targetRatio - closestRatio)) {
                    closestRatio = whRatio;
                    previewSize = candidateSize;
                }
            }

            int rotation = getWindowManager().getDefaultDisplay().getRotation();
            int degrees = 0;
            switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break; // Natural orientation
            case Surface.ROTATION_90:
                degrees = 90;
                break; // Landscape left
            case Surface.ROTATION_180:
                degrees = 180;
                break;// Upside down
            case Surface.ROTATION_270:
                degrees = 270;
                break;// Landscape right
            }
            int displayRotation;
            if (isFrontFacingCam) {
                displayRotation = (cameraRotationOffset + degrees) % 360;
                displayRotation = (360 - displayRotation) % 360; // compensate
                                                                    // the
                                                                    // mirror
            } else { // back-facing
                displayRotation = (cameraRotationOffset - degrees + 360) % 360;
            }

            Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = "
                    + displayRotation);

            this.camera.setDisplayOrientation(displayRotation);

            int rotate;
            if (isFrontFacingCam) {
                rotate = (360 + cameraRotationOffset + degrees) % 360;
            } else {
                rotate = (360 + cameraRotationOffset - degrees) % 360;
            }

            Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate);

            Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height);
            parameters.setPreviewSize(previewSize.width, previewSize.height);
            parameters.setRotation(rotate);
            camera.setParameters(parameters);
            camera.setPreviewDisplay(mHolder);
            camera.startPreview();

            Log.d(TAG, "preview started");

            started = true;
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }
Louis GRIGNON
  • 634
  • 4
  • 15
  • 1
    In my opinion if you are taking the picture yourself and have access to the camera...this is the correct solution and also somewhat inline with what Android docs recommend: see setRotation() http://developer.android.com/reference/android/hardware/Camera.Parameters.html Basically you can use the camera.info along with sensor orientation to determine how many degrees to use in your postRotate call. I SPENT MANY HOURS on this...this approach is the most reliable and simplistic if you have access to the camera – tronious Mar 13 '15 at 06:55
  • After searching lots of answers and forums I found this is the working solution. Thanks! – Dharmendra Jan 26 '16 at 13:58
  • The code is too much for a single method. It is hell of a mess like that. – toshkinl Jun 29 '16 at 12:29
  • Yeah, I am kind of a clean code lover too, but for a SO answer, it's pretty straightforward to read. Clean code is not the point here, you can obviously split this method in your own project ;) – Louis GRIGNON Sep 07 '16 at 09:50
0

You are ignoring an Exception at :

    try {
        // Code
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } catch (Exception e) {

    }

Try :

    try {
        // Code
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } catch (Exception e) {
        e.printStackTrace();
    }

And post logcat. It is never wise to thow away exceptions.

Binoy Babu
  • 15,749
  • 17
  • 87
  • 127
  • If you have checked and there are no exceptions, It must be something else. You were ignoring at least four FileNotFoundException and IOException. Just wanted to check. – Binoy Babu Nov 09 '12 at 10:57