有一个开源的 Java 类库叫做 “zxing” (Zebra Crossing),可以用来读写不同类型的条形码,包括二维码。
我测试了 zxing,它可以读取一个嵌在 100 dpi 的灰度文档中的条形码!
![]() |
| QR Code |
![]() |
| UPC-A |
![]() |
PDF417 code |
1. 下载 zxing 类库
你可以在这里下载 zxing 的源代码。你需要用下载的源代码来编译出两个jar,core.jar 和 javase.jar。
2. 在你的项目中引入 zxing 类库
你需要把 core.jar 和 javase.jar 添加到你项目的 classpath 中,然后你可以调用 zxing 的 API 了。下面的例子讲述了一个通用的方法来读写各种条形码,以及如何用编码/解码暗示来增强条形码引擎。
3. 读取条形码
你首先以输入流的形式载入图形,然后调用这个工具方法。
/**
* Decode method used to read image or barcode itself, and recognize the barcode,
* get the encoded contents and returns it.
* @param file image that need to be read.
* @param config configuration used when reading the barcode.
* @return decoded results from barcode.
*/
public static String decode(File file, Map<DecodeHintType, Object> hints) throws Exception {
// check the required parameters
if (file == null || file.getName().trim().isEmpty())
throw new IllegalArgumentException("File not found, or invalid file name.");
BufferedImage image;
try {
image = read(file);
} catch (IOException ioe) {
throw new Exception(ioe.getMessage());
}
if (image == null)
throw new IllegalArgumentException("Could not decode image.");
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader barcodeReader = new MultiFormatReader();
Result result;
String finalResult;
try {
if (hints != null && ! hints.isEmpty())
result = barcodeReader.decode(bitmap, hints);
else
result = barcodeReader.decode(bitmap);
// setting results.
finalResult = String.valueOf(result.getText());
} catch (Exception e) {
e.printStackTrace();
throw new BarcodeEngine().new BarcodeEngineException(e.getMessage());
}
return finalResult;
}
4. 写入条形码
你可以把一段文本写入条形码:
/**
* Encode method, used to generate barcode with contents and
* configurations provided.
* @param file the image.
* @param contents the contents to be encoded.
* @param barcodeFormat the required barcode formate.
* @param width image width.
* @param height image height.
* @param config any required configurations.
*/
public static void encode(File file, String contents, BarcodeFormat barcodeFormat, int width,
int height, Map<EncodeHintType, Object> hints) throws Exception {
// set default values
if (barcodeFormat == null)
barcodeFormat = DEFAULT_BARCODE_FORMAT;
if (width <= 9)
width = 300;
if (height <= 9)
height = 300;
// check the required parameters
if (file == null || file.getName().trim().isEmpty())
throw new IllegalArgumentException("File not found, or invalid file name.");
if (contents == null || contents.trim().isEmpty())
throw new IllegalArgumentException("Can't encode null or empty contents.");
try {
MultiFormatWriter barcodeWriter = new MultiFormatWriter();
BitMatrix matrix;
if (config != null && ! hints.isEmpty())
matrix =
barcodeWriter.encode(contents, barcodeFormat, width, height, hints);
else
matrix = barcodeWriter.encode(contents, barcodeFormat, width, height);
String imageFormat = file.getName().substring(file.getName().indexOf(".") + 1);
writeToFile(matrix, imageFormat, file);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
5. JavaDoc 和帮助文档
Javadoc 就在你下载的 zip 文件中。你可以通过 javadoc 查看所有被支持的条形码类型。
原文链接,OSChina.NET 编译
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
请问里面的参数是什么个情况File file, Map<DecodeHintType, Object> hints
这里还有 qr code in java