Java 圖片轉二進位制及生成圖片
阿新 • • 發佈:2019-02-09
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Image2Binary { public static void main(String[] args) { String path = "V:/test.bmp"; File file = new File(path); FileInputStream fis; try { fis = new FileInputStream(file); byte[] b; b = new byte[fis.available()]; StringBuilder str = new StringBuilder();// 不建議用String fis.read(b); for (byte bs : b) { str.append(Integer.toBinaryString(bs));// 轉換為二進位制 } System.out.println(str); File apple = new File("V:/test2.bmp");// 把位元組陣列的圖片寫到另一個地方 FileOutputStream fos = new FileOutputStream(apple); fos.write(b); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }