java 将图片转成二进制数据
时间: 2023-10-06 17:13:16 浏览: 105
Java可以通过以下代码将图片转换成二进制数据:
```java
import java.io.*;
public class ImageToBinary {
public static void main(String args[]) throws IOException {
FileInputStream fis = null;
DataInputStream dis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("image.jpg");
dis = new DataInputStream(fis);
fos = new FileOutputStream("image.bin");
byte[] buffer = new byte[1024];
int length;
while ((length = dis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
fis.close();
dis.close();
fos.close();
}
}
}
```
其中,将图片文件名 `image.jpg` 替换成你要转换的图片文件名,将输出文件名 `image.bin` 替换成你要保存二进制数据的文件名即可。