图片和字节之间的互相转换
2012-10-24 17:51
288 查看
package xiao.wei.test; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import javax.imageio.ImageIO; public class TestImage { public static byte[] toByteArray(File imageFile) throws Exception { BufferedImage img = ImageIO.read(imageFile); ByteArrayOutputStream buf = new ByteArrayOutputStream((int) imageFile .length()); try { ImageIO.write(img, "jpg", buf); } catch (Exception e) { e.printStackTrace(); return null; } return buf.toByteArray(); } public static void main(String[] args) throws Exception { byte[] b = toByteArray(new File("F:" + File.separator + "lol.jpg")); ByteArrayInputStream in = new ByteArrayInputStream(b); BufferedImage image = ImageIO.read(in); File newFile = new File("d:" + File.separator + "lol.jpg"); ImageIO.write(image, "jpg", newFile); } }
相关文章推荐
- JAVA 实现jpg/tif/bmp 等图片之间格式得互相转换
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
- 2进制数据与图片之间的互相转换
- 字节数组与浮点数之间的互相转换(IEEE)
- 使用JAVA 实现jpg/tif/bmp 等图片之间格式得互相转换
- 视频与图片序列之间的互相转换
- JAVA 实现jpg/tif/bmp 等图片之间格式得互相转换
- C#学习笔记之图片、字节数组、base64之间的转换
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
- [java] 图片与base64之间的互相转换
- 图片和byte[]的互相转换
- Base64编码和图片的互相转换 .
- Win8 图片uri保存到本地图片库 -- 及各种流之间的转换
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byt
- 多字节与宽字节之间的转换
- java实现图片与base64字符串之间的转换
- iOS class与string之间的互相转换
- java对象和json对象之间互相转换(中)
- 二进制数据与对象之间互相转换
- C# 图片与byte[]之间以及byte[]与string之间的转换