二维码识别号码:h162450105

备忘录(19)
自己折腾了好久,百度、google了半天,发现java生成二维码的文章倒是很多,但是调用二维码打印的就很少了,好多打印出来要么是二维码占用好几张纸,要么就是多出4、5张白纸,浪费了我不少的条码纸,最终还是搞定了,在这里将代码记录下来,也方便大家,希望能对大家有帮助。
首先是工具类BufferedImageLuminanceSource
package com.lc.erwei.
import java.awt.Graphics2D;
import java.awt.geom.AffineT
import java.awt.image.BufferedI
import com.google.zxing.LuminanceS
public class BufferedImageLuminanceSource extends LuminanceSource {
private final BufferedI
public BufferedImageLuminanceSource(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight());
public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
super(width, height);
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width & sourceWidth || top + height & sourceHeight) {
throw new IllegalArgumentException(&Crop rectangle does not fit within image data.&);
for (int y = y & top + y++) {
for (int x = x & left + x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left =
this.top =
public byte[] getRow(int y, byte[] row) {
if (y & 0 || y &= getHeight()) {
throw new IllegalArgumentException(&Requested row is outside the image: & + y);
int width = getWidth();
if (row == null || row.length & width) {
row = new byte[width];
image.getRaster().getDataElements(left, top + y, width, 1, row);
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width *
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
public boolean isCropSupported() {
public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
public boolean isRotateSupported() {
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
然后是生成二维码和打印的类
import java.awt.BasicS
import java.awt.G
import java.awt.Graphics2D;
import java.awt.I
import java.awt.S
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedI
import java.io.F
import java.io.FileInputS
import java.io.IOE
import java.io.OutputS
import java.util.H
import java.util.R
import javax.imageio.ImageIO;
import javax.print.D
import javax.print.DocF
import javax.print.DocPrintJ
import javax.print.PrintE
import javax.print.PrintS
import javax.print.PrintServiceL
import javax.print.SimpleD
import javax.print.attribute.DocAttributeS
import javax.print.attribute.HashDocAttributeS
import javax.print.attribute.HashPrintRequestAttributeS
import javax.print.attribute.PrintRequestAttributeS
import javax.print.attribute.standard.C
import javax.print.attribute.standard.MediaPrintableA
import javax.print.attribute.standard.MediaSizeN
import com.google.zxing.BarcodeF
import com.google.zxing.BinaryB
import com.google.zxing.DecodeHintT
import com.google.zxing.EncodeHintT
import com.google.zxing.MultiFormatR
import com.google.zxing.MultiFormatW
import com.google.zxing.R
import com.mon.BitM
import com.mon.HybridB
import com.google.zxing.qrcode.decoder.ErrorCorrectionL
public class QRCodeUtil {
private static final String CHARSET = &utf-8&;
private static final String FORMAT_NAME = &JPG&;
// 二维码尺寸
private static final int QRCODE_SIZE = 300;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
Hashtable&EncodeHintType, Object& hints = new Hashtable&EncodeHintType, Object&();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x & x++) {
for (int y = 0; y & y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
if (imgPath == null || &&.equals(imgPath)) {
// 插入图片
QRCodeUtil.insertImage(image, imgPath, needCompress);
private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println(&& + imgPath + & 二维logo不存在!&);
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width & WIDTH) {
width = WIDTH;
if (height & HEIGHT) {
height = HEIGHT;
Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
mkdirs(destPath);
// 二维码图片
String file = new Random().nextInt() + &.jpg&;
ImageIO.write(image, FORMAT_NAME, new File(destPath + &/& + file));
public static void mkdirs(String destPath) {
File file = new File(destPath);
// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
public static void encode(String content, String imgPath, String destPath) throws Exception {
QRCodeUtil.encode(content, imgPath, destPath, false);
public static void encode(String content, String destPath, boolean needCompress) throws Exception {
QRCodeUtil.encode(content, null, destPath, needCompress);
public static void encode(String content, String destPath) throws Exception {
QRCodeUtil.encode(content, null, destPath, false);
public static void encode(String content, String imgPath, OutputStream output, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
ImageIO.write(image, FORMAT_NAME, output);
public static void encode(String content, OutputStream output) throws Exception {
QRCodeUtil.encode(content, null, output, false);
public static String decode(File file) throws Exception {
image = ImageIO.read(file);
if (image == null) {
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable&DecodeHintType, String& hints = new Hashtable&DecodeHintType, String&();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultS
public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path));
public static void main(String[] args) throws Exception {
String text = &&;
// 生成带logo的二维码
QRCodeUtil.encode(text, &d:/180.jpg&, &d:/MyWorkDoc&, true);
// 生成不带二维码的logo图片
QRCodeUtil.encode(text, null, &d:/MyWorkDoc&, true);
drawImage(&D:/MyWorkDoc/.jpg&, 1);
* 打印二维码
* @param fileName
* @param count
public static void drawImage(String fileName, int count) {
DocFlavor dof =
if (fileName.endsWith(&.gif&)) {
dof = DocFlavor.INPUT_STREAM.GIF;
} else if (fileName.endsWith(&.jpg&)) {
dof = DocFlavor.INPUT_STREAM.JPEG;
} else if (fileName.endsWith(&.png&)) {
dof = DocFlavor.INPUT_STREAM.PNG;
// 获取默认打印机
PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(OrientationRequested.PORTRAIT);
pras.add(PrintQuality.HIGH);
pras.add(new Copies(count));
pras.add(MediaSizeName.ISO_A10); // 设置打印的纸张
DocAttributeSet das = new HashDocAttributeSet();
das.add(new MediaPrintableArea(0, 0, 1, 1, MediaPrintableArea.INCH));
FileInputStream fin = new FileInputStream(fileName);
Doc doc = new SimpleDoc(fin, dof, das);
DocPrintJob job = ps.createPrintJob();
job.print(doc, pras);
fin.close();
} catch (IOException ie) {
ie.printStackTrace();
} catch (PrintException pe) {
pe.printStackTrace();
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:19053次
排名:千里之外
原创:14篇
(1)(1)(1)(3)(2)(1)(1)(1)(1)(4)(2)(2)(3)“娱乐”中共找到约有2173条符合长按指纹识别二维码的记录,耗时0.007秒
栏目:不限时间:不限排序:不限
-> -> -> 长按指纹识别二维码
浏览:85 | 下载: | 评论:0浏览:340 | 下载: | 评论:0浏览:272 | 下载: | 评论:0浏览:61 | 下载: | 评论:0浏览:242 | 下载: | 评论:0浏览:176 | 下载: | 评论:0浏览:262 | 下载: | 评论:0浏览:300 | 下载: | 评论:0浏览:226 | 下载: | 评论:0浏览:423 | 下载: | 评论:0浏览:191 | 下载: | 评论:0浏览:362 | 下载: | 评论:0浏览:292 | 下载: | 评论:0浏览:196 | 下载: | 评论:0浏览:268 | 下载: | 评论:0浏览:435 | 下载: | 评论:0浏览:67 | 下载: | 评论:0浏览:391 | 下载: | 评论:0浏览:500 | 下载: | 评论:0浏览:301 | 下载: | 评论:0浏览:180 | 下载: | 评论:0浏览:341 | 下载: | 评论:0浏览:341 | 下载: | 评论:0浏览:215 | 下载: | 评论:0QRcodeDe r 是二维码识别源码,里面加入了图像的预处理,可以实现 的正确 。 Graph Recognize 图形/文字
240万源代码下载-
&文件名称: QRcodeDecoder& & [
& & & & &&]
&&所属分类:
&&开发工具: Visual C++
&&文件大小: 4129 KB
&&上传时间:
&&下载次数: 431
&&提 供 者:
&详细说明:是二维码识别源码,里面加入了图像的预处理,可以实现二维码的正确识别。-Two-dimensional code to identify source, I added the image preprocessing, can achieve the correct identification of the two-dimensional code.
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&QRcodeDecoder新\AlignmentPattern.cpp&&...............\AlignmentPattern.h&&...............\Axis.cpp&&...............\Axis.h&&...............\ContentDecoder.cpp&&...............\ContentDecoder.h&&...............\Debug\AlignmentPattern.obj&&...............\.....\AlignmentPattern.sbr&&...............\.....\Axis.obj&&...............\.....\Axis.sbr&&...............\.....\ContentDecoder.obj&&...............\.....\ContentDecoder.sbr&&...............\.....\FinderPattern.obj&&...............\.....\FinderPattern.sbr&&...............\.....\Galois.obj&&...............\.....\Galois.sbr&&...............\.....\Line.obj&&...............\.....\Line.sbr&&...............\.....\Point.obj&&...............\.....\Point.sbr&&...............\.....\QRcodeDecoder.bsc&&...............\.....\QRcodeDecoder.exe&&...............\.....\QRcodeDecoder.ilk&&...............\.....\QRcodeDecoder.obj&&...............\.....\QRcodeDecoder.pch&&...............\.....\QRcodeDecoder.pdb&&...............\.....\QRcodeDecoder.res&&...............\.....\QRcodeDecoder.sbr&&...............\.....\QRcodeDecoderDlg.obj&&...............\.....\QRcodeDecoderDlg.sbr&&...............\.....\QRcodeImage.obj&&...............\.....\QRcodeImage.sbr&&...............\.....\RsDecode.obj&&...............\.....\RsDecode.sbr&&...............\.....\SamplingGrid.obj&&...............\.....\SamplingGrid.sbr&&...............\.....\StdAfx.obj&&...............\.....\StdAfx.sbr&&...............\.....\vc60.idb&&...............\.....\vc60.pdb&&...............\Demo2.bmp&&...............\Demo3.bmp&&...............\FinderPattern.cpp&&...............\FinderPattern.h&&...............\Galois.cpp&&...............\Galois.h&&...............\Line.cpp&&...............\Line.h&&...............\Point.cpp&&...............\Point.h&&...............\QRcodeDecoder.aps&&...............\QRcodeDecoder.clw&&...............\QRcodeDecoder.cpp&&...............\QRcodeDecoder.dsp&&...............\QRcodeDecoder.dsw&&...............\QRcodeDecoder.h&&...............\QRcodeDecoder.ncb&&...............\QRcodeDecoder.opt&&...............\QRcodeDecoder.plg&&...............\QRcodeDecoder.rc&&...............\QRcodeDecoderDlg.cpp&&...............\QRcodeDecoderDlg.h&&...............\QRcodeImage.cpp&&...............\QRcodeImage.h&&...............\QR_1.bmp&&...............\ReadMe.txt&&...............\res\QRcodeDecoder.ico&&...............\...\QRcodeDecoder.rc2&&...............\resource.h&&...............\RsDecode.cpp&&...............\RsDecode.h&&...............\SamplingGrid.cpp&&...............\SamplingGrid.h&&...............\StdAfx.cpp&&...............\StdAfx.h&&...............\test1.bmp&&...............\未命名.bmp&&...............\Debug&&...............\res&&QRcodeDecoder新
&[]:文件不全&[]:纯粹是垃圾&[]:纯粹是垃圾&[]:纯粹是垃圾&[]:很好,推荐下载&[]:一般,勉强可用&[]:很好,推荐下载
&近期下载过的用户:
&&&&&&&&&&&&&&&&&&&&&[]
&相关搜索:
&输入关键字,在本站240万海量源码库中尽情搜索:
&[] - VC++写的QR+Code二维码的解码器
&[] - c++编写的完整人脸识别系统!具体操作的步骤菜单显示
&[] - QR二维码,程序出自日本,方便使用,准确率高
&[] - 快速响应码的识别和解码
花钱搞到的论文
&[] - c++ mfc 二维码 编码 解码 画图
c++ mfc 二维码 编码 解码 画图
是自己写的,做了大量的处理,无内存泄露
可以根据输入的内容生成二维码,并且将图片显示。
可以根据二维码的图片解码。
如果有问题可以留言。
&[] - 二维码解码程序,用VC++开发。输入二维码图案,可以识别出其内容。阅读 84 °
阅读 88 °
阅读 99 °
阅读 87 °2253人阅读
& & & & &闲来无事,查阅oschina发现有人写了一个关于Qt下识别二维码的例子,兴趣一起,进去研究了下,将其源码下载下来,发现快捷键等源码在我机器无法使用,关掉企鹅后还是不能正常使用,就研究了下二维码的识别部分,其中用到了QZXing库,里面对识别只有一个方法QString decodeImage(QImage image);
& & & & 试验了一下,确实可以识别二维码,特此记录下
&分享一个开源链接&/dplanella/qzxing
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:118312次
积分:1999
积分:1999
排名:第13091名
原创:68篇
转载:26篇
评论:77条
(1)(2)(2)(1)(1)(3)(1)(11)(4)(4)(7)(5)(1)(1)(3)(4)(9)(5)(8)(6)(6)(5)(4)(2)(1)

我要回帖

 

随机推荐