Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private File urlToImg(String urlImg) throws Exception {
- URL url=new URL(urlImg);
- URLConnection connection = null;
- connection = url.openConnection();
- InputStream in = null;
- in = connection.getInputStream();
- FileOutputStream fos = null;
- File factura = null;
- fos = new FileOutputStream(factura=new File("downloaded.jpg"));
- byte[] buf = new byte[512];
- while (true) {
- int len = in.read(buf);
- if (len == -1)
- {break;}
- fos.write(buf, 0, len);
- }
- in.close();fos.flush();fos.close();
- return factura;
- }
- private BufferedImage crearQR(String datos, int ancho, int altura) throws WriterException {
- BitMatrix matrix;
- Writer escritor = new QRCodeWriter();
- matrix = escritor.encode(datos, BarcodeFormat.QR_CODE, ancho, altura);
- BufferedImage imagen = new BufferedImage(ancho, altura, BufferedImage.TYPE_INT_RGB);
- for(int y = 0; y < altura; y++) {
- for(int x = 0; x < ancho; x++) {
- int grayValue = (matrix.get(x, y) ? 0 : 1) & 0xff;
- imagen.setRGB(x, y, (grayValue == 0 ? 0 : 0xFFFFFF));
- }
- }
- return imagen;
- }
- private void concatenarFotos(String urlTemplateFactura){
- try {
- File facturaTemplate = urlToImg(urlTemplateFactura);
- BufferedImage biUno = ImageIO.read(facturaTemplate);//1240x1755
- BufferedImage biDos = crearQR("https://www.google.com/",200,200);
- BufferedImage biResultado = new BufferedImage(biUno.getWidth(), biUno.getHeight(), BufferedImage.TYPE_INT_ARGB);
- Graphics g = biResultado.getGraphics();
- g.drawImage(biUno, 0, 0, null);
- g.drawImage(biDos, biUno.getWidth()/2, biUno.getHeight()-(biUno.getHeight()/4), null);
- //ImageIO.write(biResultado, "PNG", new File("resultado.png"));
- //~*~*~*~*~*~*~asigna la img generada como template del pdf:~*~*~*~*~*~*~
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ImageIO.write(biResultado, "gif", baos);
- this.image = Image.getInstance(baos.toByteArray());
- logger.info("----------------------Imagenes concatenadas correctamente----------------------");
- }
- catch(Exception e) {
- logger.info("----------------------Error al momento de concatenar las imgs----------------------");
- System.out.println(e.getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement