java流图片
‘壹’ java中如何把图片转换成二进制流
Java中将图片转为二进制流只需要使用FileImageInputStream取得图片文件,然后使用ByteArrayOutputStream 写入到二进制流中即可,下面是详细代码:
//图片到byte数组
publicbyte[]image2byte(Stringpath){
byte[]data=null;
FileImageInputStreaminput=null;
try{
input=newFileImageInputStream(newFile(path));
ByteArrayOutputStreamoutput=newByteArrayOutputStream();
byte[]buf=newbyte[1024];
intnumBytesRead=0;
while((numBytesRead=input.read(buf))!=-1){
output.write(buf,0,numBytesRead);
}
data=output.toByteArray();
output.close();
input.close();
}
catch(FileNotFoundExceptionex1){
ex1.printStackTrace();
}
catch(IOExceptionex1){
ex1.printStackTrace();
}
returndata;
}
另外,如果需要将byte[]存回图片或转为String,则:
//byte数组到图片
publicvoidbyte2image(byte[]data,Stringpath){
if(data.length<3||path.equals(""))return;
try{
=newFileImageOutputStream(newFile(path));
imageOutput.write(data,0,data.length);
imageOutput.close();
System.out.println("MakePicturesuccess,Pleasefindimagein"+path);
}catch(Exceptionex){
System.out.println("Exception:"+ex);
ex.printStackTrace();
}
}
//byte数组到16进制字符串
publicStringbyte2string(byte[]data){
if(data==null||data.length<=1)return"0x";
if(data.length>200000)return"0x";
StringBuffersb=newStringBuffer();
intbuf[]=newint[data.length];
//byte数组转化成十进制
for(intk=0;k<data.length;k++){
buf[k]=data[k]<0?(data[k]+256):(data[k]);
}
//十进制转化成十六进制
for(intk=0;k<buf.length;k++){
if(buf[k]<16)sb.append("0"+Integer.toHexString(buf[k]));
elsesb.append(Integer.toHexString(buf[k]));
}
return"0x"+sb.toString().toUpperCase();
}
‘贰’ java中如何把一个图片转换成二进制流存入到类中啊
1.将Image图像文件存入到数据库中
我们知道数据库里的Image类型的数据是"二进制数据",因此必须将图像文件转换成字节数组才能存入数据库中.
要这里有关数据的操作略写,我将一些代码段写成方法,方便直接调用.
//根据文件名(完全路径)
public byte[] SetImageToByteArray(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
int streamLength = (int)fs.Length;
byte[] image = new byte[streamLength];
fs.Read(image, 0, streamLength);
fs.Close();
return image;
}
//另外,在ASP.NET中通过FileUpload控件得到的图像文件可以通过以下方法
public byte[] SetImageToByteArray(FileUpload FileUpload1)
{
Stream stream = FileUpload1.PostedFile.InputStream;
byte[] photo = new byte[FileUpload1.PostedFile.ContentLength];
stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength);
stream.Close();
return photo;
}
2.从SQL Server数据库读取Image类型的数据,并转换成bytes[]或Image图像文件
//要使用SqlDataReader要加载using System.Data.SqlClient命名空间
//将数据库中的Image类型转换成byte[]
public byte[] SetImage(SqlDataReader reader)
{
return (byte[])reader["Image"];//Image为数据库中存放Image类型字段
}
//将byte[]转换成Image图像类型
//加载以下命名空间using System.Drawing;/using System.IO;
using System.Data.SqlClient;*/
public Image SetByteToImage(byte[] mybyte)
{
Image image;
MemoryStream mymemorystream = new MemoryStream(mybyte,0, mybyte.Length);
image = Image.FromStream(mymemorystream);
return image;
}
‘叁’ java把图片转换成二进制流
public static void main(String[] args) throws Exception {
File file = new File("d:\L.jpg");//图片
FileInputStream fis = new FileInputStream(file);//把图片变成流
FileOutputStream fos = new FileOutputStream(new File("E:\L.jpg")); //把图片流写入E盘
byte[] read = new byte[1024]; //每次读取的字节 可以自己定义 256 512 1024 2048 等。。。
int len = 0;
while((len = fis.read(read))!= -1){ //读取变成流的图片
fos.write(read,0,len);//写入图片
}
fis.close();//关闭输入流
fos.close();//关闭输出流
}