zoukankan      html  css  js  c++  java
  • 将图片转换成二进制, (用到 输入流,输出流)

      public static void main(String[]args) throws IOException{  
            
                String path = "E:/图片/111.jpg"; //图片地址
                File file = new File(path);     //将图片转换成file类型的文件
                FileInputStream fis = new FileInputStream(file); //创建输入流
                byte[] byt = new byte[fis.available()]; //字节数据      available() 返回的实际可读字节数,也就是总大小
                StringBuilder str = new StringBuilder();// 不建议用String
                fis.read(byt); // 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b  中。以整数形式返回实际读取的字节数。 
                for (byte bs : byt) {
                        // 将字节数组byte[] 转换为二进制,在以字符串的形式显示出来     
                        //Integer.toBinaryString(bs)返回无符号整数的二进制的字符串表示形式
                        str.append(Integer.toBinaryString(bs)); 
                }
                str.toString();
                
                System.out.println("输出二进制字符串+==="+str);   
                
                // 把字节数组的图片写到另一个地方
                File apple = new File("D:/apple.jpg");
                FileOutputStream fos = new FileOutputStream(apple);
                fos.write(byt);       
                fos.flush();
                fos.close();
        }
  • 相关阅读:
    python-深浅copy-18
    Python-集合-17
    linux-阿里云仓库搭建-搭建本地仓库-yum
    python-知识回顾-16
    python-编码-15
    python-小知识点-14
    codevs 1048石子归并
    codevs 1048 石子归并
    codevs1068乌龟棋
    codevs 1697 ⑨要写信
  • 原文地址:https://www.cnblogs.com/sunhaoyu/p/4602958.html
Copyright © 2011-2022 走看看