用java写个程序 将 图像文件(png,jpg,等等) 转换成byte数组的 程序 谢谢 了

用java写个程序 将 图像文件(png,jpg,等等) 转换成byte数组的 程序 谢谢 了
我想要把转换后的字节数组文件写到一个txt中去,我要的就是那个txt文件,
不能是乱码,急 等!!!!!!!!!!

网上现成的太多
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();
}

}是这种效果吗?

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-03-11
/* 读写图像文件 */
import javax.swing.JOptionPane;
import java.io.*;
class FileRW
{
int bytes;
byte buffer[ ] = new byte[65560];
FileInputStream fileInput;
FileOutputStream fileOutput;

FileRW()
{
takeimg();
loadimg();
JOptionPane.showMessageDialog(null,"文件复制并更名成功!\n文件大小为:"+bytes);
System.exit(0); //退出程序
}
//读取图像文件a.jpg
public void takeimg()
{
try {
File file=new File("a.jpg");
fileInput = new FileInputStream(file);
bytes = fileInput.read(buffer,0,65560);
} catch(IOException ei) { System.out.println(ei); }
}
//写入到b.jpg
public void loadimg()
{
try {
fileOutput = new FileOutputStream("b.jpg");
fileOutput.write(buffer, 0, bytes) ;
} catch(IOException eo) { System.out.println(eo); }
}
}

public class 读写图像文件
{
public static void main(String[] args)
{ new FileRW(); }
}本回答被网友采纳
第2个回答  2011-03-11
体,转换之后就不会乱码或者变框框了。
相似回答