Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. import com.google.zxing.BarcodeFormat;
  2. import com.google.zxing.EncodeHintType;
  3. import com.google.zxing.WriterException;
  4. import com.google.zxing.client.j2se.MatrixToImageConfig;
  5. import com.google.zxing.client.j2se.MatrixToImageWriter;
  6. import com.google.zxing.common.BitMatrix;
  7. import com.google.zxing.qrcode.QRCodeWriter;
  8. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  9.  
  10. import javax.imageio.ImageIO;
  11. import java.awt.*;
  12. import java.awt.image.BufferedImage;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.net.URL;
  18. import java.nio.file.*;
  19. import java.util.Comparator;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.Random;
  23.  
  24. public class QrCode {
  25.  
  26. private final String DIR = "/qrcode/";
  27. private final String ext = ".png";
  28. private final String LOGO = "http://pngimg.com/uploads/google/google_PNG19644.png";
  29. private final String CONTENT = "some content here";
  30. private final int WIDTH = 500;
  31. private final int HEIGHT = 500;
  32.  
  33. public void generate() {
  34. // Create new configuration that specifies the error correction
  35. Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
  36. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  37.  
  38. QRCodeWriter writer = new QRCodeWriter();
  39. BitMatrix bitMatrix = null;
  40. ByteArrayOutputStream os = new ByteArrayOutputStream();
  41.  
  42. try {
  43. // init directory
  44. cleanDirectory(DIR);
  45. initDirectory(DIR);
  46. // Create a qr code with the url as content and a size of WxH px
  47. bitMatrix = writer.encode(CONTENT, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
  48.  
  49. // Load QR image
  50. BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix, getMatrixConfig());
  51.  
  52. // Load logo image
  53. BufferedImage overly = getOverly(LOGO);
  54. // System.out.println(overly);
  55.  
  56. // Calculate the delta height and width between QR code and logo
  57. int deltaHeight = qrImage.getHeight() - overly.getHeight();
  58. int deltaWidth = qrImage.getWidth() - overly.getWidth();
  59.  
  60. // Initialize combined image
  61. BufferedImage combined = new BufferedImage(qrImage.getHeight(), qrImage.getWidth(), BufferedImage.TYPE_INT_ARGB);
  62. Graphics2D g = (Graphics2D) combined.getGraphics();
  63.  
  64. // Write QR code to new image at position 0/0
  65. g.drawImage(qrImage, 0, 0, null);
  66. g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
  67.  
  68. // Write logo into combine image at position (deltaWidth / 2) and
  69. // (deltaHeight / 2). Background: Left/Right and Top/Bottom must be
  70. // the same space for the logo to be centered
  71. g.drawImage(overly, (int) Math.round(deltaWidth / 2), (int) Math.round(deltaHeight / 2), null);
  72.  
  73. // Write combined image as PNG to OutputStream
  74. ImageIO.write(combined, "png", os);
  75. // Store Image
  76. // Files.copy(new ByteArrayInputStream(os.toByteArray()), Paths.get(DIR + generateRandoTitle(new Random(), 9) + ext), StandardCopyOption.REPLACE_EXISTING);
  77. System.out.println("Halo");
  78. Files.copy(new ByteArrayInputStream(os.toByteArray()), Paths.get("qr.png"), StandardCopyOption.REPLACE_EXISTING);
  79.  
  80. } catch (WriterException e) {
  81. e.printStackTrace();
  82. //LOG.error("WriterException occured", e);
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. //LOG.error("IOException occured", e);
  86. }
  87. }
  88.  
  89. private BufferedImage getOverly(String LOGO) throws IOException {
  90. URL url = new URL(LOGO);
  91. return resize(ImageIO.read(url), 100, 100);
  92. }
  93.  
  94. private void initDirectory(String DIR) throws IOException {
  95. Files.createDirectories(Paths.get(DIR));
  96. }
  97.  
  98. private void cleanDirectory(String DIR) {
  99. try {
  100. Files.walk(Paths.get(DIR), FileVisitOption.FOLLOW_LINKS)
  101. .sorted(Comparator.reverseOrder())
  102. .map(Path::toFile)
  103. .forEach(File::delete);
  104. } catch (IOException e) {
  105. // Directory does not exist, Do nothing
  106. }
  107. }
  108.  
  109. private MatrixToImageConfig getMatrixConfig() {
  110. // ARGB Colors
  111. // Check Colors ENUM
  112. return new MatrixToImageConfig(QrCode.Colors.RED.getArgb(), QrCode.Colors.WHITE.getArgb());
  113. }
  114.  
  115. private String generateRandoTitle(Random random, int length) {
  116. return random.ints(48, 122)
  117. .filter(i -> (i < 57 || i > 65) && (i < 90 || i > 97))
  118. .mapToObj(i -> (char) i)
  119. .limit(length)
  120. .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
  121. .toString();
  122. }
  123.  
  124. public enum Colors {
  125.  
  126. BLUE(0xFF40BAD0),
  127. RED(0xFFE91C43),
  128. PURPLE(0xFF8A4F9E),
  129. ORANGE(0xFFF4B13D),
  130. WHITE(0xFFFFFFFF),
  131. BLACK(0xFF000000);
  132.  
  133. private final int argb;
  134.  
  135. Colors(final int argb) {
  136. this.argb = argb;
  137. }
  138.  
  139. public int getArgb() {
  140. return argb;
  141. }
  142. }
  143.  
  144. public static BufferedImage resize(BufferedImage img, int newW, int newH) {
  145. Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
  146. BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
  147.  
  148. Graphics2D g2d = dimg.createGraphics();
  149. g2d.drawImage(tmp, 0, 0, null);
  150. g2d.dispose();
  151.  
  152. return dimg;
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement