The Base64 of Bitmaps created on two different android devices is not identical

276 Views Asked by At

I create a bitmap after doing some canvas and paint operations and then Base64 encode it to a string. When I repeat the process on a separate device and compare the base64 encoded strings returned by the two devices, they are different. Any ideas on why that would be the case ?

Code that generates the bitmap -

Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.save();
canvas.rotate(45, midX, midY);
canvas.restore();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(45);
paint.setTextAlign(Align.CENTER);
paint.setTextColor(Color.parseColor(colorString));
StaticLayout staticLayout = new StaticLayout("Text", paint, width,Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
staticLayout.draw(canvas);

Code that converts the bitmap to a Base64 encoded string -

    int size = bitmap.getRowBytes() * bitmap.getHeight();
    byte[] byteArray;

    ByteBuffer byteBuffer = ByteBuffer.allocate(size);
    bitmap.copyPixelsToBuffer(byteBuffer);
    byteArray = byteBuffer.array();

    String encodedString =  Base64.encodeToString(byteArray, Base64.NO_WRAP);
2

There are 2 best solutions below

0
On

Even if the fonts are exactly the same, hinting (both of the character glyphs and lines) will be dependent on the underlying hardware as well as the browser.

This is a well known characteristic of the HTML canvas.

If you want to be able to generate exactly the same image on 2 different devices then you'd need to work on uint array with your own primitives for drawing / your own font handlers.

0
On

It's not clear where the bitmap object from the second code snippet is coming from, so there could be loads of explanation.

Though, my guess is the bitmap gets converted into device pixels at some point, and that would explain why you get different results on different device, depending on screen size and resolution.

It's unlikely this is an issue with Base64 encoding method, and you could verify that by comparing the byte array.