I let users to choose image resolution, for example 4096x3024px then get that mutable bitmap from the stream then send it to surfaceview and create another canvas, which is to be used to scale from screen size to bitmap size.
When i scale this canvas up image is not upscaled correctly. I tried creating a matrix scaled it up from a center pivot but still no result.
This where i get the bitmap from camera preview and draw to canvas used for scaling and saving
public Bitmap mergeImageAndCanvas(Bitmap bitmap) {
// DRAW TO Canvas created in constructor
canvasImage.setBitmap(bitmap);
render(canvasImage, true);
canvasImage.setBitmap(null);
System.out.println("Canvas " + canvas.getWidth() + ", height " + canvas.getHeight());
return bitmap;
}
Here i upScale and try to center image to maintain exact position of texts and lines i draw
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Toast.makeText(getContext(), "Surfaceview surfaceChanged() width " +
// width + ", height" + height,
// Toast.LENGTH_LONG).show();
bitmapWitdh = photoHandler.photoWidth;
bitmapHeight = photoHandler.photoHeight;
canvasWidth = width;
ratioX = (float) canvasWidth / 1280;
canvasHeight = height;
scaleX = ((float) bitmapWitdh) / (float) canvasWidth;
scaleY = ((float) bitmapHeight) / (float) canvasHeight;
float middleX = bitmapWitdh / 2.0f;
float middleY = bitmapHeight / 2.0f;
scaleMatrix = new Matrix();
// scaleMatrix.preScale(scaleX, scaleY);
scaleMatrix.setScale(scaleX, scaleY, middleX, middleY);
// scaleMatrix.postTranslate(-(bitmapWitdh) / 2, -(bitmapHeight) / 2);
// scaleMatrix.setTranslate(300, 300);
canvasImage.setMatrix(scaleMatrix);
}
Here is the what i wish to achive after upscale(This is what user sees on screen with resolution 1280x720) Image Screen
This what user gets(image resolution 2560x1920) Final image
Solution to scale canvas for any resolution is just to scale it with Canvas.setScale()
method. Matrix scaling is applied from origin, which is (0,0),top- left of the screen by default, it converts canvas dimension to any other dimension via scaleX and scaleY factors you get from dividing desired resolution(dimensions of the captured image from camera) by current resolution(device resolution) width and height.
It's basically matching captured image and device's screen dimensions.