欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java中Byte数组与图片的转换 博客分类: j2se imagebufferedimagebytearrayoutputstreambytearrayinputstream 

程序员文章站 2024-02-24 21:03:58
...
package com.image;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;


public class ImageWithByte {
	
	 public static  byte[] image2Bytes(String imagePath) throws Exception {
		 BufferedImage bu=ImageIO.read(new File(imagePath));
	     ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
	     try {
	         boolean resultWrite = ImageIO.write(bu, "jpg", imageStream);
	     } catch (Exception e) {
	      	 e.printStackTrace();
	     }
	     imageStream.flush();
	     byte[] tagInfo = imageStream.toByteArray();

	     return tagInfo;
	    } 
	   
	public static void main(String[] args) throws Exception {
	        byte[]info=image2Bytes("D:/我的文档/美女.jpg");
	        ByteArrayInputStream imageStream = new ByteArrayInputStream(info);
	        BufferedImage image=ImageIO.read(imageStream);
	        String newFilePath="d:/我的文档/图片收藏/美女.jpg";	        
	        ImageIO.write(image, "jpg", new File(newFilePath));

	   }
}