13

Tôi đang tạo ứng dụng máy ảnh. Hình ảnh khi chụp được hiển thị trong giao diện lưới. Bây giờ, mã hoạt động hoàn toàn tốt trên tất cả các thiết bị ngoại trừ thiết bị samsung.Hướng chụp ảnh máy ảnh trên các thiết bị Samsung trong android

Tôi đang gặp vấn đề về định hướng. Khi tôi chụp ảnh ở chế độ dọc, ảnh sẽ xoay khi hiển thị trong chế độ xem lưới. Tôi đã không giữ bất kỳ mã xoay nào. Thứ hai, với EXIF ​​tôi đã đạt được hình ảnh thích hợp trong chế độ xem lưới nhưng khi định hướng thiết bị thay đổi, một lần nữa hình ảnh quay theo kiểu thời trang bị nhiễu.

Gắn hình ảnh: enter image description here

enter image description here

Xin lỗi vì độ phân giải của hình ảnh. Vui lòng cho chúng tôi biết nếu chúng không hiển thị đúng cách. Sẽ tải lên lại. Tôi biết có rất nhiều trợ giúp trên SO. Nhưng tôi đoán tôi đang mắc kẹt ở đâu đó.

tôi đề cập liên kết sau:

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

Trả lời

12

Đây là code tôi đã thực hiện việc này với (nó hoạt động cho mọi thiết bị):

phần này là nơi tôi đặt ảnh đã chụp thành lần xem hình ảnh trong hoạt động chính:

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

giá trị không đổi trong hoạt động camera: chức năng

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 trong hoạt động camera:

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

mất chức năng ảnh trong hoạt động camera:

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

hướng listenner mà được gọi là onr esume trong hoạt động camera:

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

       } 
      }; 
+0

u làm cho tôi ngày ... nhờ ... Tôi đã thực hiện một sai lầm ngớ ngẩn ... nhờ một lần nữa –

+0

vui vì tôi có thể giúp. –

+0

làm việc chỉ dành cho samsung! ! –

8

Đây là mã mà tôi sử dụng trong ứng dụng của tôi để xoay và làm việc tại tất cả các thiết bị:

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

này được xoay màn hình điện thoại của tôi :( – NarendraJi

1

Trước tiên, bạn cần phải nhận được các tập tin gốc orientation--

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

Bạn cần phải viết một phương pháp mà trả lại Bitmap sau khi xoay nó để đi đúng hướng.

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; 
     } 
    } 
Các vấn đề liên quan