| import com.google.zxing.*; |
| import com.google.zxing.client.j2se.BufferedImageLuminanceSource; |
| import com.google.zxing.common.BitMatrix; |
| import com.google.zxing.common.HybridBinarizer; |
| import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; |
| import org.springframework.beans.factory.annotation.Value; |
| import org.springframework.stereotype.Component; |
| |
| import javax.imageio.ImageIO; |
| import java.awt.*; |
| import java.awt.image.BufferedImage; |
| import java.io.File; |
| import java.io.IOException; |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.Random; |
| |
| @Component |
| public class QRcodeUtils { |
| |
| |
| private static final int BLACK = 0xFF000000; |
| |
| private static final int WHITE = 0xFFFFFFFF; |
| |
| private static final List<String> IMAGE_TYPE = new ArrayList<>(); |
| |
| static { |
| IMAGE_TYPE.add("jpg"); |
| IMAGE_TYPE.add("png"); |
| } |
| |
| private static String defaultPath; |
| |
| @Value("${platform.filePath}") |
| public void setDefaultPath(String defaultPath) { |
| QRcodeUtils.defaultPath = defaultPath; |
| } |
| |
| private static final String tempDir = "qrcode"; |
| |
| public static String create(String content) { |
| try { |
| String fileId = IkgStringUtils.getUUID(); |
| |
| String imageType = "jpg"; |
| |
| BufferedImage image = getBufferedImage(content, null, null); |
| |
| Random random = new Random(); |
| |
| String filePath = defaultPath + File.separator + tempDir + File.separator + fileId + ".jpg"; |
| File file = new File(filePath); |
| if (!file.exists()) { |
| file.mkdirs(); |
| } |
| ImageIO.write(image, imageType, file); |
| return fileId; |
| } catch (IOException e) { |
| e.printStackTrace(); |
| return null; |
| } } |
| |
| |
| * @param content |
| * @param path |
| * @param size |
| * @param logoPath |
| * @return |
| */ |
| public static String create(String content, String path, Integer size, String logoPath) { |
| try { |
| String fileId = IkgStringUtils.getUUID(); |
| |
| String imageType = "jpg"; |
| |
| BufferedImage image = getBufferedImage(content, size, logoPath); |
| |
| Random random = new Random(); |
| |
| File file = new File(path + File.separator + fileId + ".jpg"); |
| if (!file.exists()) { |
| file.mkdirs(); |
| } |
| ImageIO.write(image, imageType, file); |
| return fileId; |
| } catch (IOException e) { |
| e.printStackTrace(); |
| return null; |
| } } |
| |
| |
| * @param content |
| * @param size |
| * @param logoPath |
| * @return |
| */ |
| public static BufferedImage getBufferedImage(String content, Integer size, String logoPath) { |
| if (size == null || size <= 0) { |
| size = 250; |
| } BufferedImage image = null; |
| try { |
| |
| Map<EncodeHintType, Object> hints = new HashMap<>(); |
| |
| hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); |
| |
| hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); |
| hints.put(EncodeHintType.MARGIN, 1); |
| |
| MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); |
| BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, size, size, hints); |
| |
| int codeWidth = bitMatrix.getWidth(); |
| int codeHeight = bitMatrix.getHeight(); |
| |
| image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB); |
| for (int i = 0; i < codeWidth; i++) { |
| for (int j = 0; j < codeHeight; j++) { |
| |
| image.setRGB(i, j, bitMatrix.get(i, j) ? BLACK : WHITE); |
| } |
| } |
| |
| if (logoPath != null && !"".equals(logoPath)) { |
| File logoPic = new File(logoPath); |
| if (logoPic.exists()) { |
| Graphics2D g = image.createGraphics(); |
| BufferedImage logo = ImageIO.read(logoPic); |
| int widthLogo = logo.getWidth(null) > image.getWidth() * 2 / 10 ? (image.getWidth() * 2 / 10) : logo.getWidth(null); |
| int heightLogo = logo.getHeight(null) > image.getHeight() * 2 / 10 ? (image.getHeight() * 2 / 10) : logo.getHeight(null); |
| int x = (image.getWidth() - widthLogo) / 2; |
| int y = (image.getHeight() - heightLogo) / 2; |
| |
| g.drawImage(logo, x, y, widthLogo, heightLogo, null); |
| g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15); |
| |
| g.setStroke(new BasicStroke(2)); |
| |
| g.setColor(Color.WHITE); |
| g.drawRect(x, y, widthLogo, heightLogo); |
| g.dispose(); |
| logo.flush(); |
| image.flush(); |
| } |
| } |
| } catch (WriterException e) { |
| e.printStackTrace(); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } return image; |
| } |
| |
| |
| * @param qrPic |
| * @param logoPic |
| * @param path |
| * @return |
| */ |
| public static boolean create(File qrPic, File logoPic, String path) { |
| try { |
| String imageType = path.substring(path.lastIndexOf(".") + 1).toLowerCase(); |
| if (!IMAGE_TYPE.contains(imageType)) { |
| return false; |
| } |
| if (!qrPic.isFile() && !logoPic.isFile()) { |
| return false; |
| } |
| |
| BufferedImage image = ImageIO.read(qrPic); |
| Graphics2D g = image.createGraphics(); |
| |
| BufferedImage logo = ImageIO.read(logoPic); |
| |
| int widthLogo = logo.getWidth(null) > image.getWidth() * 2 / 10 ? (image.getWidth() * 2 / 10) : logo.getWidth(null); |
| int heightLogo = logo.getHeight(null) > image.getHeight() * 2 / 10 ? (image.getHeight() * 2 / 10) : logo.getHeight(null); |
| |
| int x = (image.getWidth() - widthLogo) / 2; |
| int y = (image.getHeight() - heightLogo) / 2; |
| |
| g.drawImage(logo, x, y, widthLogo, heightLogo, null); |
| g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15); |
| |
| g.setStroke(new BasicStroke(2)); |
| |
| g.setColor(Color.WHITE); |
| g.drawRect(x, y, widthLogo, heightLogo); |
| g.dispose(); |
| logo.flush(); |
| image.flush(); |
| File newFile = new File(path); |
| if (!newFile.exists()) { |
| newFile.mkdirs(); |
| } |
| ImageIO.write(image, imageType, newFile); |
| return true; |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return false; |
| } } |
| |
| |
| * @param path |
| * @return |
| */ |
| public static Result analyze(String path) { |
| try { |
| MultiFormatReader formatReader = new MultiFormatReader(); |
| File file = new File(path); |
| if (file.exists()) { |
| BufferedImage image = ImageIO.read(file); |
| LuminanceSource source = new BufferedImageLuminanceSource(image); |
| Binarizer binarizer = new HybridBinarizer(source); |
| BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); |
| Map hints = new HashMap(); |
| hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); |
| Result result = formatReader.decode(binaryBitmap, hints); |
| return result; |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } catch (NotFoundException e) { |
| e.printStackTrace(); |
| } return null; |
| } |
| |
| public static String getQrcode(String id) { |
| return defaultPath + File.separator + tempDir + File.separator + id + ".jpg"; |
| } |
| } |