이 기사는 Java가 이미지를 참조를 위해 문자 도면으로 변환 할 수있는 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
public class imageProcesser {private static final char [] charset1 = { 'm', '8', 'v', '|': ','. ',' '}; // 기본 문자 자료 SET Private char [] charset; // 문자 그리기 자료 세트 개인 문자열 imgstring = ""; // 변환 된 문자열 저장 // public imageProcesser 구성 (char [] charset) {this.charset = charset; } // public imageProcesser () {this.charset = charset1; } public String getImgstring () {return imgstring; } /*그래픽 파일을 문자로 변환하여 문자열을 그려 문자열을 그립니다. } public imageProcesser tobitMapConvert (파일 ImageFile) {StringBuffer sb = new StringBuffer (); if (! imagefile.exists ()) {// 읽기 파일이 존재하지 않으면 프로그램 System.out.println ( "파일이 존재하지 않습니다!"); System.exit (1); } 색상; {bufferedImage buff = imageio.Read (imageFile); // bufferedImage stream buff = compressImage (buff)와 같은 이미지 파일을로드합니다. int bitmaph = buff.getheight (); int bitmapw = buff.getWidth (); // 이미지의 픽셀을 점차적으로 스캔하고, RGB 값을 읽고, 평균 값을 취하고, 숯으로부터 해당 문자 자료를 가져 와서 (int y = 0; y <bitmaph; y ++) {for (int x = 0; x ++) {int rgb = buff.getrgb (x, y); 색상 = 새로운 색상 (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 (); 이것을 반환하십시오; } /* 이미지 파일 전처리 : 이미지를 가장 긴 가장자리로 100px* / private bufferedimage compressimage (bufferedImage srcimg)로 압축합니다. {int h = srcimg.getheight (); int w = srcimg.getWidth (); if (math.max (h, w) <= 100) 반환 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 ()); 그래픽 g = smallimg.getGraphics (); G.DrawImage (SRCIMG, 0,0, New_W, New_H, NULL); g.dispose (); 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 (문자열 srcfile, String tragetfile) {file folder = new File (TragetFile); // 이미지 파일을 생성하는 폴더 srcfolder = new File (srcfile); if (! folder.exists () ||! 폴더.isdirectory ()) folder.mkdirs (); ImageProcesser 프로세서 = 새 ImageProcesser (); file [] filElist = srcfolder.listfiles (); for (int i = 0; i <fileList.length; i ++) {if (! filElist [i] .isfile ()) 계속; Processor.tobitMapConvert (FilElist [i]); Processor.saveastxt (TragetFile+"/"+(i+1)+". txt"); System.out.println (filElist [i] .getName ()+"is converted!"); } system.out.println ( "모든 img이 변환되었습니다!"); }}보기를 클릭하십시오 : 참조 링크.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.