Java中将图片转为二进制流只需要使用FileImageInputStream取得图片文件,然后使用ByteArrayOutputStream 写入到二进制流中即可,下面是详细代码:
//图片到byte数组
public byte[] image2byte(String path){
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
}
catch (FileNotFoundException ex1) {
ex1.printStackTrace();
}
catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
另外,如果需要将byte[]存回图片或转为String,则:
//byte数组到图片
public void byte2image(byte[] data,String path){
if(data.length<3||path.equals("")) return;
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
//byte数组到16进制字符串
public String byte2string(byte[] data){
if(data==null||data.length<=1) return "0x";
if(data.length>200000) return "0x";
StringBuffer sb = new StringBuffer();
int buf[] = new int[data.length];
//byte数组转化成十进制
for(int k=0;kbuf[k] = data[k]<0?(data[k]+256):(data[k]);
}
//十进制转化成十六进制
for(int k=0;kif(buf[k]<16) sb.append("0"+Integer.toHexString(buf[k]));
else sb.append(Integer.toHexString(buf[k]));
}
return "0x"+sb.toString().toUpperCase();
}
使用java的IO流对图片进行二进制读取操作即可
示例为:读取图片为二进制流,并写入到其他图片中
static void testCopyImage(){
File source=new File("E:\\share\\Wallpaper\\Bliss.jpg");
File desk=new File("d:\\images");
if(!desk.exists()){
desk.mkdir();
}
try {
FileInputStream inputStream=new FileInputStream(source);
FileOutputStream outputStream=new FileOutputStream(new File("d:/images/Bliss.jpg"));
int ch=inputStream.read();
while(ch!=-1){
outputStream.write(ch);
ch=inputStream.read();
}
inputStream.close();
outputStream.close();
System.out.println("图片复制成功!");
} catch (FileNotFoundException e) {
System.out.println("文件不存在:"+e.getMessage());
} catch (IOException e) {
System.out.println("文件读取错误:"+e.getMessage());
}
}
我们知道数据库里的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;
}