前置摄像头倒置图片问题

时间:2015-05-25 20:12:15

标签: java android eclipse camera photo

我刚在我的应用中发现了一个错误,但不知道如何修复它。这是一个简单的相机/照片应用程序,但问题是,当用户使用前置摄像头拍摄照片时,编辑器中的图片会呈现倒置。如果用户使用后(主)相机拍照,一切正常。

这是我的CameraBitmapUtils.java类中的代码。

package com.example.myapp.common;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;

public class CameraBitmapUtils {
private static final String TAG = "CameraBitmapUtils";

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

@SuppressLint("NewApi")
public static Bitmap decodeSampledBitmap(Uri uri, int reqWidth, int reqHeight, Activity act) {

    // First decode with inJustDecodeBounds=true to check dimensions
    InputStream is;
    try {
        is = act.getApplicationContext().getContentResolver().openInputStream(uri);
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, options);
        is.close(); // consider use is.mark and is.reset instead [TODO]

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        // open input stream again
        is = act.getApplicationContext().getContentResolver().openInputStream(uri);
        options.inJustDecodeBounds = false;
        Bitmap ret = BitmapFactory.decodeStream(is, null, options);
        is.close();
        return ret;
    } catch (FileNotFoundException e) {
        Log.v(TAG, "File not found:" + uri.toString());
        e.printStackTrace();
    } catch (IOException e) {
        Log.v(TAG, "I/O exception with file:" + uri.toString());
        e.printStackTrace();
    }
    return null;
}

public static Bitmap downSampleBitmap(Uri uri, Activity act, Boolean needRotate) {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    Resources r = act.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics()); // 50:
                                                                                                        // magic
                                                                                                        // num
    int targetWidth = displaymetrics.heightPixels;
    int targetHeight = displaymetrics.widthPixels - px;

    Bitmap resizedBitmap = decodeSampledBitmap(uri, targetWidth, targetHeight, act);
    Bitmap returnBitmap = null;
    ExifInterface exif;
    try {
        float degree = 0;
        exif = new ExifInterface(uri.toString());

        if (uri.getScheme().equals("content")) {
 /*
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = act.getContentResolver().query(uri, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

 */
            String filePath  = ImageFilePath.getPath(act, uri);
            // /new 27/9

            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 1024;

            // Find the correct scale value. It should be the power of
            // 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap b1 = BitmapFactory.decodeFile(filePath, o2);
            returnBitmap = ExifUtils.rotateBitmap(filePath, b1);

        } else {
            int orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            if (resizedBitmap != null && needRotate) {
                degree = getDegree(orient);
                if (degree != 0) {
                    returnBitmap = createRotatedBitmap(resizedBitmap, degree);
                }
                returnBitmap = returnBitmap == null ? resizedBitmap : returnBitmap;
            }
        }

    } catch (IOException e) {
        Log.v(TAG, "not found file at downsample");
        e.printStackTrace();
    }
    return returnBitmap;
}

public static float getDegree(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90)
        return 180;
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180)
        return 270;
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270)
        return 0;

    return 90;
}

public static Bitmap createRotatedBitmap(Bitmap bm, float degree) {
    Bitmap bitmap = null;
    if (degree != 0) {
        Matrix matrix = new Matrix();
        matrix.preRotate(degree);
        bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
    }

    return bitmap;
}

}

如果我在return 90;上设置了return 270;,则前置摄像头已固定,但后置摄像头的照片却是颠倒的。这段代码不是我的,所以我真的不知道如何修复它。

0 个答案:

没有答案