【问题标题】:How to fix degree of image rotation after take image from front camera and back camera in android?在android中从前置摄像头和后置摄像头拍摄图像后如何修复图像旋转度数?
【发布时间】:2020-01-30 09:00:06
【问题描述】:

从摄像头拍摄图像后,此图像在前置摄像头和后置摄像头之间以两个不同的角度旋转,我尝试在从摄像头拍摄图像后为图像添加旋转,但它不适用于所有设备以相同的角度。

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

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

提前致谢。

【问题讨论】:

  • 你可能遇到和this one一样的问题。

标签: java android android-activity camera


【解决方案1】:

请试试这个

我只需要添加

camera.setDisplayOrientation(90);

现在显示在正确的角度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2021-03-14
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多