-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22a2997
commit b1b2f68
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
--- | ||
title: "Crear QR de AFIP en Java" | ||
description: "Con pocas líneas de código" | ||
pubDate: "Dec 17 2024" | ||
cover: "/images/blog/java-create-qr.png" | ||
category: "Java" | ||
--- | ||
|
||
Luego de [obtener el CAE](/blog/crear-factura-electronica-de-afip-en-java/) vamos a generar el QR de AFIP para nuestro comprobante. | ||
|
||
## Preparamos el QR | ||
|
||
Para generar el QR vamos a utilizar [ZXing](https://github.com/zxing/zxing), una popular librería para trabajar con códigos QR en Java, y [Gson](https://github.com/google/gson/) para trabajar con JSON. | ||
|
||
Primero, añadimos la dependencia de Maven: | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.11.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.zxing</groupId> | ||
<artifactId>core</artifactId> | ||
<version>3.5.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.zxing</groupId> | ||
<artifactId>javase</artifactId> | ||
<version>3.5.1</version> | ||
</dependency> | ||
``` | ||
|
||
Y generamos el codigo QR | ||
|
||
```java | ||
package com.example; | ||
|
||
import com.google.zxing.BarcodeFormat; | ||
import com.google.zxing.WriterException; | ||
import com.google.zxing.client.j2se.MatrixToImageWriter; | ||
import com.google.zxing.common.BitMatrix; | ||
import com.google.zxing.qrcode.QRCodeWriter; | ||
import com.google.gson.Gson; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Path; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
// Datos para el QR (Respetar los tipos string/numero) | ||
Map<String, Object> qrCodeData = new HashMap<>(); | ||
qrCodeData.put("ver", 1); // Versión del formato de los datos (1 por defecto) | ||
qrCodeData.put("fecha", "2017-10-25"); // Fecha de emisión del comprobante | ||
qrCodeData.put("cuit", 12345678912L); // Cuit del Emisor del comprobante | ||
qrCodeData.put("ptoVta", 1); // Punto de venta utilizado para emitir el comprobante | ||
qrCodeData.put("tipoCmp", 6); // Tipo de comprobante | ||
qrCodeData.put("nroCmp", 32); // Número del comprobante | ||
qrCodeData.put("importe", 150); // Importe Total del comprobante (en la moneda en la que fue emitido) | ||
qrCodeData.put("moneda", "ARS"); // Moneda del comprobante | ||
qrCodeData.put("ctz", 1); // Cotización en pesos argentinos de la moneda utilizada | ||
qrCodeData.put("tipoDocRec", 80); // Código del Tipo de documento del receptor | ||
qrCodeData.put("nroDocRec", 12345678912L); // Número de documento del receptor | ||
qrCodeData.put("tipoCodAut", "E"); // “A” para comprobante autorizado por CAEA, “E” para comprobante autorizado por CAE | ||
qrCodeData.put("codAut", 12345678912345L); // CAE o CAEA, según corresponda | ||
|
||
// Convertimos los datos a JSON y los codificamos en Base64 | ||
String json = new Gson().toJson(qrCodeData); | ||
String qrCodeText = "https://www.afip.gob.ar/fe/qr/?p=" + Base64.getUrlEncoder().encodeToString(json.getBytes()); | ||
|
||
// Generamos el QR | ||
QRCodeWriter qrCodeWriter = new QRCodeWriter(); | ||
int width = 200; | ||
int height = 200; | ||
try { | ||
BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, width, height); | ||
|
||
// Guardamos el QR como una imagen PNG | ||
Path path = FileSystems.getDefault().getPath("./qr-afip.png"); | ||
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); | ||
System.out.println("QR generado correctamente: " + path.toAbsolutePath()); | ||
|
||
// O convertimos el QR a Base64 para usarlo en HTML | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", byteArrayOutputStream); | ||
String base64Image = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray()); | ||
|
||
System.out.println("Base64 para <img>: data:image/png;base64," + base64Image); | ||
|
||
} catch (WriterException | IOException e) { | ||
System.err.println("Error al generar el QR: " + e.getMessage()); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
![QR de AFIP](/images/blog/qr-afip.png) | ||
|
||
|
||
Ya tenemos el QR listo para nuestro comprobante. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters