将HTTPResponse转换为图像

时间:2018-07-25 09:13:09

标签: zpl

对于其中一种实现,我从Java代码中点击了标签化的Rest服务,以将ZPL格式的代码转换为Image。 我能够成功获取响应。但是我无法将HttpResponse转换为图像文件。

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost "http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/"); 
byte[] byteArray = Base64.decodeBase64(base64Val.getBytes());
String decodedString = new String(byteArray);
StringEntity requestEntity;
try {
requestEntity = new StringEntity(decodedString);
requestEntity.setContentType("application/x-www-form-urlencoded");
post.setEntity(requestEntity);
HttpResponse response = client.execute(post);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
#Need suggestions to convert BufferReader to Image
}

发布引用建议答案的代码,代码如下:

HttpResponse response = client.execute(post); InputStream inStream = response.getEntity().getContent(); String dataString = convertStreamToString(inStream); byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(dataString); BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes)); File outputfile = new File("myImage.png"); ImageIO.write(image, "png", outputfile);

1 个答案:

答案 0 :(得分:0)

将此字节用于image converter。数据字符串中的过去值,并检查是否得到图像。