java實(shí)現(xiàn)圖像轉(zhuǎn)碼為字符畫(huà)的方法
更新時(shí)間:2018年03月31日 10:14:21 作者:Al_assad
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖像轉(zhuǎn)碼為字符畫(huà)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了java實(shí)現(xiàn)圖像轉(zhuǎn)碼為字符畫(huà)的具體代碼,供大家參考,具體內(nèi)容如下
public class ImageProcesser {
private static final char[] charset1 = {'M','8','V','|',':','.',' '}; //默認(rèn)字符素材集
private char[] charset; //字符畫(huà)素材集
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)化為字符畫(huà)字符串*/
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)文章
Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之二叉堆
二叉堆是一種特殊的堆,其實(shí)質(zhì)是完全二叉樹(shù)。二叉堆有兩種:最大堆和最小堆。最大堆是指父節(jié)點(diǎn)鍵值總是大于或等于任何一個(gè)子節(jié)點(diǎn)的鍵值。而最小堆恰恰相反,指的是父節(jié)點(diǎn)鍵值總是小于任何一個(gè)子節(jié)點(diǎn)的鍵值2022-02-02
詳解spring項(xiàng)目中如何動(dòng)態(tài)刷新bean
這篇文章主要為大家介紹了詳解spring項(xiàng)目中如何動(dòng)態(tài)刷新bean,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(45)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07
Java JVM字節(jié)碼指令集總結(jié)整理與介紹
本節(jié)將會(huì)著重介紹一下JVM中的指令集、Java是如何跨平臺(tái)的、JVM指令集參考手冊(cè)等內(nèi)容。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

