Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.WriterException;
- import com.google.zxing.client.j2se.MatrixToImageConfig;
- import com.google.zxing.client.j2se.MatrixToImageWriter;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.qrcode.QRCodeWriter;
- import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.net.URL;
- import java.nio.file.*;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- public class QrCode {
- private final String DIR = "/qrcode/";
- private final String ext = ".png";
- private final String LOGO = "http://pngimg.com/uploads/google/google_PNG19644.png";
- private final String CONTENT = "some content here";
- private final int WIDTH = 500;
- private final int HEIGHT = 500;
- public void generate() {
- // Create new configuration that specifies the error correction
- Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>();
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
- QRCodeWriter writer = new QRCodeWriter();
- BitMatrix bitMatrix = null;
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- try {
- // init directory
- cleanDirectory(DIR);
- initDirectory(DIR);
- // Create a qr code with the url as content and a size of WxH px
- bitMatrix = writer.encode(CONTENT, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
- // Load QR image
- BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix, getMatrixConfig());
- // Load logo image
- BufferedImage overly = getOverly(LOGO);
- // System.out.println(overly);
- // Calculate the delta height and width between QR code and logo
- int deltaHeight = qrImage.getHeight() - overly.getHeight();
- int deltaWidth = qrImage.getWidth() - overly.getWidth();
- // Initialize combined image
- BufferedImage combined = new BufferedImage(qrImage.getHeight(), qrImage.getWidth(), BufferedImage.TYPE_INT_ARGB);
- Graphics2D g = (Graphics2D) combined.getGraphics();
- // Write QR code to new image at position 0/0
- g.drawImage(qrImage, 0, 0, null);
- g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
- // Write logo into combine image at position (deltaWidth / 2) and
- // (deltaHeight / 2). Background: Left/Right and Top/Bottom must be
- // the same space for the logo to be centered
- g.drawImage(overly, (int) Math.round(deltaWidth / 2), (int) Math.round(deltaHeight / 2), null);
- // Write combined image as PNG to OutputStream
- ImageIO.write(combined, "png", os);
- // Store Image
- // Files.copy(new ByteArrayInputStream(os.toByteArray()), Paths.get(DIR + generateRandoTitle(new Random(), 9) + ext), StandardCopyOption.REPLACE_EXISTING);
- System.out.println("Halo");
- Files.copy(new ByteArrayInputStream(os.toByteArray()), Paths.get("qr.png"), StandardCopyOption.REPLACE_EXISTING);
- } catch (WriterException e) {
- e.printStackTrace();
- //LOG.error("WriterException occured", e);
- } catch (IOException e) {
- e.printStackTrace();
- //LOG.error("IOException occured", e);
- }
- }
- private BufferedImage getOverly(String LOGO) throws IOException {
- URL url = new URL(LOGO);
- return resize(ImageIO.read(url), 100, 100);
- }
- private void initDirectory(String DIR) throws IOException {
- Files.createDirectories(Paths.get(DIR));
- }
- private void cleanDirectory(String DIR) {
- try {
- Files.walk(Paths.get(DIR), FileVisitOption.FOLLOW_LINKS)
- .sorted(Comparator.reverseOrder())
- .map(Path::toFile)
- .forEach(File::delete);
- } catch (IOException e) {
- // Directory does not exist, Do nothing
- }
- }
- private MatrixToImageConfig getMatrixConfig() {
- // ARGB Colors
- // Check Colors ENUM
- return new MatrixToImageConfig(QrCode.Colors.RED.getArgb(), QrCode.Colors.WHITE.getArgb());
- }
- private String generateRandoTitle(Random random, int length) {
- return random.ints(48, 122)
- .filter(i -> (i < 57 || i > 65) && (i < 90 || i > 97))
- .mapToObj(i -> (char) i)
- .limit(length)
- .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
- .toString();
- }
- public enum Colors {
- BLUE(0xFF40BAD0),
- RED(0xFFE91C43),
- PURPLE(0xFF8A4F9E),
- ORANGE(0xFFF4B13D),
- WHITE(0xFFFFFFFF),
- BLACK(0xFF000000);
- private final int argb;
- Colors(final int argb) {
- this.argb = argb;
- }
- public int getArgb() {
- return argb;
- }
- }
- public static BufferedImage resize(BufferedImage img, int newW, int newH) {
- Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
- BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
- Graphics2D g2d = dimg.createGraphics();
- g2d.drawImage(tmp, 0, 0, null);
- g2d.dispose();
- return dimg;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement