Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera capture orientation on samsung devices in android

I am creating a camera app. The image when captured is shown in the grid view. Now, the code is working completely fine on all the devices except for samsung devices.

I am facing the orientation issue. When I capture an image in a portrait mode, the image rotates when displayed in the gridview. I have not kept any rotate code. Secondly, with the EXIF I achieved the proper image in the grid view but when the device orientation changes, again the image rotates in a wiered fashion.

Attaching images: enter image description here

enter image description here

Sorry for the resolution of the image. Please lemme know if they are not visible properly. Will upload again. I know there are lot such help on SO. But I guess I am stuck up somewhere.

I am referring the following link:

http://blog.andolasoft.com/2013/06/how-to-show-captured-images-dynamically-in-gridview-layout.html

like image 359
android developer Avatar asked Aug 13 '13 07:08

android developer


People also ask

How do I fix the camera orientation on my Android?

Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.

What is the orientation of image in camera?

The orientation of a Camera Station defines the position of and the pointing direction of the camera at the time of exposure of a photograph. Orientation can also refer to the process of determining the orientation of one or more Camera Stations.


2 Answers

This is the code I've done this with (it is working for every device):

this part is where I set the taken photo to the imageview in the main activity:

            try {
                File imageFile = new File(cursor.getString(0));
                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;
                }

                Log.v("", "Exif orientation: " + orientation);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Matrix matrix = new Matrix();
            matrix.postRotate(rotate);
            bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
            testImage.setImageBitmap(null);
            testImage.setImageBitmap(bmp);

constant values in camera activity:

  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;
  private OrientationEventListener mOrientationEventListener;
  private int mOrientation =  -1;

callback function in camera activity:

      Camera.PictureCallback photoCallback=new Camera.PictureCallback(){
          public void onPictureTaken(final byte[] data, final Camera camera){

              dialog=ProgressDialog.show(CameraActivity.this,"","Please wait while the photo is being saved..");
              new Thread(){
                  public void run(){
                      try{
                          Thread.sleep(1000);         
                      }
                      catch(Exception ex){}
                      onPictureTake(data,camera);     
                  }
              }.start();      
          }
      };

take photo function in camera activity:

      public void onPictureTake(byte[] data, Camera camera){
          switch (mOrientation) {
          case ORIENTATION_PORTRAIT_NORMAL:
              rotate = 90;
              break;
          case ORIENTATION_LANDSCAPE_NORMAL:
              rotate = 0;
              break;
          case ORIENTATION_PORTRAIT_INVERTED:
              rotate = 270;
              break;
          case ORIENTATION_LANDSCAPE_INVERTED:
              rotate = 180;
              break;
          }

          Matrix matrix = new Matrix();
          matrix.postRotate(rotate);
          bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
          bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
          mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
          savePhoto(mutableBitmap);
          dialog.dismiss();
          flag = 0;
          finish();
      }

orientation listenner which is called in onresume in camera activity:

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {

                @SuppressWarnings("deprecation")
                @Override
                public void onOrientationChanged(int orientation) {

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

                    Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();   
                    int rotation = getWindowManager().getDefaultDisplay().getRotation();
                    System.out.println(rotation+"");

                if (display.getOrientation() != Surface.ROTATION_0) {   // landscape oriented devices
                        System.out.println("LANDSCAPE");
                        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
                        System.out.println("PORTRAIT");
                        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;
                            }                       
                        }
                    }

                }
            };
like image 149
canova Avatar answered Oct 12 '22 23:10

canova


Here is the code that I used in my app to rotate and works in all devices:

private Bitmap adjustImageOrientation(Bitmap image) {
        ExifInterface exif;
        try {
            exif = new ExifInterface(picturePath);
            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 = image.getWidth();
                int h = image.getHeight();

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

                // Rotating Bitmap & convert to ARGB_8888, required by tess
                image = Bitmap.createBitmap(image, 0, 0, w, h, mtx, false);

            }
        } catch (IOException e) {
                 return null;
        }
        return image.copy(Bitmap.Config.ARGB_8888, true);
    }
like image 26
Renan Bandeira Avatar answered Oct 13 '22 01:10

Renan Bandeira