Android invoice pdf file (with signature) to bitmap image

1. Background

Recently, there is a demand for a self-service invoice printing function. After the customer's transaction is completed, the invoice can be printed by self-service invoicing with the QR code. After the billing is successful, a pdf file (with signature) will be generated.

The printer we docked directly uses the pdf file to print, and the printing will fail due to the size problem, so the pdf file is converted into a picture for printing (mainly because the printer is cheap, and everyone understands it).

There is a problem here. If you directly read the file data and convert it into a picture, the signature data will be lost. I searched the Internet, but I didn’t find a solution, and the time is a bit tight, so here are some tricks: Use AndroidPdfViewer to load the pdf file, then save the content of the control as a bitmap, and then convert the picture to black and deepen the font color for printing.

2. Implemented here using Java

The following is the main implementation code

2.1 AndroidPdfViewer control loads invoice pdf file
// invoucePdf为AndroidPdfViewer控件,com.github.barteksc.pdfviewer.PDFView
// 设置控件渲染内容保存到缓存中生成bitmap,下面这两个方法已经过时,有需要可以查看方法源码获取
invoicePdf.setDrawingCacheEnabled(true);
invoicePdf.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
// 这里可以直接获取图片, 这里图片的宽高跟控件的宽高一样
Bitmap invoiceBitmap = invoicePdf.getDrawingCache();

// https://github.com/barteksc/AndroidPdfViewer
invoicePdf.fromFile(file)
    .enableAnnotationRendering(true)  // 调用该方法设置为true,将签章渲染出来
    .load();

// 获取PDFView控件的渲染截图
Bitmap invoiceBitmap;
// bmWidth, bmHeight为图片的宽高,根据实际情况设置
invoiceBitmap = Bitmap.createBitmap(bmWidth, bmHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(invoiceBitmap);
invoicePdf.draw(canvas);
2.2 Convert the picture to black and darken the font (for a clearer print)
private Bitmap convertBitMap2Black(Bitmap invoiceBitmap) {
    
    

    int width = invoiceBitmap .getWidth();
    int height = invoiceBitmap .getHeight();

    int[] pixels = new int[width * height];

    // 从发票图片中获取对应像素
    invoiceBitmap .getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i=0; i < height; i++) {
    
    
        for (int j=0; j<width; j++) {
    
    
            int pixel = pixels[width * i + j];

            // 根据RGB位置,移位获取对应的像素值
            int red = ((pixel  & 0x00FF0000 ) >> 16);
            int green = ((pixel & 0x0000FF00) >> 8);
            int blue = (pixel & 0x000000FF);

            // 像素值越大,表示颜色越淡,若图片像素内容小于0xB0(该值可变),直接将该像素点置为黑色
            if (red < 0xB0 || green < 0xB0 || blue < 0xB0) {
    
    
                pixels[width * i + j] = 0xFF000000;
            }
        }
    }
    
    // 保存用于打印的黑白图片
    Bitmap  printBitMap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    printBitMap.setPixels(pixels, 0, width, 0, 0, width, height);
    
    return printBitMap;

Guess you like

Origin blog.csdn.net/qq_36224961/article/details/128327841