Android打印图片

抄自安卓官方开发文档,翻墙不易,英文日后再翻译吧,https://developer.android.com/training/printing/photos.html#image

Printing Photos



Taking and sharing photos is one of the most popular uses for mobile devices. If your application takes photos, displays them, or allows users to share images, you should consider enabling printing of those images in your application. The Android Support Library provides a convenient function for enabling image printing using a minimal amount of code and simple set of print layout options.

This lesson shows you how to print an image using the v4 support library PrintHelper class.

Print an Image


The Android Support Library PrintHelper class provides a simple way to print of images. The class has a single layout option, setScaleMode(), which allows you to print with one of two options:

  • SCALE_MODE_FIT - This option sizes the image so that the whole image is shown within the printable area of the page.
  • SCALE_MODE_FILL - This option scales the image so that it fills the entire printable area of the page. Choosing this setting means that some portion of the top and bottom, or left and right edges of the image is not printed. This option is the default value if you do not set a scale mode.

Both scaling options for setScaleMode() keep the existing aspect ratio of the image intact. The following code example shows how to create an instance of the PrintHelper class, set the scaling option, and start the printing process:

private void doPhotoPrint() {
    PrintHelper photoPrinter = new PrintHelper(getActivity());
    photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.droids);
    photoPrinter.printBitmap("droids.jpg - test print", bitmap);
}

This method can be called as the action for a menu item. Note that menu items for actions that are not always supported (such as printing) should be placed in the overflow menu. For more information, see the Action Bar design guide.

After the printBitmap() method is called, no further action from your application is required. The Android print user interface appears, allowing the user to select a printer and printing options. The user can then print the image or cancel the action. If the user chooses to print the image, a print job is created and a printing notification appears in the system bar.

If you want to include additional content in your printouts beyond just an image, you must construct a print document. For information on creating documents for printing, see the Printing an HTML Document or Printing a Custom Document lessons.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,可以使用PrintManager类来实现打印图片的功能。下面是一个简单的示例代码: 1. 首先,将需要打印图片转换成Bitmap对象。可以使用BitmapFactory类的decodeResource方法或者decodeStream方法来加载图片资源。 2. 创建一个PrintAttributes对象来设置打印属性,例如纸张大小、页面方向等。可以使用PrintAttributes.Builder类来构建PrintAttributes对象。 3. 创建一个PrintDocumentAdapter对象,用于将打印内容提供给打印系统。可以继承PrintDocumentAdapter类,并重写其onWrite方法来实现自定义的打印内容。 4. 在onWrite方法中,可以使用Canvas对象来绘制图片。可以使用printAttributes属性来获取页面的宽度和高度,然后使用Canvas的drawBitmap方法将图片绘制到画布上。 5. 在Activity或Fragment中,通过PrintManager的print方法来触发打印操作。在print方法中,需要传入一个打印作业名称和PrintDocumentAdapter对象。 以下是示例代码: ```java // 将图片资源转换成Bitmap对象 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image); // 创建打印属性 PrintAttributes attributes = new PrintAttributes.Builder() .setMediaSize(PrintAttributes.MediaSize.ISO_A4) .setOrientation(PrintAttributes.ORIENTATION_PORTRAIT) .build(); // 创建打印文档适配器 PrintDocumentAdapter adapter = new PrintDocumentAdapter() { @Override public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) { try { // 创建页面 PrintedPdfDocument pdfDocument = new PrintedPdfDocument(context, attributes); // 开始绘制页面 PdfDocument.Page page = pdfDocument.startPage(0); Canvas canvas = page.getCanvas(); RectF contentRect = new RectF(canvas.getClipBounds()); // 将图片绘制到页面上 canvas.drawBitmap(bitmap, null, contentRect, null); // 结束绘制页面 pdfDocument.finishPage(page); // 将绘制好的PDF保存到文件中 pdfDocument.writeTo(new FileOutputStream(destination.getFileDescriptor())); // 关闭打印文档 pdfDocument.close(); // 打印完成回调 callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES}); } catch (IOException e) { // 打印出错回调 callback.onWriteFailed(e.getMessage()); } } // 其他方法省略... }; // 调用PrintManager的print方法触发打印操作 PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); String jobName = getString(R.string.print_job_name); printManager.print(jobName, adapter, null); ``` 通过上述代码,可以实现在Android应用中打印图片的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值