【问题标题】:Taking picture with camera intent rotate picture in portrait mode android用相机意图拍照以肖像模式android旋转图片
【发布时间】:2014-03-13 15:54:58
【问题描述】:

使用相机成功拍摄了照片,但在三星 Galaxy s3 的人像模式下,照片会旋转。我该如何解决这个问题。 相机意图如下:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(xdestination));
        startActivityForResult(intent, CAMERA_PIC_REQUEST);

结果活动中

 if (requestCode==CAMERA_PIC_REQUEST){

            //  Bitmap bm = (Bitmap) data.getExtras().get("data"); 

                Uri photo_uri = Uri.fromFile(xdestination);

                Editer.PHOTO_FROM=11;

                Bitmap decodeSampledBitmapFromFile=null;
                try {
                    decodeSampledBitmapFromFile = decodeUri(photo_uri);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                decodeSampledBitmapFromFile.compress(Bitmap.CompressFormat.JPEG,100, bytes);

                File f  = new File(Environment.getExternalStorageDirectory(), "user_image.jpg");

                try {

                    if(f.exists())
                        f.delete();

                    f.createNewFile();

                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());
                    fo.close();

                } catch (IOException e) {

                    e.printStackTrace( );
                    Log.d("ERROR", e.toString());
                }

            }

【问题讨论】:

  • 我也遇到了同样的问题,拍完后我旋转了图片。
  • 你能告诉我你是如何旋转图片的吗?如果可能,请发布您的代码或部分代码
  • 试试我下面的代码,让我知道它是否适合你。
  • 好,但不是我的问题,我需要旋转保存在路径 File f = new File(Environment.getExternalStorageDirectory(), "user_image.jpg");

标签: android android-camera android-camera-intent


【解决方案1】:

将您拍摄的图片和该图片的 SDCard 路径传递给以下方法,该方法将返回正确方向的图片...

private Bitmap imageOreintationValidator(Bitmap bitmap, String path) {

    ExifInterface ei;
    try {
        ei = new ExifInterface(path);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            bitmap = rotateImage(bitmap, 90);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            bitmap = rotateImage(bitmap, 180);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            bitmap = rotateImage(bitmap, 270);
            break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

private Bitmap rotateImage(Bitmap source, float angle) {

    Bitmap bitmap = null;
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    try {
        bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                matrix, true);
    } catch (OutOfMemoryError err) {
        err.printStackTrace();
    }
    return bitmap;
}

【讨论】:

  • 对不起,我做了 decodeSampledBitmapFromFile = imageOreintationValidator(decodeSampledBitmapFromFile,f.getPath()); decodeSampledBitmapFromFile.compress(Bitmap.CompressFormat.JPEG,100, bytes);但横向效果图仍然没有
  • 如何从相机获取“字符串路径”?
【解决方案2】:

使用:

public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
    int rotate = 0;
    try {
        context.getContentResolver().notifyChange(imageUri, null);
        File imageFile = new File(imagePath);

        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.i("RotateImage", "Exif orientation: " + orientation);
        Log.i("RotateImage", "Rotate value: " + rotate);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

【讨论】:

    【解决方案3】:

    像下面这样试试。

    File photo = new File(Environment.getExternalStorageDirectory(), "user_image.jpg");
            if (photo.exists()) {
                Bitmap myBitmap = BitmapFactory.decodeFile(photo
                        .getAbsolutePath());
                imageView.setImageBitmap(myBitmap);
                imageView.setRotation(90);
            }
    

    第 1 步:提供照片的完整路径,例如 File photo = new File(Environment.getExternalStorageDirectory() , "Face.jpg");

    第2步:检查照片是否存在,如果存在则将其设置为图像视图并将其旋转90度。

    【讨论】:

      【解决方案4】:

      您别无选择,只能读取图像,将其旋转并将结果写回 SD 卡。使用 BitmapFactory 的直接方法很容易run into OutOfMemory exception

      或者,您可以使用JPEG lossless rotation,使用jpegtran

      在 SourceForge 上有一个 Java 开源类LLJTran。 Android 端口是on GitHub

      【讨论】:

        【解决方案5】:

        今天我遇到了这个问题。我知道我迟到了,但以防万一有人需要。在我的例子中,如果我在相机意图中传递 Uri,那么输出文件就会被旋转。也就是说,

        fun takePhoto() {
                val intent = Intent(android.media.action.IMAGE_CAPTURE)
                intent.putExtra(MediaStore.EXTRA_OUTPUT, 
                        Uri.fromFile(xdestination)) // <-- this line is the culprit for me
                startActivityForResult(intent, CAMERA_PIC_REQUEST)
        }
        

        然后在 onActivity 结果中,您将旋转位图。我不知道为什么会这样。

        所以我没有为意图提供 Uri。那就是我写的:

        fun takePhoto() {
                val intent = Intent(android.media.action.IMAGE_CAPTURE)
                startActivityForResult(intent, CAMERA_PIC_REQUEST)
        }
        

        因此,在onActivityResult 中,我将返回的图像作为位图。然后我手动将该位图保存在所需的文件/Uri 中。

        
          override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == PHOTO_CAPTURE_WITH_CAMERA_REQ_CODE && resultCode == RESULT_OK) {
              // 1. handle the image as normal bitmap
              val photo: Bitmap? = data?.extras?.get("data") as Bitmap;
              // handleImage(SharedData.pendingPhotoUri.pop())
              if (photo != null) {
                // inside handle camera
                handleCameraImage(photo)
              };
            }
          }
        
          private fun handleCameraImage(inputBitmap: Bitmap) {
            //2. according to previous implementation, this uri should be the final output image file.
        
            // 3. save the original bitmap in a temp image file
            val tempBitmapFile: String = requireActivity().cacheDir.absolutePath +"/temp_photo_"+System.currentTimeMillis();
            val fOuttemp: FileOutputStream = FileOutputStream(tempBitmapFile);
            inputBitmap.compress(Bitmap.CompressFormat.PNG, 85, fOuttemp);
            fOuttemp.flush();
            fOuttemp.close();
        
            // 4. use ExifInterface to make sure the photo is in right orientation.
            val exifInterface: ExifInterface = ExifInterface(tempBitmapFile);
            val orientation: Int = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        
            Timber.tag(TAG).e("orientation == " + orientation);
            var matrix = Matrix();
            var isRotationNeeded: Boolean = true;
            when(orientation){
              ExifInterface.ORIENTATION_NORMAL -> {
                Timber.tag(TAG).e("when ExifInterface.ORIENTATION_NORMAL");
                // do nothing I guess
                isRotationNeeded = false;
              }
              ExifInterface.ORIENTATION_ROTATE_90 -> {
                Timber.tag(TAG).e("when ExifInterface.ORIENTATION_ROTATE_90");
                matrix.setRotate(90f);
              }
              ExifInterface.ORIENTATION_ROTATE_180 -> {
                Timber.tag(TAG).e("when ExifInterface.ORIENTATION_ROTATE_180");
                matrix.setRotate(180f);
              }
              ExifInterface.ORIENTATION_ROTATE_270 -> {
                Timber.tag(TAG).e("when ExifInterface.ORIENTATION_ROTATE_270");
                matrix.setRotate(270f);
              }
              else -> {
                Timber.tag(TAG).e("when else");
                isRotationNeeded = false;
              }
            }
        
            // if in wrong orientation,
            //    rotate image and save it in the file from step 2.
            // else save the original image in the file from step 2.
            lateinit var outputBitmap: Bitmap;
            if(!isRotationNeeded) {
              outputBitmap = inputBitmap;
            }else{
              outputBitmap = Bitmap.createBitmap(inputBitmap, 0, 0, inputBitmap.width,
                  inputBitmap.height, matrix, true);
            }
        
            val outputBitmapfile: File = File("your_awesome_file");
        
            val fOut: FileOutputStream = FileOutputStream(outputBitmapfile);
            outputBitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
            fOut.flush();
            fOut.close();
        
            var outputPhotoUri:Uri = getUriForFile(requireActivty(), outputBitmapfile);
        
        
        
            // 5. finally use this photo / Uri to do whatever you want
        
          }
        
          fun getUriForFile(context: Context, file: File?): Uri? {
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
              try {
                val destCopiedTempFile = File(context.getExternalFilesDir(null).toString() + File.separator + "temp")
                FileBackend.copy(file, destCopiedTempFile)
                FileProvider.getUriForFile(context,
                    BuildConfig.APPLICATION_ID + ".fileprovider", destCopiedTempFile)
              } catch (e: IllegalArgumentException) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                  throw SecurityException(e)
                } else {
                  Uri.fromFile(file)
                }
              }
            } else {
              Uri.fromFile(file)
            }
          }
        
        

        这对我有用。

        【讨论】:

          猜你喜欢
          • 2014-04-19
          • 2013-10-03
          • 2015-05-12
          • 2012-10-07
          • 1970-01-01
          • 2013-03-22
          • 2015-10-21
          • 1970-01-01
          • 2012-06-23
          相关资源
          最近更新 更多