您好我是Android新手,我目前正在尝试将Android 4.4.2表格中的收据打印到我的Zicox热敏收据打印机。到目前为止我能够打印文本,但现在我需要更进一步打印条形码/ qrcodes。不幸的是,这超出了我的知识范围,我用Google搜索了解决方案,还没有为我找到一个。
这些是我用来生成条形码的方法:
/**************************************************************
* getting from com.google.zxing.client.android.encode.QRCodeEncoder
*
* See the sites below
* http://code.google.com/p/zxing/
* http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/encode/EncodeActivity.java
* http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java
*/
private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;
Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException {
String contentsToEncode = contents;
if (contentsToEncode == null) {
return null;
}
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contentsToEncode);
if (encoding != null) {
hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result;
try {
result = writer.encode(contentsToEncode, format, img_width, img_height, hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
private static String guessAppropriateEncoding(CharSequence contents) {
// Very crude at the moment
for (int i = 0; i < contents.length(); i++) {
if (contents.charAt(i) > 0xFF) {
return "UTF-8";
}
}
return null;
}
这是我启动整个过程的onClick方法:
// barcode data
String barcode_data = "123456";
// barcode image
ImageView iv = new ImageView(this);
try {
barCode = encodeAsBitmap(barcode_data, BarcodeFormat.CODE_128, 300, 40);
// bitmap.getRowBytes();
iv.setImageBitmap(barCode);
} catch (WriterException e) {
e.printStackTrace();
}
iv.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
innerLayout.addView(iv);
所以现在我基本上能够生成并显示条形码,现在我希望能够在我的收据上打印。
答案 0 :(得分:0)
我从你的问题中可以理解,你基本上想要打印一个包含QR码的位图。
您可以使用PrintHelper课程。以下是官方文档中的示例代码,其中显示了如何使用它。
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);
}
另外,它说:
调用printBitmap()方法后,无需进一步执行应用程序操作。将出现Android打印用户界面,允许用户选择打印机和打印选项。
更新:
上述方法仅打印位图。为了打印整个布局(如收据,如你所说),已经列出了here的说明。我会在这里为你总结一下:
第一步是扩展PrintDocumentAdapter类并覆盖某些方法。在该适配器中,有一个称为onWrite()
的回调方法,用于在要打印的文件上绘制内容。在此方法中,您必须使用Canvas对象来绘制内容。该对象具有绘制位图/行/文本等的所有辅助方法。
然后,当请求打印时,获取“PrintManager”类的实例,如下所示:
PrintManager printManager = (PrintManager) getActivity()
.getSystemService(Context.PRINT_SERVICE);
然后,将print方法调用为:
printManager.print(jobName, new CustomDocumentAdapter(getActivity()),
null);
如果有帮助,请告诉我。