Basically, I have a rectangular bitmap and want to create a new Bitmap with squared dimensions which will contain the rectangular bitmap inside of it.
So, for example, if the source bitmap has width:100 and height:400, I want a new bitmap with width:400 and height:400. Then, draw the source bitmap centered inside of this new bitmap (see attached image for a better understanding).
My code below creates the square bitmap fine, but the source bitmap is not being drawn into it. As a result, I'm left with a bitmap that is completely black.
Here is the code:
Bitmap sourceBitmap = BitmapFactory.decodeFile(sourcePath);
Bitmap resultBitmap= Bitmap.createBitmap(sourceBitmap.getHeight(), sourceBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(resultBitmap);
Rect sourceRect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight());
Rect destinationRect = new Rect((resultBitmap.getWidth() - sourceBitmap.getWidth())/2, 0, (resultBitmap.getWidth() + sourceBitmap.getWidth())/2, sourceBitmap.getHeight());
c.drawBitmap(resultBitmap, sourceRect, destinationRect, null);
// save to file
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
File file = new File(mediaStorageDir.getPath() + File.separator + "result.jpg");
try {
result.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Any idea what I'm doing wrong?
Try this:
private static Bitmap createSquaredBitmap(Bitmap srcBmp) {
int dim = Math.max(srcBmp.getWidth(), srcBmp.getHeight());
Bitmap dstBmp = Bitmap.createBitmap(dim, dim, Config.ARGB_8888);
Canvas canvas = new Canvas(dstBmp);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(srcBmp, (dim - srcBmp.getWidth()) / 2, (dim - srcBmp.getHeight()) / 2, null);
return dstBmp;
}
Whoops, just realized what the problem is. I was drawing the wrong Bitmap
to the Canvas
. If it helps anyone in the future, remember that the Canvas is already attached and will paint to the bitmap you specify in its constructor. So basically:
This:
c.drawBitmap(resultBitmap, sourceRect, destinationRect, null);
Should actually be:
c.drawBitmap(sourceBitmap, sourceRect, destinationRect, null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With