Java 前摄像头倒置图片问题

Java 前摄像头倒置图片问题,java,android,eclipse,camera,photo,Java,Android,Eclipse,Camera,Photo,我刚刚在我的应用程序中发现了一个bug,但不知道如何修复它。这是一个简单的照相机/照片应用程序,但问题是,当用户使用前置照相机拍照时,编辑器中的图片会被颠倒渲染。如果用户使用后(主)摄像头拍照,一切正常 这是我的CameraBitmapUtils.java类中的代码 package com.example.myapp.common; import java.io.FileNotFoundException; import java.io.IOException; import java.io.

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

这是我的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;
}

}
package com.example.myapp.common;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.InputStream;
导入android.util.DisplayMetrics;
导入android.util.Log;
导入android.util.TypedValue;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.content.res.Resources;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Matrix;
导入android.media.ExifInterface;
导入android.net.Uri;
公共类CameraBitmapUtils{
私有静态最终字符串TAG=“CameraBitmapUtils”;
公共静态int-calculateInSampleSize(BitmapFactory.Options选项、int-reqWidth、int-reqHeight){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
@SuppressLint(“新API”)
公共静态位图decodeSampledBitmap(Uri、int-reqWidth、int-reqHeight、Activity act){
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
输入流为;
试一试{
is=act.getApplicationContext().getContentResolver().openInputStream(uri);
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码流(is,null,options);
(考虑)使用IS.Max和IS.Read代替[todo]
//计算样本大小
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
//使用inSampleSize集合解码位图
//再次打开输入流
is=act.getApplicationContext().getContentResolver().openInputStream(uri);
options.inJustDecodeBounds=false;
位图ret=BitmapFactory.decodeStream(is,null,options);
is.close();
返回ret;
}catch(filenotfounde异常){
Log.v(标记“未找到文件:”+uri.toString());
e、 printStackTrace();
}捕获(IOE异常){
Log.v(标记“I/O异常和文件:”+uri.toString());
e、 printStackTrace();
}
返回null;
}
公共静态位图downSampleBitmap(Uri、Activity act、Boolean needRotate){
DisplayMetrics DisplayMetrics=新的DisplayMetrics();
act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
Resources r=act.getResources();
int px=(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,r.getDisplayMetrics());/50:
//魔力
//num
int targetWidth=displaymetrics.heightPixels;
int targetHeight=displaymetrics.widthPixels-px;
位图resizedBitmap=decodeSampledBitmap(uri、targetWidth、TargetLight、act);
位图返回位图=空;
出口接口;
试一试{
浮动度=0;
exif=newexifinterface(uri.toString());
if(uri.getScheme().equals(“内容”)){
/*
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
Cursor Cursor=act.getContentResolver().query(uri,filePathColumn,null,null);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
String filePath=cursor.getString(columnIndex);
cursor.close();
*/
字符串filePath=ImageFilePath.getPath(act,uri);
///新的27/9
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(文件路径,o);
//我们要扩展到的新尺寸
所需的最终int_SIZE=1024;
//找到正确的刻度值。它应该是
// 2.
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp<要求的\u尺寸和高度\u tmp<要求的\u尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
位图b1=位图工厂.decodeFile(文件路径,o2);
returnBitmap=exufitls.rotateBitmap(文件路径,b1);
}否则{
int-orient=exif.getAttributeInt(ExifInterface.TAG\u方向,ExifInterface.ORIENTATION\u法线);
if(resizedBitmap!=null&&needRotate){
度=获得度(方向);
如果(度!=0){
returnBitmap=createRotatedBitmap(resizedBitmap,度);
}
returnBitmap=returnBitmap==null?resizedBitmap:returnBitmap;
}
}
}捕获(IOE异常){
Log.v(标记“在下采样时未找到文件”);
e、 printStackTrace();
}
返回位图;
}
公共静态浮点数getDegree(内部存在方向){
如果(ExiforOrientation==ExifinInterface.ORIENTATION\u ROTATE\u 90)
返回180;
else if(exiforOrientation==ExifInterface.ORIENTATION\u ROTA