Flask接口签名sign原理与实例代码浅析
290
2023-02-10
java实现图像转码为字符画的方法
本文实例为大家分享了java实现图像转码为字符画的具体代码,供大家参考,具体内容如下
public class ImageProcesser {
private static final char[] charset1 = {'M','8','V','|',':','.',' '}; //默认字符素材集
private char[] charset; //字符画素材集
bZgcWuYprivate String imgString = ""; //储存转化后的字符串
//使用指定字符集构造
public ImageProcesser(char[] charset){
this.charset = charset;
}
//使用默认字符集构造
public ImageProcesser(){
this.charset = charset1;
}
public String getImgString(){
return imgString;
}
/*将图形文件转化为字符画字符串*/
public ImageProcesser toBitmapConvert(String imagepath){
return toBitmapConvert(new File(imagepath));
}
public ImageProcesser toBitmapConvert(File imageFile){
StringBuffer sb = new StringBuffer();
if(!imageFile.exists()){ //当读取的文件不存在时,结束程序
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 for(int x=0; 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 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);bZgcWuY //生成图片的文件夹 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 if(!filelist[i].isFile()) continue; processer.toBitmapConvert(filelist[i]); processer.saveAsTxt(tragetfibZgcWuYle+"/"+(i+1)+".txt"); System.out.println(filelist[i].getName()+" is converted!"); } System.out.println("All img were converted!"); } } 点击查看:参考链接。
for(int x=0; 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 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);bZgcWuY //生成图片的文件夹 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 if(!filelist[i].isFile()) continue; processer.toBitmapConvert(filelist[i]); processer.saveAsTxt(tragetfibZgcWuYle+"/"+(i+1)+".txt"); System.out.println(filelist[i].getName()+" is converted!"); } System.out.println("All img were converted!"); } } 点击查看:参考链接。
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 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);bZgcWuY //生成图片的文件夹 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 if(!filelist[i].isFile()) continue; processer.toBitmapConvert(filelist[i]); processer.saveAsTxt(tragetfibZgcWuYle+"/"+(i+1)+".txt"); System.out.println(filelist[i].getName()+" is converted!"); } System.out.println("All img were converted!"); } } 点击查看:参考链接。
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);bZgcWuY //生成图片的文件夹
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 if(!filelist[i].isFile()) continue; processer.toBitmapConvert(filelist[i]); processer.saveAsTxt(tragetfibZgcWuYle+"/"+(i+1)+".txt"); System.out.println(filelist[i].getName()+" is converted!"); } System.out.println("All img were converted!"); } } 点击查看:参考链接。
if(!filelist[i].isFile())
continue;
processer.toBitmapConvert(filelist[i]);
processer.saveAsTxt(tragetfibZgcWuYle+"/"+(i+1)+".txt");
System.out.println(filelist[i].getName()+" is converted!");
}
System.out.println("All img were converted!");
}
}
点击查看:参考链接。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~