├── data ├── 0 │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ └── 9.png ├── 1 │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ └── 9.png ├── 2 │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ └── 9.png ├── 3 │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ └── 9.png └── 4 │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ └── 9.png ├── fonts └── Siyamrupali.ttf ├── src ├── long-text-gneration.png ├── TextToImage.java └── BanglaTextToImage.java ├── corpus └── bn-data-map.txt ├── MIT-LICENSE.txt └── README.md /data/0/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/0/1.png -------------------------------------------------------------------------------- /data/0/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/0/2.png -------------------------------------------------------------------------------- /data/0/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/0/3.png -------------------------------------------------------------------------------- /data/0/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/0/4.png -------------------------------------------------------------------------------- /data/0/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/0/5.png -------------------------------------------------------------------------------- /data/0/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/0/6.png -------------------------------------------------------------------------------- /data/0/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/0/7.png -------------------------------------------------------------------------------- /data/0/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/0/8.png -------------------------------------------------------------------------------- /data/0/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/0/9.png -------------------------------------------------------------------------------- /data/1/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/1/1.png -------------------------------------------------------------------------------- /data/1/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/1/2.png -------------------------------------------------------------------------------- /data/1/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/1/3.png -------------------------------------------------------------------------------- /data/1/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/1/4.png -------------------------------------------------------------------------------- /data/1/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/1/5.png -------------------------------------------------------------------------------- /data/1/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/1/6.png -------------------------------------------------------------------------------- /data/1/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/1/7.png -------------------------------------------------------------------------------- /data/1/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/1/8.png -------------------------------------------------------------------------------- /data/1/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/1/9.png -------------------------------------------------------------------------------- /data/2/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/2/1.png -------------------------------------------------------------------------------- /data/2/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/2/2.png -------------------------------------------------------------------------------- /data/2/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/2/3.png -------------------------------------------------------------------------------- /data/2/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/2/4.png -------------------------------------------------------------------------------- /data/2/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/2/5.png -------------------------------------------------------------------------------- /data/2/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/2/6.png -------------------------------------------------------------------------------- /data/2/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/2/7.png -------------------------------------------------------------------------------- /data/2/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/2/8.png -------------------------------------------------------------------------------- /data/2/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/2/9.png -------------------------------------------------------------------------------- /data/3/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/3/1.png -------------------------------------------------------------------------------- /data/3/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/3/2.png -------------------------------------------------------------------------------- /data/3/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/3/3.png -------------------------------------------------------------------------------- /data/3/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/3/4.png -------------------------------------------------------------------------------- /data/3/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/3/5.png -------------------------------------------------------------------------------- /data/3/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/3/6.png -------------------------------------------------------------------------------- /data/3/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/3/7.png -------------------------------------------------------------------------------- /data/3/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/3/8.png -------------------------------------------------------------------------------- /data/3/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/3/9.png -------------------------------------------------------------------------------- /data/4/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/4/1.png -------------------------------------------------------------------------------- /data/4/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/4/2.png -------------------------------------------------------------------------------- /data/4/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/4/3.png -------------------------------------------------------------------------------- /data/4/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/4/4.png -------------------------------------------------------------------------------- /data/4/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/4/5.png -------------------------------------------------------------------------------- /data/4/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/4/6.png -------------------------------------------------------------------------------- /data/4/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/4/7.png -------------------------------------------------------------------------------- /data/4/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/4/8.png -------------------------------------------------------------------------------- /data/4/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/data/4/9.png -------------------------------------------------------------------------------- /fonts/Siyamrupali.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/fonts/Siyamrupali.ttf -------------------------------------------------------------------------------- /src/long-text-gneration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/menon92/BanglaText2Image/HEAD/src/long-text-gneration.png -------------------------------------------------------------------------------- /corpus/bn-data-map.txt: -------------------------------------------------------------------------------- 1 | দৃ 1 2 | ষ্টি 1 3 | ভ 2 4 | ঙ্গি 1 5 | কে 1 6 | ম 2 7 | ন 3 8 | হ 1 9 | তে 3 10 | পা 1 11 | রে 1 12 | -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2022 Scott Chacon and others 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BanglaText2Image 2 | 3 | Convert any bangla text to image. This tool will help to generate synthetic data for bangla OCR training 4 | 5 | ## Example data sample 6 | | Text | Image | 7 | | ----------- | ----------- | 8 | | ঙ্গি |![](./data/3/7.png) | 9 | | ষ্টি |![](./data/1/7.png)| 10 | | দৃষ্টিভঙ্গি কেমন হতে পারে |![](./src/long-text-gneration.png)| 11 | 12 | 13 | ## Dependency 14 | ```sh 15 | sudo apt install default-jre 16 | sudo apt install default-jdk 17 | ``` 18 | 19 | ## Project structure 20 | ```sh 21 | ├── corpus 22 | │   └── bn-data-map.txt # contain the text which need to convert to image 23 | ├── data # unique class based data 24 | │   ├── 0 25 | │   ├── 1 26 | │   ├── 2 27 | │   ├── 3 28 | │   └── 4 29 | ├── fonts # bangla fonts 30 | │   └── Siyamrupali.ttf 31 | ├── README.md 32 | └── src 33 | ├── BanglaTextToImage.java 34 | └── textToImage.java 35 | ``` 36 | 37 | ## How to run 38 | আপনি যদি ক্লাস ভিত্তিক ডাটা সেট বানাতে চান তাহলে এই স্ক্রিপ্ট বাবহারেন করতেন পারেন। 39 | যেমন আপনি একটা টেক্সট ফাইলে সব বাংলা বর্ণমালা রেখে প্রতিটা বর্ণমালাকে একটা ক্লাস ধরে ডাটা জেনারেট করতে পারেন। 40 | সেই ক্ষেত্রে প্রতিটা ক্লাস আর জন্য ফোল্ডারে `data/{0..n}` ডাটা জমা হবে 41 | ```sh 42 | javac BanglaTextToImage.java 43 | java BanglaTextToImage 44 | ``` 45 | 46 | ## Configs 47 | Some of the config variable you may need to update based on your need 48 | ```java 49 | public static String BN_DATA_MAP_PATH = "../corpus/bn-data-map.txt"; 50 | // you can put multiple fonts here 51 | public static String FONTS_DIR = "../fonts"; 52 | // shere your images will be saved 53 | public static String DATA_SAVED_DIR = "../data"; 54 | 55 | // set font size and font color 56 | public static int[] FONT_SIZES = {30, 40, 50}; 57 | public static int[] BG_COLOR = {255, 230, 205}; 58 | 59 | public static int IMAGE_NAME_COUNTER = 1; 60 | public static int NUMBER_OF_CLASS = 11; // number of class you have 61 | // 5 line will be taken from `bn-data-map.txt` 62 | public static int CHUNK_TO_TAKE = 5; // CHUNK_TO_TAKE must be <= chunks.lenght() 63 | ``` 64 | 65 | ## Sentence to image 66 | Convert bangla sentece to Image 67 | 68 | এই স্ক্রিপ্টে যেকোনো বাংলা লেখা দিলে সেটাকে ইমেজে কনভার্ট করে দিবে। 69 | ```sh 70 | javac TextToImage.java 71 | java TextToImage 72 | ``` 73 | This will output an image like 74 | 75 | ![](./src/long-text-gneration.png) 76 | 77 | ## Citation 78 | ```sh 79 | @misc{banglatext2image, 80 | title={Bnagla synthetic text image generation}, 81 | author={Mehadi Hasan Menon}, 82 | howpublished={https://github.com/menon92/BanglaText2Image}, 83 | year={2022} 84 | } 85 | ``` 86 | -------------------------------------------------------------------------------- /src/TextToImage.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Font; 3 | import java.awt.FontMetrics; 4 | import java.awt.Graphics2D; 5 | import java.awt.RenderingHints; 6 | import java.awt.image.BufferedImage; 7 | import java.io.File; 8 | import java.io.IOException; 9 | import javax.imageio.ImageIO; 10 | 11 | public class TextToImage { 12 | 13 | public static void main(String[] args) { 14 | 15 | try { 16 | textToImage("দৃষ্টিভঙ্গি কেমন হতে পারে", 30); 17 | } catch (IOException e) { 18 | // TODO Auto-generated catch block 19 | e.printStackTrace(); 20 | } 21 | 22 | } 23 | 24 | 25 | public static void textToImage(String text, float size) throws IOException { 26 | //Derives font to new specified size, can be removed if not necessary. 27 | 28 | BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); 29 | try{ 30 | Graphics2D g2d = img.createGraphics(); 31 | // Font font = new Font("Arial", Font.PLAIN, 48); 32 | Font fnt=Font.createFont(Font.TRUETYPE_FONT, new File("../fonts/Siyamrupali.ttf")); 33 | fnt = fnt.deriveFont(Font.PLAIN,size); 34 | System.out.println(fnt.getName()); 35 | System.out.println(fnt.getNumGlyphs()); 36 | g2d.setFont(fnt); 37 | FontMetrics fm = g2d.getFontMetrics(); 38 | int width = fm.stringWidth(text); 39 | int height = fm.getHeight(); 40 | g2d.dispose(); 41 | 42 | img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 43 | 44 | g2d = img.createGraphics(); 45 | g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 46 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 47 | g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 48 | g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 49 | g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 50 | g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 51 | g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 52 | g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 53 | 54 | g2d.setFont(fnt); 55 | fm = g2d.getFontMetrics(); 56 | // g2d.setBackground(Color.WHITE); 57 | // g2d.setColor(Color.BLUE); 58 | g2d.setColor(new Color(255, 255, 255)); 59 | g2d.fillRect(0, 0, width, height); 60 | g2d.setColor(Color.BLACK); 61 | 62 | g2d.drawString(text, 0, fm.getAscent()); 63 | g2d.dispose(); 64 | 65 | try { 66 | ImageIO.write(img, "png", new File("2.png")); 67 | } catch (IOException ex) { 68 | ex.printStackTrace(); 69 | } 70 | 71 | }catch(Exception e){ 72 | e.printStackTrace(); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/BanglaTextToImage.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Font; 3 | import java.awt.FontMetrics; 4 | import java.awt.Graphics2D; 5 | import java.awt.List; 6 | import java.awt.RenderingHints; 7 | import java.awt.image.BufferedImage; 8 | import java.awt.print.Printable; 9 | import java.io.BufferedReader; 10 | import java.io.File; 11 | import java.io.FileReader; 12 | import java.io.IOException; 13 | import java.util.ArrayList; 14 | 15 | import javax.imageio.ImageIO; 16 | 17 | 18 | public class BanglaTextToImage { 19 | public static String BN_DATA_MAP_PATH = "../corpus/bn-data-map.txt"; 20 | public static String FONTS_DIR = "../fonts"; 21 | public static String DATA_SAVED_DIR = "../data"; 22 | 23 | public static int[] FONT_SIZES = {30, 40, 50}; 24 | public static int[] BG_COLOR = {255, 230, 205}; 25 | 26 | public static int IMAGE_NAME_COUNTER = 1; 27 | public static int NUMBER_OF_CLASS = 11; // number of class you have 28 | public static int CHUNK_TO_TAKE = 5; // CHUNK_TO_TAKE must be <= chunks.lenght() 29 | 30 | public static void main(String[] args) { 31 | createFolders(); 32 | bnMakeDateset(); 33 | System.out.println("All data created."); 34 | } 35 | 36 | public static void bnMakeDateset() { 37 | ArrayList chunks = getChunks(); 38 | System.out.println(chunks); 39 | 40 | for(int i = 0; i < CHUNK_TO_TAKE; i++) { 41 | String chunk = chunks.get(i); 42 | File folderFonts = new File(FONTS_DIR); 43 | File[] listOfFonts = folderFonts.listFiles(); 44 | int classFolderName = i; 45 | 46 | for (File fontFile : listOfFonts) { 47 | if (fontFile.isFile()) { 48 | // System.out.println("File Font name:" + fontFile.getName()); 49 | // System.out.println("font abs path: " + fontFile.getAbsolutePath()); 50 | String fontAbsPath = fontFile.getAbsolutePath(); 51 | for (int fs : FONT_SIZES) { 52 | for (int bgColor : BG_COLOR) { 53 | bnTextToImage(chunk, fontAbsPath, fs, bgColor, classFolderName); 54 | System.out.printf("Write[Ok]:%4d, fs-%2d, bg-color:%3d, folder:%4d, Font:%s\n", IMAGE_NAME_COUNTER, fs, bgColor, i, fontFile.getName()); 55 | } 56 | } 57 | } else if (fontFile.isDirectory()) { 58 | System.out.println("Directory " + fontFile.getName()); 59 | } 60 | } 61 | // re-init IMAGE_NAME_COUNTER 62 | IMAGE_NAME_COUNTER = 1; 63 | } 64 | } 65 | 66 | public static void createFolders() { 67 | // create all folder for all class 68 | for(int i = 0; i < NUMBER_OF_CLASS; i++) { 69 | File classNameFile = new File(DATA_SAVED_DIR + "/" + i); 70 | if(!classNameFile.exists()) { 71 | if(classNameFile.mkdirs()) { 72 | System.out.printf("folder %d created.\n", i); 73 | } 74 | else { 75 | System.out.printf("folder %d creation failed.\n", i); 76 | } 77 | } 78 | } 79 | } 80 | 81 | public static ArrayList getChunks() { 82 | ArrayList chunks = new ArrayList(); 83 | // read & load map file 84 | try { 85 | BufferedReader br = new BufferedReader(new FileReader(BN_DATA_MAP_PATH)); 86 | 87 | for(String line; (line = br.readLine()) != null; ) { 88 | // System.out.println("line is: " + line); 89 | String tempString[] = line.split(" "); 90 | String chunk = tempString[0].trim(); 91 | // System.out.println("chunk is: " + chunk); 92 | chunks.add(chunk); 93 | } 94 | br.close(); 95 | // System.out.println(chunks); 96 | } catch (IOException ex) { 97 | ex.printStackTrace(); 98 | } 99 | 100 | return chunks; 101 | } 102 | 103 | public static void bnTextToImage(String text, String fontPath, float size, int bgColor, int classFolderName) { 104 | //Derives font to new specified size, can be removed if not necessary. 105 | 106 | BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); 107 | try{ 108 | Graphics2D g2d = img.createGraphics(); 109 | // Font font = new Font("Arial", Font.PLAIN, 48); 110 | Font fnt=Font.createFont(Font.TRUETYPE_FONT, new File(fontPath)); 111 | fnt = fnt.deriveFont(Font.PLAIN,size); 112 | // System.out.println(fnt.getName()); 113 | // System.out.println(fnt.getNumGlyphs()); 114 | g2d.setFont(fnt); 115 | FontMetrics fm = g2d.getFontMetrics(); 116 | int width = fm.stringWidth(text); 117 | int height = fm.getHeight(); 118 | g2d.dispose(); 119 | 120 | img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 121 | 122 | g2d = img.createGraphics(); 123 | g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 124 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 125 | g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 126 | g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); 127 | g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 128 | g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 129 | g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 130 | g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); 131 | 132 | g2d.setFont(fnt); 133 | fm = g2d.getFontMetrics(); 134 | g2d.setColor(new Color(bgColor, bgColor, bgColor)); 135 | g2d.fillRect(0, 0, width, height); 136 | g2d.setColor(Color.BLACK); 137 | 138 | g2d.drawString(text, 0, fm.getAscent()); 139 | g2d.dispose(); 140 | 141 | // write image to disk 142 | try { 143 | // ImageIO.write(img, "png", new File("2.png")); 144 | ImageIO.write(img, "png", new File(DATA_SAVED_DIR + "/" + classFolderName + "/" + IMAGE_NAME_COUNTER + ".png")); 145 | IMAGE_NAME_COUNTER += 1; 146 | } catch (IOException ex) { 147 | ex.printStackTrace(); 148 | } 149 | 150 | } catch(Exception e){ 151 | e.printStackTrace(); 152 | } 153 | } 154 | } 155 | --------------------------------------------------------------------------------