Base64 Bitmaps created on two different Android devices is not identical

I create a bitmap after doing some canvas and paint operations, and then Base64 encodes it into 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 this is?

The code that generates the bitmap is

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

The code that converts the bitmap to a Base64 encoded string is

    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);
+4
source share
3 answers

Even if the fonts exactly match, hints (both glyphs and strings) will depend on the underlying hardware, as well as the browser.

HTML canvas.

, uint / .

+4

, , .

, , - , , , .

Base64, , .

+2

, , ,

 /**
 * <p> get bitmap from input and return BASE64 String image  </p>
 * @param bmp source bitmap
 * @return return string of image on main thread
 *
 */
public static String getStringImage( Bitmap bmp)
{
    try
    {
        if (!bmp.isRecycled())
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();

            return Base64.encodeToString(imageBytes, Base64.DEFAULT);
        }
        else
        {
            return null;
        }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
        return null;
    }
}

if you don’t know what a bitmap is, check out this post . this method takes a bitmap as an argument and returns a Base64 String image.

-2
source

Source: https://habr.com/ru/post/1691905/


All Articles