| | |
| | | import com.google.zxing.Result; |
| | | import com.google.zxing.WriterException; |
| | | import com.google.zxing.client.j2se.BufferedImageLuminanceSource; |
| | | import com.google.zxing.client.j2se.MatrixToImageWriter; |
| | | import com.google.zxing.common.BitMatrix; |
| | | import com.google.zxing.common.HybridBinarizer; |
| | | import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; |
| | |
| | | * 二维码生成 |
| | | * |
| | | * @author Administrator |
| | | * |
| | | */ |
| | | public class QRCodeUtil { |
| | | private static final String CHARSET = "UTF-8"; |
| | | private static final String FORMAT_NAME = "JPG"; |
| | | |
| | | // 二维码尺寸 |
| | | int QRCODE_SIZE = 200; |
| | | // LOGO宽度 |
| | |
| | | /** |
| | | * user: Rex date: 2016年12月29日 上午12:31:29 |
| | | * |
| | | * @param content |
| | | * 二维码内容 |
| | | * @param logoImgPath |
| | | * Logo |
| | | * @param needCompress |
| | | * 是否压缩Logo |
| | | * @param content 二维码内容 |
| | | * @param logoImgPath Logo |
| | | * @param needCompress 是否压缩Logo |
| | | * @return 返回二维码图片 |
| | | * @throws WriterException |
| | | * @throws IOException |
| | | * BufferedImage TODO 创建二维码图片 |
| | | */ |
| | | private BufferedImage createImage(String content, InputStream logoImg, boolean needCompress) |
| | | throws WriterException, IOException { |
| | | return this.createImage(content, logoImg, 0xFF000000, 0xFFFFFFFF, needCompress); |
| | | } |
| | | |
| | | |
| | | public BufferedImage createImage(String content, InputStream logoImg, int color, int bgColor, boolean needCompress) |
| | | throws WriterException, IOException { |
| | | Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); |
| | | hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); |
| | |
| | | BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
| | | for (int x = 0; x < width; x++) { |
| | | for (int y = 0; y < height; y++) { |
| | | image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); |
| | | image.setRGB(x, y, bitMatrix.get(x, y) ? color : bgColor); |
| | | } |
| | | } |
| | | if (logoImg == null) { |
| | | return image; |
| | | } |
| | | |
| | | if (logoImg != null) { |
| | | // 插入图片 |
| | | insertImage(image, logoImg, needCompress); |
| | | } |
| | | |
| | | return image; |
| | | } |
| | | |
| | | /** |
| | | * user: Rex date: 2016年12月29日 上午12:31:29 |
| | | * |
| | | * @param content |
| | | * 二维码内容 |
| | | * @param logoImgPath |
| | | * Logo |
| | | * @param needCompress |
| | | * 是否压缩Logo |
| | | * @param content 二维码内容 |
| | | * @param logoImgPath Logo |
| | | * @param needCompress 是否压缩Logo |
| | | * @return 返回二维码图片 |
| | | * @throws WriterException |
| | | * @throws IOException |
| | | * BufferedImage TODO 创建二维码图片 |
| | | */ |
| | | private BufferedImage createImageDeleteWhite(String content, InputStream logoImg, boolean needCompress) |
| | | throws WriterException, IOException { |
| | | return createImageDeleteWhite(content, logoImg, 0xFF000000, 0xFFFFFFFF, needCompress); |
| | | } |
| | | |
| | | |
| | | public BufferedImage createImageDeleteWhite(String content, InputStream logoImg, int color, int bgColor, boolean needCompress) |
| | | throws WriterException, IOException { |
| | | Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); |
| | | hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); |
| | |
| | | BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
| | | for (int x = 0; x < width; x++) { |
| | | for (int y = 0; y < height; y++) { |
| | | image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); |
| | | image.setRGB(x, y, bitMatrix.get(x, y) ? color : bgColor); |
| | | } |
| | | } |
| | | if (logoImg == null) { |
| | |
| | | } |
| | | |
| | | // 插入图片 |
| | | if (logoImg != null) { |
| | | insertImage(image, logoImg, needCompress); |
| | | } |
| | | return image; |
| | | } |
| | | |
| | | /** |
| | | * user: Rex date: 2016年12月29日 上午12:30:09 |
| | | * |
| | | * @param source |
| | | * 二维码图片 |
| | | * @param logoImgPath |
| | | * Logo |
| | | * @param needCompress |
| | | * 是否压缩Logo |
| | | * @throws IOException |
| | | * void TODO 添加Logo |
| | | * @param source 二维码图片 |
| | | * @param logoImgPath Logo |
| | | * @param needCompress 是否压缩Logo |
| | | * @throws IOException void TODO 添加Logo |
| | | */ |
| | | private void insertImage(BufferedImage source, InputStream logoImg, boolean needCompress) throws IOException { |
| | | Image src = ImageIO.read(logoImg); |
| | |
| | | /** |
| | | * user: Rex date: 2016年12月29日 上午12:32:32 |
| | | * |
| | | * @param content |
| | | * 二维码内容 |
| | | * @param logoImgPath |
| | | * Logo |
| | | * @param destPath |
| | | * 二维码输出路径 |
| | | * @param needCompress |
| | | * 是否压缩Logo |
| | | * @throws Exception |
| | | * void TODO 生成带Logo的二维码 |
| | | * @param content 二维码内容 |
| | | * @param logoImgPath Logo |
| | | * @param destPath 二维码输出路径 |
| | | * @param needCompress 是否压缩Logo |
| | | * @throws Exception void TODO 生成带Logo的二维码 |
| | | */ |
| | | public void encode(String content, InputStream logoImg, String destPath, boolean needCompress) throws Exception { |
| | | BufferedImage image = createImage(content, logoImg, needCompress); |
| | |
| | | /** |
| | | * user: Rex date: 2016年12月29日 上午12:35:44 |
| | | * |
| | | * @param content |
| | | * 二维码内容 |
| | | * @param destPath |
| | | * 二维码输出路径 |
| | | * @throws Exception |
| | | * void TODO 生成不带Logo的二维码 |
| | | * @param content 二维码内容 |
| | | * @param destPath 二维码输出路径 |
| | | * @throws Exception void TODO 生成不带Logo的二维码 |
| | | */ |
| | | public void encode(String content, String destPath) throws Exception { |
| | | encode(content, null, destPath, false); |
| | |
| | | /** |
| | | * user: Rex date: 2016年12月29日 上午12:36:58 |
| | | * |
| | | * @param content |
| | | * 二维码内容 |
| | | * @param logoImgPath |
| | | * Logo |
| | | * @param output |
| | | * 输出流 |
| | | * @param needCompress |
| | | * 是否压缩Logo |
| | | * @throws Exception |
| | | * void TODO 生成带Logo的二维码,并输出到指定的输出流 |
| | | * @param content 二维码内容 |
| | | * @param logoImgPath Logo |
| | | * @param output 输出流 |
| | | * @param needCompress 是否压缩Logo |
| | | * @throws Exception void TODO 生成带Logo的二维码,并输出到指定的输出流 |
| | | */ |
| | | public void encode(String content, InputStream logoImg, OutputStream output, boolean needCompress) |
| | | throws Exception { |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param content |
| | | * @param logoImg |
| | | * @param output |
| | | * 二进制输出流 |
| | | * @param output 二进制输出流 |
| | | * @param needCompress |
| | | * @throws Exception |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param content |
| | | * @param logoImg |
| | | * @param output |
| | | * 二进制输出流 |
| | | * @param output 二进制输出流 |
| | | * @param needCompress |
| | | * @throws Exception |
| | | */ |
| | |
| | | /** |
| | | * user: Rex date: 2016年12月29日 上午12:38:02 |
| | | * |
| | | * @param content |
| | | * 二维码内容 |
| | | * @param output |
| | | * 输出流 |
| | | * @throws Exception |
| | | * void TODO 生成不带Logo的二维码,并输出到指定的输出流 |
| | | * @param content 二维码内容 |
| | | * @param output 输出流 |
| | | * @throws Exception void TODO 生成不带Logo的二维码,并输出到指定的输出流 |
| | | */ |
| | | public void encode(String content, OutputStream output) throws Exception { |
| | | encode(content, null, output, false); |
| | |
| | | /** |
| | | * user: Rex date: 2016年12月29日 上午12:39:10 |
| | | * |
| | | * @param file |
| | | * 二维码 |
| | | * @param file 二维码 |
| | | * @return 返回解析得到的二维码内容 |
| | | * @throws Exception |
| | | * String TODO 二维码解析 |
| | | * @throws Exception String TODO 二维码解析 |
| | | */ |
| | | public String decode(File file) throws Exception { |
| | | BufferedImage image; |
| | |
| | | /** |
| | | * user: Rex date: 2016年12月29日 上午12:39:48 |
| | | * |
| | | * @param path |
| | | * 二维码存储位置 |
| | | * @param path 二维码存储位置 |
| | | * @return 返回解析得到的二维码内容 |
| | | * @throws Exception |
| | | * String TODO 二维码解析 |
| | | * @throws Exception String TODO 二维码解析 |
| | | */ |
| | | public String decode(String path) throws Exception { |
| | | return decode(new File(path)); |