[英]How to convert List<String> of Strings to BufferedImage in Java
我想將Base64 String數組轉換為BufferedImage
。 我嘗試完成的方法:
static BufferedImage decodeToImage(List<String> imageStrings) {
BufferedImage image = null;
ByteBuffer imageByteBuffer = ByteBuffer.allocate(500000000);
try {
BASE64Decoder decoder = new BASE64Decoder();
for (String imageString : imageStrings) {
imageByteBuffer.put(imageString.getBytes());
}
ByteArrayInputStream bis = new ByteArrayInputStream(imageByteBuffer.array());
image = ImageIO.read(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
我想返回一個BufferedImage
但它返回null。
您可以使用以下命令:
public static BufferedImage decodeToImage(List<String> imageStrings) {
try {
byte[] bytes = Base64.getDecoder().decode(String.join("", imageStrings));
return ImageIO.read(new ByteArrayInputStream(bytes));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
你看過你要做什么嗎?
導致此代碼
public static void main(String[] args) {
String base64String = "data:image/png;base64,iVBORw0...=";
String[] strings = base64String.split(",");
List<String> param = new ArrayList<String>();
param.add(strings[1]);
BufferedImage bi = decodeToImage(param);
System.out.println("BufferedImage: " + bi);
}
static BufferedImage decodeToImage(List<String> imageStrings) {
BufferedImage image = null;
ByteBuffer imageByteBuffer = ByteBuffer.allocate(500000000);
try {
for (String imageString : imageStrings) {
imageByteBuffer.put(DatatypeConverter.parseBase64Binary(imageString));
}
ByteArrayInputStream bis = new ByteArrayInputStream(imageByteBuffer.array());
image = ImageIO.read(bis);
bis.close();
} catch (Exception e) {
System.out.println("error?");
e.printStackTrace();
}
return image;
}
將打印
BufferedImage:BufferedImage @ 7eda2dbb:type = 6 ColorModel:#pixelBits = 32 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@6576fe71透明度= 3 has alpha = true isAlphaPre = false ByteInterleavedRaster:width = 302 height = 232 #numDataElements 4個dataOff [0] = 3
但是,如果我只給它整個base64文本,並且圖像信息在前面
public static void main(String[] args) {
String base64String = "data:image/png;base64,iVBORw0...=";
String[] strings = base64String.split(",");
List<String> param = new ArrayList<String>();
param.add(base64String); //THIS IS THE DIFFERENT LINE
BufferedImage bi = decodeToImage(param);
System.out.println("BufferedImage: " + bi);
}
它將打印null
將base64String替換為來自的有效base 64編碼的png
http://freeonlinetools24.com/base64-image
我在此示例中將其縮減,因為否則它將變成數千個字符。
Java中字符串的最大長度真的對您的代碼有問題嗎? 如果沒有,我會像這樣簡化您的代碼(並修復解碼錯誤):
static BufferedImage decodeToImage(String imageString) {
try {
BASE64Decoder decoder = new BASE64Decoder();
return ImageIO.read(new ByteArrayInputStream(decoder.decode(imageString)));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
或者,如果您確實對字符串的長度有疑問(Base64的輸出長度長於輸入,因此在極端情況下可能有意義,但請進行驗證,否則將使您的代碼變得不必要),請保留以下列表:字符串。 我只是跳過ByteBuffer
而是使用ByteArrayOutputStream
,因為使用起來要容易得多。
static BufferedImage decodeToImage(List<String> imageStrings) {
try {
ByteArrayOuputStream buffer = new ByteArrayOuputStream();
BASE64Decoder decoder = new BASE64Decoder();
for (String imageString : imageStrings) {
buffer.write(decoder.decode(imageString));
}
return ImageIO.read(new ByteArrayInputStream(buffer.toArray()));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
PS:雖然通常應該關閉流,但是ByteArrayInputStream
和ByteArrayOutputStream
上的close()
方法是禁止操作的。
緩沖區有點棘手。 最好在這里看看: https : //docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html#flip()
據我所記得,您必須先准備好緩沖區,然后才能讀取它。 必須將內部“光標”的位置設置為起始位置,然后您才能從該位置讀取“容量”字節。 好吧,正如我所說:據我所記得。 大約十二年前,我不得不直接使用緩沖區。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.