捕获的图像在某些设备中旋转

时间:2018-06-17 12:28:25

标签: android image rotation

我在我的项目中使用imagePicker从图库中选择图像或从相机中捕获图像但是当我从设备的相机拍摄图像时,图像会旋转。我有下面列出的旋转功能,但图像不会旋转。

这是我的ImagePicker java类:

package il.co.ntech.constinspect.custom;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import il.co.ntech.constinspect.BuildConfig;
import il.co.ntech.constinspect.R;

public class ImagePicker {

   private static final int DEFAULT_MIN_WIDTH_QUALITY = 400;        // min pixels
   private static final String TAG = "ImagePicker";
   private static final String TEMP_IMAGE_NAME = "tempImage";

   public static int minWidthQuality = DEFAULT_MIN_WIDTH_QUALITY;


public static Intent getPickImageIntent(Context context) {
    Intent chooserIntent = null;

    List<Intent> intentList = new ArrayList<>();

    Intent pickIntent = new Intent(Intent.ACTION_PICK,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra("return-data", true);
    Uri.fromFile(getTempFile(context))); crashes in api 26 and higher (8 oreo)
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(context,
            BuildConfig.APPLICATION_ID + ".provider",
            getTempFile(context)));
    intentList = addIntentsToList(context, intentList, pickIntent);
    intentList = addIntentsToList(context, intentList, takePhotoIntent);

    if (intentList.size() > 0) {
        chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
                context.getResources().getString(R.string.attach_photo_by));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
    }

    return chooserIntent;
}

private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) {
    List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo resolveInfo : resInfo) {
        String packageName = resolveInfo.activityInfo.packageName;
        Intent targetedIntent = new Intent(intent);
        targetedIntent.setPackage(packageName);
        list.add(targetedIntent);
        Log.d(TAG, "Intent: " + intent.getAction() + " package: " + packageName);
    }
    return list;
}


public static Bitmap getImageFromResult(Context context, int resultCode,
                                        Intent imageReturnedIntent) {
    Log.d(TAG, "getImageFromResult, resultCode: " + resultCode);
    Bitmap bm = null;
    File imageFile = getTempFile(context);
    if (resultCode == Activity.RESULT_OK) {
        Uri selectedImage;
        boolean isCamera = (imageReturnedIntent == null ||
                imageReturnedIntent.getData() == null  ||
                imageReturnedIntent.getData().toString().contains(imageFile.toString()));
        if (isCamera) {     /** CAMERA **/
           // selectedImage = Uri.fromFile(imageFile);
            selectedImage = FileProvider.getUriForFile(context,
                    BuildConfig.APPLICATION_ID + ".provider",
                    imageFile);
        } else {            /** ALBUM **/
            selectedImage = imageReturnedIntent.getData();
        }
        Log.d(TAG, "selectedImage: " + selectedImage);

        bm = getImageResized(context, selectedImage);
        int rotation = getRotation(context, selectedImage, isCamera);
        bm = rotate(bm, rotation);
    }
    return bm;
}


private static File getTempFile(Context context) {
    File imageFile = new File(context.getExternalCacheDir(), TEMP_IMAGE_NAME);
    imageFile.getParentFile().mkdirs();
    return imageFile;
}

private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize;

    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor(
            fileDescriptor.getFileDescriptor(), null, options);

    Log.d(TAG, options.inSampleSize + " sample method bitmap ... " +
            actuallyUsableBitmap.getWidth() + " " + actuallyUsableBitmap.getHeight());

    return actuallyUsableBitmap;
}

/**
 * Resize to avoid using too much memory loading big images (e.g.: 2560*1920)
 **/
private static Bitmap getImageResized(Context context, Uri selectedImage) {
    Bitmap bm = null;
    int[] sampleSizes = new int[]{5, 3, 2, 1};
    int i = 0;
    do {
        bm = decodeBitmap(context, selectedImage, sampleSizes[i]);
        Log.d(TAG, "resizer: new bitmap width = " + bm.getWidth());
        i++;
    } while (bm.getWidth() < minWidthQuality && i < sampleSizes.length);
    return bm;
}


private static int getRotation(Context context, Uri imageUri, boolean isCamera) {
    int rotation;
    if (isCamera) {
        rotation = getRotationFromCamera(context, imageUri);
    } else {
        rotation = getRotationFromGallery(context, imageUri);
    }
    Log.d(TAG, "Image rotation: " + rotation);
    return rotation;
}

private static int getRotationFromCamera(Context context, Uri imageFile) {
    int rotate = 0;
    try {

        context.getContentResolver().notifyChange(imageFile, null);
        ExifInterface exif = new ExifInterface(imageFile.getPath());
        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;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

public static int getRotationFromGallery(Context context, Uri imageUri) {
    int result = 0;
    String[] columns = {MediaStore.Images.Media.ORIENTATION};
    Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(imageUri, columns, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int orientationColumnIndex = cursor.getColumnIndex(columns[0]);
            result = cursor.getInt(orientationColumnIndex);
        }
    } catch (Exception e) {
        //Do nothing
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }//End of try-catch block
    return result;
}


private static Bitmap rotate(Bitmap bm, int rotation) {
    if (rotation != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        Bitmap bmOut = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        return bmOut;
    }
    return bm;
}
}

这就是我将拾取的图像作为位图的方式:

pickBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent chooseImageIntent = ImagePicker.getPickImageIntent(context);
            fragment.startActivityForResult(chooseImageIntent, IMAGE_PICKER_REQUEST_CODE);
        }
    });

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

    switch (requestCode) {

        /*   image picker result  */
        case IMAGE_PICKER_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                Bitmap bmReport = null;

                try {

                    bmReport = ImagePicker.getImageFromResult(getContext(), resultCode, data);
                } catch (FileUriExposedException e) {
                    e.printStackTrace();
                    Log.d(TAG,"file uri exception");
                }
                try {
                    if (bmReport != null) {
                       bmReport = AppUtils.getResizedBitmap(bmReport, SIZE_BYTES_CAPTURED_IMAGE);
                        img.setImageBitmap(bmReport);

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(TAG,"Exception: "+e.getMessage());
                }
            }
            break;
    }
}

我用照相机拍照后,将图像视图中显示的照片向左旋转,即使我已经添加了旋转功能来处理旋转图像。

任何人都可以帮助我如何对齐图像吗?

0 个答案:

没有答案