Advertisement
MX37S

add qr pdf

Nov 11th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1.     private File urlToImg(String urlImg) throws Exception {
  2.         URL url=new URL(urlImg);
  3.         URLConnection connection = null;
  4.  
  5.         connection = url.openConnection();
  6.            
  7.         InputStream in = null;
  8.         in = connection.getInputStream();
  9.        
  10.         FileOutputStream fos = null;
  11.    
  12.         File factura = null;
  13.         fos = new FileOutputStream(factura=new File("downloaded.jpg"));
  14.  
  15.         byte[] buf = new byte[512];
  16.         while (true) {
  17.             int len = in.read(buf);
  18.             if (len == -1)
  19.                 {break;}
  20.             fos.write(buf, 0, len);
  21.         }
  22.         in.close();fos.flush();fos.close();
  23.        
  24.         return factura;
  25.     }
  26.  
  27.     private BufferedImage crearQR(String datos, int ancho, int altura) throws WriterException {
  28.         BitMatrix matrix;
  29.         Writer escritor = new QRCodeWriter();
  30.         matrix = escritor.encode(datos, BarcodeFormat.QR_CODE, ancho, altura);
  31.          
  32.         BufferedImage imagen = new BufferedImage(ancho, altura, BufferedImage.TYPE_INT_RGB);
  33.          
  34.         for(int y = 0; y < altura; y++) {
  35.             for(int x = 0; x < ancho; x++) {
  36.                 int grayValue = (matrix.get(x, y) ? 0 : 1) & 0xff;
  37.                 imagen.setRGB(x, y, (grayValue == 0 ? 0 : 0xFFFFFF));
  38.             }
  39.         }
  40.          
  41.         return imagen;        
  42.     }
  43.  
  44.  
  45.     private void concatenarFotos(String urlTemplateFactura){
  46.         try {
  47.    
  48.             File facturaTemplate = urlToImg(urlTemplateFactura);
  49.            
  50.             BufferedImage biUno = ImageIO.read(facturaTemplate);//1240x1755
  51.             BufferedImage biDos = crearQR("https://www.google.com/",200,200);
  52.        
  53.             BufferedImage biResultado = new BufferedImage(biUno.getWidth(), biUno.getHeight(), BufferedImage.TYPE_INT_ARGB);
  54.        
  55.             Graphics g = biResultado.getGraphics();
  56.             g.drawImage(biUno, 0, 0, null);
  57.             g.drawImage(biDos, biUno.getWidth()/2, biUno.getHeight()-(biUno.getHeight()/4), null);
  58.        
  59.            
  60.             //ImageIO.write(biResultado, "PNG", new File("resultado.png"));
  61.            
  62.             //~*~*~*~*~*~*~asigna la img generada como template del pdf:~*~*~*~*~*~*~
  63.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  64.             ImageIO.write(biResultado, "gif", baos);
  65.             this.image = Image.getInstance(baos.toByteArray());
  66.  
  67.             logger.info("----------------------Imagenes concatenadas correctamente----------------------");
  68.         }
  69.        
  70.         catch(Exception e) {
  71.             logger.info("----------------------Error al momento de concatenar las imgs----------------------");
  72.             System.out.println(e.getMessage());
  73.         }
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement