Android 发票pdf文件(带签章)转bitmap图片

1. 背景

最近有一个需求,是一个自助发票打印的功能。顾客交易完成可以凭二维码自助开票发票打印。开票成功后会生成一个pdf文件(带签章)。

我们对接的打印机直接使用pdf文件进行打印会因为尺寸问题导致打印失败,所以将pdf文件转换成图片进行打印(主要是贪图这款打印机便宜,懂的都懂)。

这里就有一个问题,如果直接读取文件数据转换成图片,会把签章数据给搞丢,网上找了一下,没有找到解决方法,时间又有点紧迫,所以这里搞了些奇技淫巧:使用AndroidPdfViewer加载pdf文件,然后将控件的内容保存成bitmap,然后再将图片转化为黑色加深字体颜色进行打印。

2. 这里使用Java进行实现

下面为主要的实现代码

2.1 AndroidPdfViewer控件加载发票pdf文件
// 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 将图片转换成黑色,加深字体(获取更清晰的打印内容)
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;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码将PDF文件换为图片: ```java import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.pdf.PdfRenderer; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); File pdfFile = new File("path/to/pdf/file.pdf"); try { // create a new PdfRenderer object PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY)); // get the number of pages in the PDF file final int pageCount = renderer.getPageCount(); // get the first page of the PDF file PdfRenderer.Page page = renderer.openPage(0); // create a new bitmap object Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(), Bitmap.Config.ARGB_8888); // create a new canvas object Canvas canvas = new Canvas(bitmap); // set the background color of the canvas object canvas.drawColor(Color.WHITE); // render the PDF page into the canvas object page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); // set the bitmap image to the ImageView imageView.setImageBitmap(bitmap); // close the page object page.close(); // close the renderer object renderer.close(); // save the bitmap image as a JPEG file FileOutputStream out = new FileOutputStream("path/to/output/file.jpg"); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 注意替换代码中的文件路径。此代码将PDF文件的第一页换为图片,并将其显示在ImageView中,然后将其保存为JPEG文件。你可以更改代码以换多页PDF文件或更改输出图像的格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值