废话不说,上干货!
pom.xml(maven项目的配置文件,如果你不是maven项目,请自行下载导入jar包com.google.zxing)
PS:注意包的版本 如果版本低,二维码的配置信息中,可能有的代码会报错,找不到对应类型。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.2</version>
</dependency>
生成二维码准备文件
package logoqrcode;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.EnumMap;
/**
* @ClassName EncodeImgZxing
* @Description TODO
* @Author Lucky
* @Date 2018/5/10 10:45
* @Version 1.0
*/
public class EncodeImgZxing {
//二维码颜色
private static final int BLACK = 0xFF000000;//0xFFFF0000,红色
//二维码背景色
private static final int WHITE = 0xFFFFFFFF;//0xFF0000FF,蓝色
//注:二维码颜色色差大,扫描快,但如果"BLACK'设置为黑色外其他颜色,可能无法扫描
//二维码图片宽度
private static final int width = 300;
//二维码图片高度
private static final int height = 300;
//二维码格式参数
private static final EnumMap<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
static{
/*二维码的纠错级别(排错率),4个级别:
L (7%)、
M (15%)、
Q (25%)、
H (30%)(最高H)
纠错信息同样存储在二维码中,纠错级别越高,纠错信息占用的空间越多,那么能存储的有用讯息就越少;共有四级;
选择M,扫描速度快。
*/
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 二维码边界空白大小 1,2,3,4 (4为默认,最大)
hints.put(EncodeHintType.MARGIN, 1);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MAX_SIZE, 350);
hints.put(EncodeHintType.MIN_SIZE, 150);
}
/**
* 绘制二维码
* @param contents 二维码内容
* @return image 二维码图片
* */
public static BufferedImage encodeImg(String contents){
BufferedImage image = null;
try{
BitMatrix matrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int width = matrix.getWidth();
int height = matrix.getHeight();
for(int x = 0; x < width; x++){
for(int y =0;y < height; y++){
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
}catch(Exception e){
System.out.println("生成二维码失败"+e.getMessage());
}
return image;
}
/**
* 二维码输出到文件
* @param contents 二维码内容
* @param format 图片格式
* @param file 输出文件
* */
public static void writeToFile(String contents,String format,File file){
BufferedImage image = encodeImg(contents);
try {
ImageIO.write(image, format, file);
} catch (IOException e) {
System.out.println("二维码写入文件失败"+e.getMessage());
}
}
/**
* 二维码流式输出
* @param contents 二维码内容
* @param format 图片格式
* @param stream 输出流
* */
public static void writeToStream(String contents,String format,OutputStream stream){
BufferedImage image = encodeImg(contents);
try {
ImageIO.write(image, format, stream);
} catch (IOException e) {
System.out.println("二维码写入流失败"+e.getMessage());
}
}
}
带logo的二维码生成代码
package logoqrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
/**
1. @ClassName EncodeImgZingLogo
2. @Description TODO
3. @Author Lucky
4. @Date 2018/5/10 13:23
5. @Version 1.0
*/
public class EncodeImgZingLogo {
/**
* 二维码绘制logo
* @param twodimensioncodeImg 二维码图片文件
* @param logoImg logo图片文件
* */
public static BufferedImage encodeImgLogo(File twodimensioncodeImg, File logoImg){
BufferedImage twodimensioncode = null;
try{
if(!twodimensioncodeImg.isFile() || !logoImg.isFile()){
System.out.println("输入非图片");
return null;
}
//读取二维码图片
twodimensioncode = ImageIO.read(twodimensioncodeImg);
//获取画笔
Graphics2D g = twodimensioncode.createGraphics();
//读取logo图片
BufferedImage logo = ImageIO.read(logoImg);
//设置二维码大小,太大,会覆盖二维码,此处20%
int logoWidth = logo.getWidth(null) > twodimensioncode.getWidth()*2 /10 ? (twodimensioncode.getWidth()*2 /10) : logo.getWidth(null);
int logoHeight = logo.getHeight(null) > twodimensioncode.getHeight()*2 /10 ? (twodimensioncode.getHeight()*2 /10) : logo.getHeight(null);
//设置logo图片放置位置
//中心
int x = (twodimensioncode.getWidth() - logoWidth) / 2;
int y = (twodimensioncode.getHeight() - logoHeight) / 2;
//右下角,15为调整值
// int x = twodimensioncode.getWidth() - logoWidth-15;
// int y = twodimensioncode.getHeight() - logoHeight-15;
//开始合并绘制图片
g.drawImage(logo, x, y, logoWidth, logoHeight, null);
g.drawRoundRect(x, y, logoWidth, logoHeight, 15 ,15);
//logo边框大小
g.setStroke(new BasicStroke(2));
//logo边框颜色
g.setColor(Color.WHITE);
g.drawRect(x, y, logoWidth, logoHeight);
g.dispose();
logo.flush();
twodimensioncode.flush();
}catch(Exception e){
System.out.println("二维码绘制logo失败");
}
return twodimensioncode;
}
/**
* 二维码输出到文件
* @param twodimensioncodeImg 二维码图片文件
* @param logoImg logo图片文件
* @param format 图片格式
* @param file 输出文件
* */
public static void writeToFile(File twodimensioncodeImg,File logoImg,String format,File file) throws IOException {
BufferedImage image = encodeImgLogo(twodimensioncodeImg, logoImg);
try {
ImageIO.write(image, format, file);
} catch (IOException e) {
System.out.println("二维码写入文件失败"+e.getMessage());
}
}
/**
* 二维码流式输出
* @param twodimensioncodeImg 二维码图片文件
* @param logoImg logo图片文件
* @param format 图片格式
* @param stream 输出流
* */
public static void writeToStream(File twodimensioncodeImg,File logoImg,String format,OutputStream stream){
BufferedImage image = encodeImgLogo(twodimensioncodeImg, logoImg);
try {
ImageIO.write(image, format, stream);
} catch (IOException e) {
System.out.println("二维码写入流失败"+e.getMessage());
}
}
}
二维码解析
package logoqrcode;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.EnumMap;
import javax.imageio.ImageIO;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import qrcode.BufferedImageLuminanceSource;
/**
* @ClassName DecodeImgZxing
* @Description 通过google的zxing解析二维码 注:此代码,不能解析:L纠错级别带logo和H级别的解析
* @Author Lucky
* @Date 2018/5/10 13:26
* @Version 1.0
*/
public class DecodeImgZxing {
//二维码格式参数
private static final EnumMap<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);
static{
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
}
/**
* 解析二维码,使用google的zxing
* @param imgPath 二维码路径
* @return content 二维码内容
* */
public static String decodeImg(File imgFile){
String content = null;
if(!imgFile.isFile()){
System.out.println("输入非文件");
return null;
}
try {
BufferedImage image = ImageIO.read(imgFile);
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
MultiFormatReader reader = new MultiFormatReader();
Result result = reader.decode(binaryBitmap, hints);
content = result.getText();
// System.out.println("二维码结果:"+":"+result.toString()+","+result.getBarcodeFormat()+","+result.getText());
} catch (NotFoundException e) {
System.out.println("二维码解析NotFoundException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("二维码解析IOException");
e.printStackTrace();
}
return content;
}
}
测试代码
package logoqrcode;
import java.io.File;
import java.io.IOException;
/**
* @ClassName EncodeImgZingLogoTest
* @Description TODO 测试
* @Author Lucky
* @Date 2018/5/10 13:20
* @Version 1.0
*/
public class EncodeImgZingLogoTest {
public static void main(String[] args) throws IOException {
String contents = "https://blog.csdn.net/shasiqq";
String format = "jpg"; //***此处如果格式为"gif",则logo图片为黑色,其他格式ok
//生成二维码
File img = new File("D:"+File.separator+"csdn.jpg");
EncodeImgZxing.writeToFile(contents, format, img);
// //添加logo图片
File logoImg = new File("D:"+File.separator+"logo.jpg");
File img1 = new File("D:"+File.separator+"csdnAndLogo.jpg");
EncodeImgZingLogo.writeToFile(img, logoImg, format, img1);
//解析二维码
String content = DecodeImgZxing.decodeImg(img);
System.out.println("1:"+content);
//带logo
String content1 = DecodeImgZxing.decodeImg(img1);
System.out.println("2:"+content1);
}
}
测试结果
解析结果
文件生成
logo—–>>>
不带logo的二维码—–>>>
带logo的二维码—–>>>
我爱你,
有时候我希望你的一生,
能被拍成一部漫长的电影。
我就比你晚出生一百年,
一辈子只做一件事,
独自坐在房间里对着墙上的屏幕,
用我的一生把你的一生慢慢看完。