Henrique
Henrique

Reputation: 5011

How to create a square bitmap from a rectangular bitmap in Android

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).

Example of the expected result

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?

Upvotes: 9

Views: 10811

Answers (3)

Chinese Cat
Chinese Cat

Reputation: 5428

/**
 * Add a shape to bitmap.
 */
fun addShapeToBitmap(originBitmap: Bitmap): Bitmap {

    val stream = ByteArrayOutputStream()

    // **create RectF for a rect shape**
    val rectF = RectF()
    rectF.top = originBitmap.height / 2f - 100f
    rectF.bottom = originBitmap.height / 2f + 100f
    rectF.left = originBitmap.width / 2f - 100f
    rectF.right = originBitmap.width / 2f + 100f

    // **create a new bitmap which is an empty bitmap**
    val newBitmap = Bitmap.createBitmap(originBitmap.width, 
    originBitmap.height, originBitmap.config)
    // **create a canvas and set the empty bitmap as the base layer**
    val canvas = Canvas(newBitmap)
    // **draw the originBitmap on the first laye
    canvas.drawBitmap(originBitmap, 0f, 0f, null)
    // **draw the shape on the second layer**
    // **the second layer cover on the first layer**
    canvas.drawRect(rectF, paint)
    newBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
    newBitmap.recycle()
    return newBitmap
}

Upvotes: 0

sergey.n
sergey.n

Reputation: 1757

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

Upvotes: 21

Henrique
Henrique

Reputation: 5011

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

Upvotes: 2

Related Questions