网上现成的太多
public static byte[] image2Bytes(String imagePath) {
ImageIcon ima = new ImageIcon(imagePath);
BufferedImage bu = new BufferedImage(ima.getImage().getWidth(null),
ima .getImage().getHeight(null), BufferedImage.TYPE_INT_RGB);
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
try {
//把这个jpg图像写到这个流中去,这里可以转变图片的编码格式
boolean resultWrite = ImageIO.write(bu, "png", imageStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] tagInfo = imageStream.toByteArray();
return tagInfo;
}
public static void main(String[] args) {
byte[]info=Image2Byte.image2Bytes("D:/File/1294_05051048_03.jpg");
for(int i=0;i<info.length;i++){
System.out.println(info[i]);
}
}
追问你好 首先谢谢你,其实我是想要这个把这byte数组写到一个txt文件中去,我想要的是这个含有byte数组的 txt 文件 ,是这样的 ,呵呵
追答那把main方法改为这个
public static void main(String[] args) {
byte[]info=Image2Byte.image2Bytes("D:/File/1294_05051048_03.jpg");
try {
FileWriter file=new FileWriter("d:\\text.txt");
for(byte in:info){
file.write(String.valueOf((int)in));
}
file.flush();
}catch (Exception e) {
e.printStackTrace();
}
}是这种效果吗?