欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)圖像轉(zhuǎn)碼為字符畫的方法

 更新時(shí)間:2018年03月31日 10:14:21   作者:Al_assad  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖像轉(zhuǎn)碼為字符畫的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)圖像轉(zhuǎn)碼為字符畫的具體代碼,供大家參考,具體內(nèi)容如下

public class ImageProcesser { 
  
 private static final char[] charset1 = {'M','8','V','|',':','.',' '}; //默認(rèn)字符素材集 
 private char[] charset; //字符畫素材集 
 private String imgString = ""; //儲(chǔ)存轉(zhuǎn)化后的字符串 
  
  
 //使用指定字符集構(gòu)造 
 public ImageProcesser(char[] charset){ 
  this.charset = charset; 
 } 
 //使用默認(rèn)字符集構(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()){ //當(dāng)讀取的文件不存在時(shí),結(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(); 
    
   //逐行掃描圖像的像素點(diǎn),讀取RGB值,取其平均值,并從charset中獲取相應(yīng)的字符素材,并裝載到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; 
 } 
  
  
 /*圖像文件預(yù)處理:將圖片壓縮到 最長邊為 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!"); 
   
 } 
 
} 

點(diǎn)擊查看:參考鏈接。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java與js代碼互調(diào)示例代碼

    java與js代碼互調(diào)示例代碼

    用到j(luò)ava和js方法互調(diào),在用HTML5做跨平臺(tái)應(yīng)用開發(fā)時(shí)經(jīng)常會(huì)用到,在這里分享一些自己在實(shí)際開發(fā)過程中的用法,希望對(duì)初學(xué)者有所幫助
    2013-07-07
  • SpringBoot項(xiàng)目中建議關(guān)閉Open-EntityManager-in-view原因

    SpringBoot項(xiàng)目中建議關(guān)閉Open-EntityManager-in-view原因

    這篇文章主要為大家解析了在Spring Boot項(xiàng)目中建議關(guān)閉Open-EntityManager-in-view的原因示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-02-02
  • 一文詳解Java中的可變對(duì)象(Mutable)與不可變對(duì)象(Immutable)

    一文詳解Java中的可變對(duì)象(Mutable)與不可變對(duì)象(Immutable)

    如何在 Java 中創(chuàng)建不可變對(duì)象?我以前以為所有對(duì)象都是不可變的,因?yàn)槿绻愀淖円粋€(gè) String 實(shí)例的內(nèi)容,它總是會(huì)創(chuàng)建一個(gè)新的 String 對(duì)象并指向該對(duì)象,在本文中,我不僅將分享在 Java 中Immutable的步驟,還將討論可變對(duì)象與不可變對(duì)象及其優(yōu)缺點(diǎn)
    2023-11-11
  • SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫訪問

    SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫訪問

    MyBatisFlex是一款優(yōu)秀的持久層框架,本文主要介紹了SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫訪問,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析

    BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析

    這篇文章主要介紹了BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 在SSM中配置了事務(wù)控制但沒生效的問題

    在SSM中配置了事務(wù)控制但沒生效的問題

    這篇文章主要介紹了在SSM中配置了事務(wù)控制但沒生效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java多線程(單例模式,堵塞隊(duì)列,定時(shí)器)詳解

    Java多線程(單例模式,堵塞隊(duì)列,定時(shí)器)詳解

    這篇文章主要介紹了java多線程的(單例模式,堵塞隊(duì)列,定時(shí)器),具有一定參考價(jià)值,加深多線程編程的理解還是很有幫助的,需要的朋友可以參考下
    2021-08-08
  • Spring Framework常用面試題及答案匯總

    Spring Framework常用面試題及答案匯總

    這篇文章主要介紹了Spring Framework常用面試題及答案匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java?數(shù)據(jù)交換?Json?和?異步請(qǐng)求?Ajax詳解

    Java?數(shù)據(jù)交換?Json?和?異步請(qǐng)求?Ajax詳解

    Json(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,采用鍵值對(duì)的形式來表示數(shù)據(jù),它廣泛應(yīng)用于Web開發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲(chǔ),這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請(qǐng)求Ajax,需要的朋友可以參考下
    2023-09-09
  • java dom4j解析xml用到的幾個(gè)方法

    java dom4j解析xml用到的幾個(gè)方法

    這篇文章主要介紹了java dom4j解析xml用到的幾個(gè)方法,有需要的朋友可以參考一下
    2013-12-12

最新評(píng)論