在Android中的三星设备上的摄像头捕获方向

and*_*per 13 android screen-orientation android-camera android-camera-intent android-orientation

我正在创建一个相机应用程序.捕获时的图像显示在网格视图中.现在,代码在除三星设备之外的所有设备上都能正常工作.

我正面临着方向问题.当我以纵向模式拍摄图像时,图像在网格视图中显示时会旋转.我没有保留任何旋转代码.其次,使用EXIF我在网格视图中获得了正确的图像,但是当设备方向改变时,图像再次以旋转的方式旋转.

附加图片: 在此输入图像描述

在此输入图像描述

对不起图像的分辨率.请知道它们是否不正确.将再次上传.我知道SO上有很多这样的帮助.但我想我在某个地方被困住了.

我指的是以下链接:

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

can*_*ova 12

这是我用它完成的代码(它适用于每个设备):

这部分是我将拍摄的照片设置为主要活动中的imageview的地方:

            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);
Run Code Online (Sandbox Code Playgroud)

相机活动中的常数值:

  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;
Run Code Online (Sandbox Code Playgroud)

相机活动中的回调函数:

      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();      
          }
      };
Run Code Online (Sandbox Code Playgroud)

在相机活动中拍摄照片功能:

      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();
      }
Run Code Online (Sandbox Code Playgroud)

在相机活动中在onresume中调用的方向监听器:

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;
                            }                       
                        }
                    }

                }
            };
Run Code Online (Sandbox Code Playgroud)


Ren*_*ira 8

以下是我在我的应用中用于旋转并在所有设备中工作的代码:

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);
    }
Run Code Online (Sandbox Code Playgroud)