【问题标题】:how to set captured image using android in sumsung device is portrait mode如何在三星设备中使用 android 设置捕获的图像是纵向模式
【发布时间】:2016-03-25 07:14:39
【问题描述】:

我正在尝试使用以下代码在 sumsung 设备中使用 android 设置捕获的图像是纵向模式,但它设置为横向模式。以下代码适用于除三星和索尼以外的其他设备。

Matrix mat = new Matrix();
ExifInterface exif = new ExifInterface(yourimagepath);
String orientstring = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientstring != null ? Integer.parseInt(orientstring) : ExifInterface.ORIENTATION_NORMAL;
int rotateangle = 0;
if(orientation == ExifInterface.ORIENTATION_ROTATE_90) 
            rotateangle = 90;
if(orientation == ExifInterface.ORIENTATION_ROTATE_180) 
            rotateangle = 180;
if(orientation == ExifInterface.ORIENTATION_ROTATE_270) 
            rotateangle = 270;

mat.setRotate(rotateangle, (float) bmpPic.getWidth() / 2, (float) bmpPic.getHeight() / 2);

File f = new File(yourimagepath);       
Bitmap bmpPic = BitmapFactory.decodeStream(new FileInputStream(f), null, null); 
Bitmap bmpPic1 = Bitmap.createBitmap(bmpPic, 0, 0, bmpPic.getWidth(), bmpPic.getHeight(), mat, true); 

还有其他解决方案吗?请提出建议。

【问题讨论】:

    标签: android android-camera portrait


    【解决方案1】:

    在您的清单文件中的活动标记中使用它..

    android:screenOrientation="portrait"

    它可以为所有设备设置纵向模式。

    【讨论】:

      【解决方案2】:

      使用 ExifInterface 检查存储在设备中的图像的方向。

      int rotate = 0;
      try {
          File imageFile = new File(uploadFile.getPath());
          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;
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
      

      然后使用矩阵将位图旋转到存储在设备中的实际纵向或横向。

      Matrix matrix = new Matrix();
      
      matrix.postRotate(rotate);
      
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = false;
      options.inPreferredConfig = Bitmap.Config.RGB_565;
      options.inDither = true;
      
      Bitmap bmp = BitmapFactory.decodeFile(uploadFile.getPath(), options);
      
      Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, bmp.getWidth(), bmp.getHeight(), true);
      
      Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
      

      rotatedBitmap 是方向正确的位图。

      希望对你有帮助。

      【讨论】:

        【解决方案3】:

        试试这个代码。我曾在包括三星在内的不同手机上使用它,并且效果很好:

        public Bitmap rotateImage(Bitmap bitmap) throws IOException {
                //Rotate the image to get in correct position
                ExifInterface exif = new ExifInterface(mImagePath);
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                Matrix matrix = new Matrix();
                if (orientation == 6) {
                    matrix.postRotate(90);
                } else if (orientation == 8) {
                    matrix.postRotate(270);
                } else if (orientation == 3) {
                    matrix.postRotate(180);
                }
        
                Bitmap rotatedImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                return rotatedImage;
            }
        

        希望对你有帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-03
          • 2012-06-16
          • 2013-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-29
          • 1970-01-01
          相关资源
          最近更新 更多