获取网络图片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();
}
...全文
886 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Silly-77 2018-12-12
  • 打赏
  • 举报
回复
0.0JPG转PNG 直接改后缀不就好了嘛。。。你读完之后保存为png格式呗。
如果你是想要透明图层的话-。- 那我就不知道了。。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧