繁体   English   中英

使用相机意图拍照在纵向模式下旋转图片android

[英]Taking picture with camera intent rotate picture in portrait mode android

照片是用相机成功拍摄的,但在三星 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());
                }

            }

将您拍摄的图片和该图片的 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;
}

像下面一样尝试。

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度。

用 :

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

您别无选择,只能读取图像,旋转它并将结果写回 SD 卡。 使用 BitmapFactory 的直接方法很容易遇到 OutOfMemory 异常

或者,您可以使用jpegtran使用JPEG 无损旋转

在 SourceForge 上,有一个 Java 开源类LLJTran Android 端口位于 GitHub 上

今天我遇到了这个问题。 我知道我迟到了,但以防万一有人需要它。 就我而言,如果我在相机意图中传递 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)
    }
  }

这对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM