[英]Convert base64 string to Image in Java
我有一个图像通过JSON字符串发送给我。 我想将该字符串转换为我的Android应用程序中的图像,然后显示该图像。
JSON字符串如下所示:
"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..."
注意:我用...截断了字符串
我有一个函数(我认为)将字符串转换为图像。 我这样做了吗?
public Bitmap ConvertToImage(String image){
try{
InputStream stream = new ByteArrayInputStream(image.getBytes());
Bitmap bitmap = BitmapFactory.decodeStream(stream);
return bitmap;
}
catch (Exception e) {
return null;
}
}
然后我尝试在我的Android活动上显示它
String image = jsonObject.getString("barcode_img");
Bitmap myBitmap = this.ConvertToImage(image);
ImageView cimg = (ImageView)findViewById(R.id.imageView1);
//Now try setting dynamic image
cimg.setImageBitmap(myBitmap);
但是,当我这样做时,什么都没有出现。 我没有在logcat中得到任何错误。 我究竟做错了什么?
谢谢
我担心你只需解码base64字符串来获取图像字节,所以在你的
"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..."
字符串,你必须得到数据之后的data:image\\/png;base64,
,所以你只得到图像字节然后解码它们:
String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1);
InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
这是一个代码,所以你了解它是如何工作的,但如果你收到一个JSON对象,它应该以正确的方式完成:
data
键下提取String。 image/png
开头,因此您知道是png图像。 base64
字符串,因此您知道必须解码数据。 base64
字符串之后解码数据以获取图像。 InputStream stream = new ByteArrayInputStream(image.getBytes());
应改为
InputStream stream = new ByteArrayInputStream(Base64.decode(image.getBytes(), Base64.DEFAULT));
有关如何进行Base64解码的详细信息,请参阅http://developer.android.com/reference/android/util/Base64.html 。
免责声明:我没有检查语法,但这是你应该怎么做。
以下是转换Base64编码的inputStream并将其写入磁盘的工作代码。 我花了一些时间让它正常工作。 所以我希望这有助于其他开发人员。
public boolean writeImageToDisk(FileItem item, File imageFile) {
// clear error message
errorMessage = null;
FileOutputStream out = null;
boolean ret = false;
try {
// write thumbnail to output folder
out = createOutputStream(imageFile);
// Copy input stream to output stream
byte[] headerBytes = new byte[22];
InputStream imageStream = item.getInputStream();
imageStream.read(headerBytes);
String header = new String(headerBytes);
// System.out.println(header);
byte[] b = new byte[4 * 1024];
byte[] decoded;
int read = 0;
while ((read = imageStream.read(b)) != -1) {
// System.out.println();
if (Base64.isArrayByteBase64(b)) {
decoded = Base64.decodeBase64(b);
out.write(decoded);
}
}
ret = true;
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
errorMessage = "error: " + sw;
} finally {
if (out != null) {
try {
out.close();
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
System.out.println("Cannot close outputStream after writing file to disk!" + sw.toString());
}
}
}
return ret;
}
/**
* Helper method for the creation of a file output stream.
*
* @param imageFolder
* : folder where images are to be saved.
* @param id
* : id of spcefic image file.
* @return FileOutputStream object prepared to store images.
* @throws FileNotFoundException
*/
protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException {
imageFile.getParentFile().mkdirs();
return new FileOutputStream(imageFile);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.