1、项目中导入code-3.3.0.jar以及javase-3.3.0.jar两个包
2、二维码生成
//生成二维码图片
public static void createQrImg(File outputFile, String content, String format, Integer height, Integer width)
throws WriterException, FileNotFoundException, IOException
{
if (outputFile == null)
throw new WriterException("Parameter: ouputFile is null.");
if (content == null)
throw new WriterException("Parameter: content is null.");
// jpg、jpeg、png、gif、bmp、tiff、ai、cdr、eps
List<String> formats = Arrays.asList("jpg", "jpeg", "png", "gif", "bmp", "tiff");
//默认使用jpg格式
if (format == null)
format = "jpg";
format = format.trim();
if (!formats.contains(format.toLowerCase()))
throw new WriterException("Parameter: format is not in " + formats.toString() + ".");
if (outputFile.getParentFile() != null && !outputFile.getParentFile().exists())
outputFile.getParentFile().mkdirs();
if (height == null || height <= 0)
height = 180;
if (width == null || width <= 0)
width = 180;
Map<EncodeHintType, Object> hints = new LinkedHashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//定义纠错等级为M
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
bitMatrix = deleteWhite(bitMatrix);
MatrixToImageWriter.writeToStream(bitMatrix, format, new FileOutputStream(outputFile));
addLogo(outputFile,format);
}
//在二维码中间添加logo
private static void addLogo(File outputFile, String format)
{
Graphics g = null;
BufferedImage combinedImg = null;
try
{
String path = "xxx/logo.png";
File file = new File(path);
file.createNewFile();
Image logo = ImageIO.read(file);
BufferedImage outputImg = ImageIO.read(outputFile);
combinedImg = new BufferedImage(outputImg.getWidth(), outputImg.getHeight(),
BufferedImage.TYPE_INT_RGB);
g = combinedImg.getGraphics();
g.drawImage(outputImg, 0, 0, null);
//图片放二维码中间
g.drawImage(logo, (outputImg.getWidth() - logo.getWidth(null)) / 2,
(outputImg.getHeight() - logo.getHeight(null)) / 2, null);
ImageIO.write(combinedImg, format, outputFile);
}
catch (Throwable e)
{
System.out.println("Error in add QRCode image!");
}
finally
{
if (g != null)
{
g.dispose();
}
}
}
//去掉生成的二维码四周的白色边框
private static BitMatrix deleteWhite(BitMatrix matrix)
{
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++)
{
for (int j = 0; j < resHeight; j++)
{
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i, j);
}
}
return resMatrix;
}
3、二维码解析
//截取屏幕中间的二维码并将其解析
public static String getQrCodeInfo(){
try
{
//截图,截取屏幕中间四分之一的位置
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
//copy the screen to a BufferedImage , only get the one fourth screenshot
BufferedImage screenshot=(new Robot()).createScreenCapture(
new Rectangle((int)d.getWidth()/4,(int)d.getHeight()/8,(int)(2*d.getWidth()/4),(int)(5*d.getHeight()/8)));
//将截图的照片放到指定位置
//String userName = System.getenv("username");
//ImageIO.write(screenshot, "png", new File("C:\\Users\\" + userName + "\\a.png"));
//开始解析二维码,若只是解析某个目录下的二维码,则去掉上面代码然后将screenshot换成对应位置图片信息即可
LuminanceSource source = new BufferedImageLuminanceSource(screenshot);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(binaryBitmap, hints);
return result.getText();
}
catch (Exception e)
{
System.out.println("analysis the qrcode has error , the error msg is " + e.getMessage());
}
return null;
}