1. 程式人生 > >java 生產文字圖片邊緣有鋸齒狀

java 生產文字圖片邊緣有鋸齒狀

java生產文字圖片時如果文字邊緣有鋸齒狀

可以新增

gd.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); 

消除鋸齒狀
public static BufferedImage drawTranslucentStringPic(int width, int height, Integer fontHeight,String drawStr) { 
		
        try {  
            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            Graphics2D gd = buffImg.createGraphics();  
            //設定透明  start  
            buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);  
            gd=buffImg.createGraphics();  
            //設定透明  end  
            gd.setFont(new Font("方正舒體", Font.PLAIN, fontHeight)); //設定字型  
            gd.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); //消除鋸齒狀
            gd.setColor(Color.white); //設定顏色  
            //gd.drawRect(0, 0, width - 1, height - 1); //畫邊框  
            gd.drawString(drawStr, width/2-fontHeight*drawStr.length()/2,fontHeight); //輸出文字(中文橫向居中)  
            return buffImg;  
        } catch (Exception e) {  
            return null;  
        }  
    }  
完整工程