获取网络图片JPG格式的URL后得到流或者byte数组,如何将这个流或者数组从JPG转换成PNG格式?
无敌大欢 2018-12-12 06:08:58 比如这样的,我下载jpg格式的
String url = "http;//xx/xx/xx/a.jpg";
byte[] bytes = HttpUtil.getFileStream(url);
public static byte[] getFileStream(String url)throws Exception{
try {
URL httpUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)httpUrl.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
byte[] btImg = readInputStream(inStream);//得到图片的二进制数据
return btImg;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}