2

after take image from camera this image rotate with tow different degree between front camera and back camera I tried add rotation to image after take image from camera but it doesn't work with all devices by the same degree .

Note : I added face recognition but it doesn't work when images rotate,this is my code .

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

thanks in advance .

1 Answers1

2

try this please

i just have to add

camera.setDisplayOrientation(90);

now the display is on the right angle.

jon mina
  • 23
  • 3