Hug*_*ugo 6 android barcode zxing
我正在使用 Zxing 库从带有以下代码的字符串数字代码生成条形码位图:
public 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 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;
}
Run Code Online (Sandbox Code Playgroud)
它运行良好,但它只生成图像中的条形码,其中没有数字。当我想用下面的数字在 ImageView 中绘制它时,问题就出现了,因为我们通常在条形码中看到它们。我尝试使用 textview 但它们太小或太大,具体取决于屏幕和 ImageView 大小。
问题是,有没有办法使用Zxing库在位图中包含条形码下方的代码编号?或者还有其他简单的解决方案作为 TextView 或 sth 中可调整大小的文本吗?
提前致谢!
| 归档时间: |
|
| 查看次数: |
1702 次 |
| 最近记录: |