我正在嘗試修改以下方法,使其回傳byte[](位元組陣列)而不是BufferedImage. 我還可以使用另一個回傳的實作byte[],但該實作是不可配置的,如下所示。那么,我怎樣才能讓這個方法回傳byte[]而不是BufferedImage?
public static BufferedImage getQRCode(String targetUrl, int width,
int height) {
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);
}
}
}
return image;
}
uj5u.com熱心網友回復:
你需要使用類似 ByteArrayOutputStream 的東西
public static byte[] toByteArray(BufferedImage bi, String format)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, format, baos);
byte[] bytes = baos.toByteArray();
return bytes;
參考:https : //mkyong.com/java/how-to-convert-bufferedimage-to-byte-in-java/ }
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/382267.html
上一篇:使用陣列輸入神經網路
