Java 在安卓系统中,如何确定前摄像头和后摄像头拍摄图像后的图像旋转度?

Java 在安卓系统中,如何确定前摄像头和后摄像头拍摄图像后的图像旋转度?,java,android,android-activity,camera,Java,Android,Android Activity,Camera,从相机拍摄图像后,此图像在前相机和后相机之间以两个不同的角度旋转。我尝试在从相机拍摄图像后向图像添加旋转,但它不能以相同的角度与所有设备一起工作 注意:我添加了人脸识别,但当图像旋转时它不起作用,这是我的代码 public class CheckIn extends AppCompatActivity { private ImageView camera; String encoded = ""; byte[] byteArray; static final i

从相机拍摄图像后,此图像在前相机和后相机之间以两个不同的角度旋转。我尝试在从相机拍摄图像后向图像添加旋转,但它不能以相同的角度与所有设备一起工作

注意:我添加了人脸识别,但当图像旋转时它不起作用,这是我的代码

public class CheckIn extends AppCompatActivity  {
    private ImageView camera;
    String encoded = "";
    byte[] byteArray;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    private Button checkIn;
    ContentValues values;
    public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
    Uri imageUri;
    Bitmap thumbnail;
    SparseArray<Face> faces ;
    Matrix matrix ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_in);
        camera = (ImageView) findViewById(R.id.camera);
        checkIn = (Button) findViewById(R.id.submit);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, "New Picture");
                values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
                imageUri = getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
            }
        });

        checkPermissionREAD_EXTERNAL_STORAGE(this);

    }

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

        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

            try {
               thumbnail = MediaStore.Images.Media.getBitmap(
                        getContentResolver(), imageUri);

                if(Build.VERSION.SDK_INT >= 27) {
                    //only api 27 above
                    matrix  = new Matrix();
                    matrix.postRotate(90);
                }else{
                    //only api 27 down
                    matrix  = new Matrix();
                    matrix.postRotate(270);
                }

                Bitmap rotatedBitmap = Bitmap.createBitmap(thumbnail, 0, 0, thumbnail.getWidth(), thumbnail.getHeight(), matrix, true);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                Bitmap converetdImage = getResizedBitmap(rotatedBitmap, 700);
                converetdImage.compress(Bitmap.CompressFormat.PNG, 10, byteArrayOutputStream);
                byteArray = byteArrayOutputStream.toByteArray();
                encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
                RequestOptions options = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.mipmap.ic_launcher_round)
                        .error(R.mipmap.ic_launcher_round);

                Paint myRectPaint = new Paint();
                myRectPaint.setStrokeWidth(5);
                myRectPaint.setColor(Color.RED);
                myRectPaint.setStyle(Paint.Style.STROKE);

                Bitmap tempBitmap = Bitmap.createBitmap(rotatedBitmap.getWidth(), rotatedBitmap.getHeight(),
                        Bitmap.Config.RGB_565);
                Canvas tempCanvas = new Canvas(tempBitmap);
                tempCanvas.drawBitmap(rotatedBitmap, 0, 0, null);
                FaceDetector faceDetector
                 = new
                        FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false)
                        .build();

                Frame frame = new Frame.Builder().setBitmap(rotatedBitmap).build();
                faces = faceDetector.detect(frame);

                for (int i = 0; i < faces.size(); i++) {
                    Face thisFace = faces.valueAt(i);
                    float x1 = thisFace.getPosition().x;
                    float y1 = thisFace.getPosition().y;
                    float x2 = x1 + thisFace.getWidth();
                    float y2 = y1 + thisFace.getHeight();
                    tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);

                }
                Glide.with(CheckIn.this).load(tempBitmap).apply(options)
                        .into(camera);

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

    public void checkPermissionREAD_EXTERNAL_STORAGE(
            final Context context) {
        // Enable if permission granted
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
                PackageManager.PERMISSION_GRANTED) {
        }
// Else ask for permission
        else {
            ActivityCompat.requestPermissions(this, new String[]
                    {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

    public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float) width / (float) height;
        if (bitmapRatio > 1) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
    }
}
公共类签入扩展了AppCompative活动{
私人影像摄像机;
字符串编码=”;
字节[]字节数组;
静态最终整数请求\图像\捕获=1;
私人按钮签入;
内容价值观;
公共静态最终int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE=123;
Uri-imageUri;
位图缩略图;
稀疏面;
矩阵;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u check\u in);
摄像头=(ImageView)findViewById(R.id.camera);
签入=(按钮)findViewById(R.id.submit);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u ADJUST\u PAN);
camera.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
values=新的ContentValues();
value.put(MediaStore.Images.Media.TITLE,“新图片”);
value.put(MediaStore.Images.Media.DESCRIPTION,“来自您的相机”);
imageUri=getContentResolver()。插入(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,值);
意向意向=新意向(MediaStore.ACTION\u IMAGE\u CAPTURE);
intent.putExtra(MediaStore.EXTRA_输出,imageUri);
startActivityForResult(意图、请求、图像捕获);
}
});
checkPermissionREAD_外部_存储(此);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==REQUEST\u IMAGE\u CAPTURE&&resultCode==RESULT\u OK){
试一试{
缩略图=MediaStore.Images.Media.getBitmap(
getContentResolver(),imageUri);
如果(Build.VERSION.SDK_INT>=27){
//仅适用于上述api 27
矩阵=新矩阵();
矩阵旋转后(90);
}否则{
//只有空气污染指数27下降
矩阵=新矩阵();
矩阵旋转后(270);
}
位图旋转位图=Bitmap.createBitmap(缩略图,0,0,缩略图.getWidth(),缩略图.getHeight(),矩阵,true);
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream();
位图ConvertDimage=getResizedBitmap(旋转位图,700);
压缩(Bitmap.CompressFormat.PNG,10,byteArrayOutputStream);
byteArray=byteArrayOutputStream.toByteArray();
encoded=Base64.encodeToString(byteArray,Base64.DEFAULT);
RequestOptions=newrequestoptions()
.centerCrop()
.占位符(R.mipmap.ic\u启动器\u轮)
.错误(R.mipmap.ic_启动器_轮);
绘制myRectPaint=新绘制();
myRectPaint。设置行程宽度(5);
myRectPaint.setColor(Color.RED);
myRectPaint.setStyle(Paint.Style.STROKE);
位图tempBitmap=Bitmap.createBitmap(rotatedBitmap.getWidth(),rotatedBitmap.getHeight(),
Bitmap.Config.RGB_565);
Canvas tempCanvas=新画布(tempBitmap);
drawBitmap(旋转位图,0,0,null);
面部检测器面部检测器
=新的
FaceDetector.Builder(getApplicationContext()).SetTrackinEnabled(false)
.build();
Frame Frame=new Frame.Builder().setBitmap(rotatedBitmap.build();
面=面检测器。检测(帧);
对于(int i=0;i1){
宽度=最大尺寸;
高度=(int)(宽度/位图比率);
}否则{
高度=最大尺寸;
宽度=(int)(高度*位图比率);
}
返回位图.createScaledBitmap(图像、宽度、高度、真值);
}
}
提前谢谢

请试试这个