java實現(xiàn)圖像轉(zhuǎn)碼為字符畫的方法
更新時間:2018年03月31日 10:14:21 作者:Al_assad
這篇文章主要為大家詳細介紹了java實現(xiàn)圖像轉(zhuǎn)碼為字符畫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)圖像轉(zhuǎn)碼為字符畫的具體代碼,供大家參考,具體內(nèi)容如下
public class ImageProcesser { private static final char[] charset1 = {'M','8','V','|',':','.',' '}; //默認字符素材集 private char[] charset; //字符畫素材集 private String imgString = ""; //儲存轉(zhuǎn)化后的字符串 //使用指定字符集構(gòu)造 public ImageProcesser(char[] charset){ this.charset = charset; } //使用默認字符集構(gòu)造 public ImageProcesser(){ this.charset = charset1; } public String getImgString(){ return imgString; } /*將圖形文件轉(zhuǎn)化為字符畫字符串*/ public ImageProcesser toBitmapConvert(String imagepath){ return toBitmapConvert(new File(imagepath)); } public ImageProcesser toBitmapConvert(File imageFile){ StringBuffer sb = new StringBuffer(); if(!imageFile.exists()){ //當讀取的文件不存在時,結(jié)束程序 System.out.println("File is not exists!"); System.exit(1); } Color color; try{ BufferedImage buff = ImageIO.read(imageFile); //將圖片文件裝載如BufferedImage流 buff = compressImage(buff); int bitmapH = buff.getHeight(); int bitmapW = buff.getWidth(); //逐行掃描圖像的像素點,讀取RGB值,取其平均值,并從charset中獲取相應的字符素材,并裝載到sb中 for(int y=0; y<bitmapH; y++){ for(int x=0; x<bitmapW; x++){ int rgb = buff.getRGB(x,y); color = new Color(rgb); int cvalue = (color.getRed()+color.getGreen()+color.getBlue()) / 3; sb.append(charset[(int)((cvalue * charset.length - 1)/255)]+" "); } sb.append("\r\n"); } }catch(IOException ex){ ex.printStackTrace(); } imgString = sb.toString(); return this; } /*圖像文件預處理:將圖片壓縮到 最長邊為 100px*/ private BufferedImage compressImage(BufferedImage srcImg){ int h = srcImg.getHeight(); int w = srcImg.getWidth(); if(Math.max(h,w)<=100) return srcImg; int new_H; int new_W; if(w>h){ new_W = 100; new_H = 100*h/w ; }else{ new_H = 100; new_W = 100*w/h; } BufferedImage smallImg = new BufferedImage(new_W,new_H,srcImg.getType()); Graphics g = smallImg.getGraphics(); g.drawImage(srcImg,0,0,new_W,new_H,null); g.dispose(); return smallImg; } /*將字符串保存為.txt文件*/ public void saveAsTxt(String fileName){ try{ PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); for(int i = 0;i<imgString.length();i++){ out.print(imgString.charAt(i)); } out.close(); }catch(IOException ex){ ex.printStackTrace(); } } /*批處理圖像文件*/ public static void batchImgFile(String srcfile, String tragetfile){ File folder = new File(tragetfile); //生成圖片的文件夾 File srcfolder = new File(srcfile); if(!folder.exists() || !folder.isDirectory()) folder.mkdirs(); ImageProcesser processer = new ImageProcesser(); File[] filelist = srcfolder.listFiles(); for(int i=0;i<filelist.length;i++){ if(!filelist[i].isFile()) continue; processer.toBitmapConvert(filelist[i]); processer.saveAsTxt(tragetfile+"/"+(i+1)+".txt"); System.out.println(filelist[i].getName()+" is converted!"); } System.out.println("All img were converted!"); } }
點擊查看:參考鏈接。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項目中建議關(guān)閉Open-EntityManager-in-view原因
這篇文章主要為大家解析了在Spring Boot項目中建議關(guān)閉Open-EntityManager-in-view的原因示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02一文詳解Java中的可變對象(Mutable)與不可變對象(Immutable)
如何在 Java 中創(chuàng)建不可變對象?我以前以為所有對象都是不可變的,因為如果你改變一個 String 實例的內(nèi)容,它總是會創(chuàng)建一個新的 String 對象并指向該對象,在本文中,我不僅將分享在 Java 中Immutable的步驟,還將討論可變對象與不可變對象及其優(yōu)缺點2023-11-11SpringBoot使用MyBatis-Flex實現(xiàn)靈活的數(shù)據(jù)庫訪問
MyBatisFlex是一款優(yōu)秀的持久層框架,本文主要介紹了SpringBoot使用MyBatis-Flex實現(xiàn)靈活的數(shù)據(jù)庫訪問,具有一定的參考價值,感興趣的可以了解一下2024-06-06BeanUtils.copyProperties復制對象結(jié)果為空的原因分析
這篇文章主要介紹了BeanUtils.copyProperties復制對象結(jié)果為空的原因分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Java?數(shù)據(jù)交換?Json?和?異步請求?Ajax詳解
Json(JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,采用鍵值對的形式來表示數(shù)據(jù),它廣泛應用于Web開發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲,這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請求Ajax,需要的朋友可以參考下2023-09-09