簡體   English   中英

在Android中的三星設備上的攝像頭捕獲方向

[英]Camera capture orientation on samsung devices in android

我正在創建一個相機應用程序。 捕獲時的圖像顯示在網格視圖中。 現在,代碼在除三星設備之外的所有設備上都能正常工作。

我正面臨着方向問題。 當我以縱向模式拍攝圖像時,圖像在網格視圖中顯示時會旋轉。 我沒有保留任何旋轉代碼。 其次,使用EXIF我在網格視圖中獲得了正確的圖像,但是當設備方向改變時,圖像再次以旋轉的方式旋轉。

附加圖片: 在此輸入圖像描述

在此輸入圖像描述

對不起圖像的分辨率。 請知道它們是否不正確。 將再次上傳。 我知道SO上有很多這樣的幫助。 但我想我在某個地方被困住了。

我指的是以下鏈接:

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

這是我用它完成的代碼(它適用於每個設備):

這部分是我將拍攝的照片設置為主要活動中的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);

相機活動中的常數值:

  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;

相機活動中的回調函數:

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

在相機活動中拍攝照片功能:

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

在相機活動中在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;
                            }                       
                        }
                    }

                }
            };

以下是我在我的應用中用於旋轉並在所有設備中工作的代碼:

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

首先,您需要獲得原始文件方向 -

      try {
                        ExifInterface exif = new ExifInterface("File AbsolutePath");
                        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
                        Bitmap bm = rotateBitmap("Old Bitmap", orientation);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

您需要編寫一個方法,在將其旋轉到正確的方向后返回Bitmap。

public Bitmap rotateBitmap(Bitmap bitmap, int orientation) throws IOException {

        Matrix matrix = new Matrix();
        switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bitmap;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.setRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();
            return bmRotated;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM