java實(shí)現(xiàn)圖像轉(zhuǎn)碼為字符畫的方法
本文實(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ù)處理:將圖片壓縮到 最長(zhǎng)邊為 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)擊查看:參考鏈接。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
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 中創(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ù)庫(kù)訪問
MyBatisFlex是一款優(yōu)秀的持久層框架,本文主要介紹了SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析
這篇文章主要介紹了BeanUtils.copyProperties復(fù)制對(duì)象結(jié)果為空的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java多線程(單例模式,堵塞隊(duì)列,定時(shí)器)詳解
這篇文章主要介紹了java多線程的(單例模式,堵塞隊(duì)列,定時(shí)器),具有一定參考價(jià)值,加深多線程編程的理解還是很有幫助的,需要的朋友可以參考下2021-08-08
Java?數(shù)據(jù)交換?Json?和?異步請(qǐng)求?Ajax詳解
Json(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,采用鍵值對(duì)的形式來(lái)表示數(shù)據(jù),它廣泛應(yīng)用于Web開發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲(chǔ),這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請(qǐng)求Ajax,需要的朋友可以參考下2023-09-09

