简体   繁体   English

如何旋转从相机或画廊拍摄的图像?

[英]How rotate image taken from camera or gallery.?

I am capturing image from camera and selecting image from gallery. 我正在从相机捕获图像并从图库中选择图像。 In samsung devices the images gets rotate after captured. 在三星设备中,图像在捕获后会旋转。

I want rotate image to straight if they are rotated. 如果要旋转图像,我希望将图像旋转为直线。

I tried to do it but its not working. 我尝试这样做,但无法正常工作。

   private void onCaptureImageResult(Intent data) {
    try {

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".png");

    FileOutputStream fo;

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


    profileImage = destination;

    Bitmap rotatedBitmap = modifyOrientation(thumbnail, profileImage.getAbsolutePath());

    ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage.
    byte[] byteArray1 = stream1.toByteArray();

    File tempFile1 = File.createTempFile("temp", null, getCacheDir());
    FileOutputStream fos1 = new FileOutputStream(tempFile1);
    fos1.write(byteArray1);


    if (rotatedBitmap != null) {
        profileImageView.setImageBitmap(rotatedBitmap);
        profileImage = tempFile1;
    } else {
        profileImageView.setImageBitmap(thumbnail);
        profileImage = destination;
    }

}
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {

    Bitmap bm=null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage.
    byte[] byteArray = stream.toByteArray();

    try {

        File tempFile = File.createTempFile("temp",null, getCacheDir());
        FileOutputStream fos = new FileOutputStream(tempFile);
        fos.write(byteArray);

        profileImage = tempFile;

        Bitmap rotatedBitmap = modifyOrientation(bm,profileImage.getAbsolutePath());

        ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage.
        byte[] byteArray1 = stream1.toByteArray();

        File tempFile1 = File.createTempFile("temp",null, getCacheDir());
        FileOutputStream fos1 = new FileOutputStream(tempFile1);
        fos1.write(byteArray1);



        if(rotatedBitmap != null) {
            profileImageView.setImageBitmap(rotatedBitmap);
            profileImage = tempFile1;
        }
        else {
            profileImageView.setImageBitmap(bm);
            profileImage = tempFile;
        }
    }

    catch (IOException e)
    {

    }

}

EDIT: 编辑:

I tried to use camera intent now and get path from intent still its not working. 我现在尝试使用相机意图,并从意图获取路径仍然无法正常工作。

private void onCaptureImageResult(Intent data) {
    try {

    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".png");

    FileOutputStream fo;

        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
        Bitmap rotatedBitmap = null;

   // profileImage = destination;

        Uri tempUri = getImageUri(getApplicationContext(),thumbnail);

        // CALL THIS METHOD TO GET THE ACTUAL PATH
        File finalFile = new File(getRealPathFromURI(tempUri));

        ExifInterface ei = new ExifInterface(finalFile.getAbsolutePath());
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotateImage(thumbnail, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotateImage(thumbnail, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotateImage(thumbnail, 270);
                break;
            case ExifInterface.ORIENTATION_NORMAL:
            default:
                break;
        }

    if (rotatedBitmap != null) {

        profileImageView.setImageBitmap(rotatedBitmap);

        ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
        rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1); //replace 100 with desired quality percentage.
        byte[] byteArray1 = stream1.toByteArray();

        File tempFile1 = File.createTempFile("temp", null, getCacheDir());
        FileOutputStream fos1 = new FileOutputStream(tempFile1);
        fos1.write(byteArray1);

        profileImage = tempFile1;
    } else {
        profileImageView.setImageBitmap(thumbnail);
        profileImage = destination;
    }

}
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
   }


public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}



   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}

What's wrong now? 现在怎么了

Can anyone help please? 有人可以帮忙吗? What's going wrong? 怎么了 Thank you.. 谢谢..

profileImage = destination;

You took the thumbnail which is a Bitmap and wrote it to file. 您将缩略图作为位图,并将其写入文件。

Then after that you used that file to extract an exifinterface. 然后,使用该文件提取exifinterface。

But bitmaps do not contain exif information. 但是位图不包含exif信息。 And hence your file does not either. 因此,您的文件也不会。

So your orientation is always null; 因此,您的方向始终为空;

If you had used profileImage to launch the camera intent then leave it as is. 如果您曾使用profileImage启动相机意图,则将其保持原样。

So remove above statement. 因此,删除上述声明。

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

And put this code in Activity result method and get value to rotate image... 并将此代码放入Activity结果方法中并获取值以旋转图像...

String selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();

        int rotateImage = getCameraPhotoOrientation(MyActivity.this, selectedImage, filePath);

Rotate Bitmap using this 使用此旋转位图

 public static Bitmap rotate(Bitmap bitmap, float degrees) {
   Matrix matrix = new Matrix();
   matrix.postRotate(degrees);
   return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
  }

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

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