Rastaman Rastaman - 6 months ago 14
Android Question

Combine Image and Text to print them

I want to create an .png to print it. The file should have this layout (100x35mm)
enter image description here

I created the QrCode using the Zxing project.

public Bitmap createQrCode(String stringForQrCode){
com.google.zxing. MultiFormatWriter writer =new MultiFormatWriter();

String data = stringForQrCode;
String finaldata = Uri.encode(data, "utf-8");

BitMatrix bm = null;
try {
bm = writer.encode(finaldata, BarcodeFormat.QR_CODE,185, 185);
} catch (WriterException e) {
e.printStackTrace();
}
Bitmap ImageBitmap = Bitmap.createBitmap(185, 185, Bitmap.Config.ARGB_8888);

for (int i = 0; i < 185; i++) {//width
for (int j = 0; j < 185; j++) {//height
assert bm != null;
ImageBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK: Color.WHITE);
}
}
return ImageBitmap;
}


But how can I combine these image with my two Strings. Do I have to create an new bitmap? And How to I chose the position of the different Items.

Answer

I solved it by creating an bitmap from the desired View. Therefore I create a View which includes my qrcode and both strings. But instead of displaying the View, I set it on invisible and use the following code to create and save the bitmap.

public static Bitmap getBitmapFromView(View view, int width, int height) {
        Bitmap returnedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null) {
            bgDrawable.draw(canvas);
        } else {
            canvas.drawColor(Color.WHITE);
        }
        view.draw(canvas);
        return returnedBitmap;
    }