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

时间:2014-02-14 10:27:40

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

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

            }

5 个答案:

答案 0 :(得分:18)

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

答案 1 :(得分:0)

尝试如下。

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

答案 2 :(得分:0)

使用:

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 :(得分:0)

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

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

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

答案 4 :(得分:0)

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

这对我有用。

相关问题