Android Studio Camera - Overflow an image over actual cameraview

19 views Asked by At

I am coding an app that use camera api 2 to take pictures and overflow last captured image on actual cameraview. The effect is not working as I want because the image is not overflowed very well over realtime camera view. I post the code where i rotate and resize the last captured image and the jpeg where is shown the error. the image is not alligned well. I am using a bluetooth button and a tripod so the phone is not shaked during taking picture.

cameraview

code where i fit last captured image:

@Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Update overlay image every second (adjust the condition based on your needs)
        if (lastCapturedBitmap != null) {
            Bitmap rotatedBitmap = rotateBitmap(lastCapturedBitmap, 90);
            //overlayImageView.setImageBitmap(rotateBitmap(lastCapturedBitmap, 90));
            //overlayImageView.setAlpha(0.5f);
            int surfaceViewWidth = textureView.getWidth();
            int surfaceViewHeight = textureView.getHeight();
            Bitmap scaledBitmap = scaleBitmap(rotatedBitmap, surfaceViewWidth, surfaceViewHeight);
            overlayImageView.setImageBitmap(scaledBitmap);
            overlayImageView.setVisibility(View.VISIBLE);
        }
    }
};

private Bitmap rotateBitmap(Bitmap bitmap, float degrees) {
    Matrix matrix = new Matrix();
    matrix.postRotate(degrees);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

private Bitmap scaleBitmap(Bitmap bitmap, int targetWidth, int targetHeight) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    float scaleWidth = ((float) targetWidth) / width;
    float scaleHeight = ((float) targetHeight) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}
0

There are 0 answers