Os exemplos deste artigo compartilham com você o código específico para Java para converter imagens em desenhos de personagens para sua referência. O conteúdo específico é o seguinte
classe pública ImageProcesser {private estático final char [] charset1 = {'m', '8', 'v', '|': ','. ',' '}; // Material de caracteres padrão conjunto privado char [] charset; // Material de desenho de caracteres Definir string privada imgstring = ""; // armazenando strings convertido // Construindo o Public ImageProcesser (char [] charset) {this.charset = charset; } // Construindo public imageProcesser () {this.charset = charset1; } public string getImgString () {return iMgString; } /*Converta o arquivo gráfico em um caractere para desenhar uma string* / public ImageProcesser TobitMapConvert (String ImagePath) {return TobbitmapConvert (new File (ImagePath)); } public ImageProcesser TobitMapConvert (arquivo imagefile) {StringBuffer sb = new StringBuffer (); if (! imagefile.exists ()) {// Quando o arquivo de leitura não existe, encerre o programa System.out.println ("O arquivo não existe!"); System.Exit (1); } Cor cor; tente {bufferImage buff = imageio.read (imagefile); // Carregar o arquivo de imagem, como BufferImage Stream Buff = Compressimage (Buff); int bitmaph = buff.getHeight (); int bitmapw = buff.getwidth (); // Digitam os pixels da imagem progressivamente, leia o valor RGB, pegue seu valor médio e obtenha o material de caractere correspondente do charset e carregue -o em sb para (int y = 0; y <bitmaph; y ++) {para (int x = 0; x <bitmapw; x ++) {int rgb = buff.getrgb (x, x, x ++) {int rgb = buff.getrgb (x, x, x ++) {int rgb = buff.getrgb (x, x, x ++) {int rgb = buff.getrgb (x, x, x ++) {int rgb = buff.getrgb (x, x, x ++) {int rgb = buff.getrgb (x, x, x+/) cor = nova cor (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 (); devolver isso; } /* Arquivo de imagem Pré -processamento: Compressa a imagem na borda mais longa a 100px* / private bufferImage compressimage (bufferImage srcimg) {int h = srcimg.getheight (); int w = srcimg.getwidth (); if (math.max (h, w) <= 100) retorna 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; } BufferImage smallImg = new bufferImage (new_w, new_h, srcimg.gettype ()); Gráficos g = smallImg.getgraphics (); G.Drawimage (srcimg, 0,0, new_w, new_h, null); G.Dispose (); retornar SmallImg; } /*Salvar a string como um arquivo .txt* / public void saveastxt (string filename) {try {printWriter out = new PrintWriter (new BufferWriter (new FileWriter (nome do arquivo))); for (int i = 0; i <imgstring.length (); i ++) {out.print (imgstring.charat (i)); } out.close (); } catch (ioexception ex) {ex.printStackTrace (); }} /*Arquivo de imagem em lote* / public static void BatchImgfile (String srcfile, String tragetfile) {pasta de arquivo = new File (TRAGETFILE); // pasta que gera o arquivo de imagem srcfolder = novo arquivo (srcfile); if (! pasta.exists () ||! pasta.isdirectory ()) pasta.mkdirs (); ImageProcesser Processor = new ImageProcesser (); Arquivo [] filelist = srcfolder.listfiles (); for (int i = 0; i <fileList.length; i ++) {if (! FILELIST [i] .isfile ()) continue; Processor.TobitMapConvert (FILELIST [I]); Processor.sAveastxt (tragetfile+"/"+(i+1)+". txt"); System.out.println (filelist [i] .getName ()+"é convertido!"); } System.out.println ("Todos os IMG foram convertidos!"); }}Clique para visualizar: link de referência.
O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.