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

Android 使用相机拍照,在纵向模式下旋转图片,android,android-camera,android-camera-intent,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, CAMER

这张照片是用相机成功拍摄的,但在三星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 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卡路径传递到以下方法,该方法将返回正确的定向照片

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

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

或者,您可以使用,使用


在SourceForge上,有一个Java开源类。Android端口是。

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

fun takePhoto(){
val intent=intent(android.media.action.IMAGE_-CAPTURE)
intent.putExtra(MediaStore.EXTRA_输出,

Uri.fromFile(xdestination))//我也遇到了同样的问题,我在拍摄后旋转了图片。你能告诉我你是如何旋转图片的吗?如果可能的话,请发布你的代码或部分代码。请尝试下面的代码,让我知道它是否适合你。很好,但不适合我的问题,我需要旋转保存在路径文件f=新文件中的图像(Environment.getExternalStorageDirectory(),“user_image.jpg”);抱歉我没有解码SampleDbitMapFromFile=ImageOrEntationValidator(decodeSampledBitmapFromFile,f.getPath());decodeSampledBitmapFromFile.compress(Bitmap.CompressFormat.JPEG,100,字节);但没有效果图像仍在Landscape中如何获取“字符串路径”从摄像机里?