Les exemples de cet article partagent avec vous le code spécifique pour Java pour convertir les images en dessins de caractères pour votre référence. Le contenu spécifique est le suivant
classe publique ImageProcesser {private static final char [] charset1 = {'m', '8', 'v', '|': ','. ',' '}; // Matériau de caractère par défaut Définit le charme privé [] Charset; // Dessin de caractères Définir la chaîne privée imgString = ""; // Stockage des chaînes converties // Constructing public ImageProcesser (char [] charset) {this.charset = charset; } // Construction de l'image publique Public () {this.charSet = charSet1; } public String getImgString () {return imgString; } / * Convertir le fichier graphique en un caractère pour dessiner une chaîne * / public ImageProcesser tobitmapConvert (String ImagePath) {return tobitmapConvert (nouveau fichier (imagePath)); } public ImageProcesser tobitmapConvert (fichier imagefile) {stringBuffer sb = new StringBuffer (); if (! ImageFile.exists ()) {// Lorsque le fichier de lecture n'existe pas, terminez le programme System.out.println ("Le fichier n'existe pas!"); System.exit (1); } Couleur couleur; try {bufferedImage buff = imageo.read (imagefile); // Chargez le fichier image tel que BufferedImage Stream Buff = CompressImage (buff); int bitmaph = buff.getheight (); int bitmapw = buff.getWidth (); // Scannez progressivement les pixels de l'image, lisez la valeur RVB, prenez sa valeur moyenne et obtenez le matériau de caractères correspondant du charset et chargez-le dans SB pour (int y = 0; y <bitmaph; y ++) {for (int x = 0; x <bitmapw; x ++) {int rgb = buff.getrgb (x, y); color = nouvelle couleur (RVB); 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 (); retourner ceci; } / * Image Fichier Prétraitement: Compressez l'image au bord le plus long à 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 stemImg = new BufferedImage (new_w, new_h, srcimg.getType ()); Graphiques g = smulMg.getGraphics (); G.DrawImage (srcimg, 0,0, new_w, new_h, null); g.dispose (); retourner smaltimg; }! for (int i = 0; i <imgString.length (); i ++) {out.print (imgString.Charat (i)); } out.close (); } catch (ioException ex) {ex.printStackTrace (); }} / * Fichier d'image par lots * / public static void batchImgFile (String srcfile, String tragetfile) {Fichier Folder = new File (tragetFile); // dossier qui génère le fichier image srcfolder = nouveau fichier (srcfile); if (! Folder.Exists () ||! Folder.isDirectory ()) Folder.mkDirs (); ImageProcesser processeur = new ImageProcesser (); File [] fileList = srcfolder.listFiles (); pour (int i = 0; i <fileList.length; i ++) {if (! FileList [i] .isfile ()) continue; processor.tobitmapconvert (FileList [i]); processeur.saveastxt (tragetfile + "/" + (i + 1) + ". txt"); System.out.println (FileList [i] .getName () + "est converti!"); } System.out.println ("Tous les img ont été convertis!"); }}Cliquez pour afficher: lien de référence.
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.