JAVA實現讀取圖片


話不讀說  直接上代碼

  1 package cn.kgc.ssm.common;
  2 
  3 import java.io.*;
  4 
  5 /**
  6  * @author
  7  * @create 2019-08-15 9:36
  8  **/
  9 public class DDD {
 10     /**
 11      * 讀取圖片  返回一個圖片的字節數組
 12      * @param path
 13      * @return
 14      */
 15     public static byte[] imgArray(String path) {
 16         //字節輸入流
 17         InputStream inputStream = null;
 18         //字節緩沖流數組
 19         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 20         try {
 21             inputStream = new FileInputStream(path);
 22             byte[] b = new byte[1024];
 23             int len = -1;
 24             //循環讀取
 25             while ((len = inputStream.read(b)) != 1) {
 26                 byteArrayOutputStream.write(b, 0, len);
 27             }
 28             //返回byteArrayOutputStream數組
 29             return byteArrayOutputStream.toByteArray();
 30         } catch (FileNotFoundException e) {
 31             e.printStackTrace();
 32         } catch (IOException e) {
 33             e.printStackTrace();
 34         } finally {
 35             try {
 36                 //關閉流
 37                 inputStream.close();
 38             } catch (IOException e) {
 39                 e.printStackTrace();
 40             }
 41         }
 42         return null;
 43     }
 44 
 45     public static void writeImg(byte[]array,String path){
 46         //創建一個字節輸出流
 47         DataOutputStream dataOutputStream = null;
 48         try {
 49             dataOutputStream = new DataOutputStream(new FileOutputStream(path));
 50             //將字節數組
 51             dataOutputStream.write(array);
 52         } catch (IOException e) {
 53             e.printStackTrace();
 54         }finally {
 55             try {
 56                 //關閉
 57                 dataOutputStream.close();
 58             } catch (IOException e) {
 59                 e.printStackTrace();
 60             }
 61         }
 62     }
 63 
 64     /**
 65      * 讀取二進制保存的圖片  放到數組里
 66      * @param path
 67      * @return
 68      */
 69     public static byte[] imageIn(String path){
 70         //創建一個字節輸出流
 71         DataInputStream dataInputStream = null;
 72         try {
 73             dataInputStream = new DataInputStream(new FileInputStream(path));
 74             //創建一個字節數組  byte的長度等於二進制圖片的返回的實際字節數
 75             byte[] b = new byte[dataInputStream.available()];
 76             //讀取圖片信息放入這個b數組
 77             dataInputStream.read(b);
 78             return b;
 79         } catch (FileNotFoundException e) {
 80             e.printStackTrace();
 81         } catch (IOException e) {
 82             e.printStackTrace();
 83         }finally {
 84             try {
 85                 //關閉流
 86                 dataInputStream.close();
 87             } catch (IOException e) {
 88                 e.printStackTrace();
 89             }
 90         }
 91         return null;
 92     }
 93 
 94     /**
 95      * 讀取二進制保存的圖片 輸出圖片
 96      * @param img
 97      * @param path
 98      */
 99     public static void writImg(byte[]img,String path){
100         //創建一個字節輸出流
101         OutputStream outputStream = null;
102         try {
103             outputStream = new FileOutputStream(path);
104             //將圖片輸處到流中
105             outputStream.write(img);
106             //刷新
107             outputStream.flush();
108         } catch (FileNotFoundException e) {
109             e.printStackTrace();
110         } catch (IOException e) {
111             e.printStackTrace();
112         }finally {
113             try {
114                 //關閉
115                 outputStream.close();
116             } catch (IOException e) {
117                 e.printStackTrace();
118             }
119         }
120     }
121 
122     /**
123      * main方法
124      * @param args
125      */
126     public static void main(String[] args) {
127         //獲取圖片  將圖片信息把存到數組b中
128         byte[] b = DDD.imgArray("D:\\5.JPG");
129         //通過數組B寫到文件中
130         DDD.writImg(b,"img.txt");
131         //讀取二進制文件保存到一個數組中
132         byte[] c = DDD.imageIn("img.txt");
133         //通過數組c  輸出圖片
134         DDD.writImg(c,"img.jpg");
135     }
136 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM