es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

¿Debe devolver byte[] en lugar de BufferedImage en Java?

Estoy tratando de modificar el siguiente método para que devuelva byte[] (matriz de bytes) en lugar de BufferedImage. También puedo usar otra implementación que devuelva byte[], pero esa implementación no es configurable como la siguiente. Entonces, ¿cómo puedo hacer que este método devuelva byte[] en lugar de BufferedImage?

“`
public static BufferedImage getQRCode(String targetUrl, int width,
int height) {
Hashtable<EncodeHintType, Object> hintMap = new Hashtable<>();

<pre><code>hintMap.put(EncodeHintType.ERROR_CORRECTION,
ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(targetUrl,
BarcodeFormat.QR_CODE, width, height, hintMap);
int CrunchifyWidth = byteMatrix.getWidth();

BufferedImage image = new BufferedImage(CrunchifyWidth,
CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
image.createGraphics();

Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
graphics.setColor(Color.BLACK);

for (int i = 0; i < CrunchifyWidth; i++) {
for (int j = 0; j < CrunchifyWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
return image;
</code></pre>

}
“`

Tags:  , , , ,

Answer

  1. Avatar for davy.ai

    Para modificar el método anterior y devolver byte[] en lugar de BufferedImage, podemos crear un objeto ByteArrayOutputStream y escribir el contenido de BufferedImage en este flujo. Finalmente, podemos convertir el flujo en un byte[] utilizando el método toByteArray() de ByteArrayOutputStream. Aquí está el código modificado:

    public static byte[] getQRCode(String targetUrl, int width, int height) throws IOException {
        Hashtable<encodehinttype, object=""> hintMap = new Hashtable<>();
    
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix byteMatrix = qrCodeWriter.encode(targetUrl, BarcodeFormat.QR_CODE, width, height, hintMap);
        int CrunchifyWidth = byteMatrix.getWidth();
    
        BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyWidth, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
    
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyWidth);
        graphics.setColor(Color.BLACK);
    
        for (int i = 0; i < CrunchifyWidth; i++) {
            for (int j = 0; j < CrunchifyWidth; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        byte[] bytes = baos.toByteArray();
    
        return bytes;
    }
    

    Aquí, hemos reemplazado return image; con el siguiente código:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);
    byte[] bytes = baos.toByteArray();
    return bytes;
    

    Aquí, estamos creando un objeto ByteArrayOutputStream llamado baos y escribiendo el contenido de BufferedImage en él utilizando el método ImageIO.write(). Finalmente, estamos devolviendo el byte[] utilizando el método baos.toByteArray().</encodehinttype,>

Comments are closed.