java.awt.Graphics2D 生成图片--个人章的方法
生活随笔
收集整理的這篇文章主要介紹了
java.awt.Graphics2D 生成图片--个人章的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# java.awt 畫矩形踩坑?
?
?
? ? 本來使用Graphics2D的drawRect()方法畫圖的,但是出現了下邊框過粗,不得已 使用了畫線的方法,四個邊框,單獨劃線,
這里要注意坐標軸和線的寬度--高度height
? 一下是代碼
?
/*** 創建私人章的方法* @param drawStrName 輸入的姓名 1- 18 位* @param filePathAndFileName E:\test2\grp.png* @param drawStrSpecial -- CN310110HAJ403CFD383*/public static void createPersonalSeal(String drawStrName,String filePathAndFileName,String drawStrSpecial) {String drawStrSecondLine ="";String drawStrFirstLine ="";int length = drawStrName.length();Map<Integer,SignImage.Params> weidthHeightMap = getWeidthHeight(length);Params params = weidthHeightMap.get(length);int width = params.getWidth() *2;int height = params.getHeight() *2;int fontHeight = 12 * 2;int fontHeightSpecial = 12;int frameLineWhiteSize = 8;int newWidth = width;int newHeight = height + frameLineWhiteSize + fontHeightSpecial;int frameLineSize = 5;BufferedImage buffImg = new BufferedImage(newWidth, newHeight , BufferedImage.TYPE_INT_RGB);Graphics2D gd = buffImg.createGraphics();//設置透明 startbuffImg = gd.getDeviceConfiguration().createCompatibleImage(newWidth, newHeight, Transparency.TRANSLUCENT);gd = buffImg.createGraphics();//設置透明 end//設置字體Font fontSong = new Font("宋體", Font.BOLD, fontHeight);gd.setFont(fontSong);//類對象,可以獲得某個字體的高度,以及字符串的寬度FontMetrics fmSong = gd.getFontMetrics(fontSong);Stroke oldStroke = gd.getStroke();//設置顏色//設置線寬為5.0gd.setStroke(new BasicStroke(5.0f));//畫邊框 // gd.drawRect(0, 0, width - 1 , height - fontHeightSpecial -8);// 畫一個矩形gd.fillRect(0, 0, newWidth, newHeight);// 去除鋸齒(當設置的字體過大的時候,會出現鋸齒)gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);gd.setColor(Color.black);gd.fillRect(0, 0, frameLineSize, height);gd.fillRect(0, 0, width , frameLineSize);gd.fillRect(0, height , width, frameLineSize);gd.fillRect(width - frameLineSize, 0, width, height);int songFontwidthFirst = 0;int songFontwidthSecond = 0;int arialFontheightFirst = 0;int arialFontheightSecond = 0;boolean largeFlag = length >3;if(largeFlag){int subStartLength = length % 2;if(subStartLength ==1){subStartLength = (length -1)/2;}else{subStartLength = length / 2;}drawStrFirstLine = drawStrName.substring(0,subStartLength);drawStrSecondLine = drawStrName.substring(subStartLength,length);songFontwidthFirst = fmSong.stringWidth(drawStrFirstLine);songFontwidthSecond = fmSong.stringWidth(drawStrSecondLine);arialFontheightFirst = fmSong.getHeight();arialFontheightSecond = fmSong.getHeight();}else{drawStrFirstLine = drawStrName;songFontwidthFirst = fmSong.stringWidth(drawStrFirstLine);arialFontheightFirst = fmSong.getHeight();}if(largeFlag){//先畫第一行,輸出文字(中文橫向居中)gd.drawString(drawStrFirstLine, (width - songFontwidthFirst) /2,(newHeight - arialFontheightFirst *2 -frameLineSize)/2 + 10);//畫第二行,輸出文字(中文橫向居中)gd.drawString(drawStrSecondLine, (width - songFontwidthSecond) /2,(newHeight - arialFontheightFirst *2 - frameLineSize )/2 + arialFontheightFirst + 20);}else{gd.drawString(drawStrFirstLine, (width - songFontwidthFirst) /2, newHeight /2 );}Font arialFont = new Font("Arial", 2, fontHeightSpecial);gd.setFont(arialFont);//類對象,可以獲得某個字體的高度,以及字符串的寬度FontMetrics fm = gd.getFontMetrics(arialFont);int arialFontwidth = fm.stringWidth(drawStrSpecial);gd.setColor(Color.BLACK);gd.drawString(drawStrSpecial, (width - arialFontwidth) /2, newHeight);// 透明度設置結束gd.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));gd.dispose();try {ImageIO.write(buffImg, "png", new File(filePathAndFileName));} catch (IOException e) {e.printStackTrace();}buffImg.flush();}public static Map<Integer,SignImage.Params> getWeidthHeight(int length){int width =0;int height = 0;switch (length){case 1:width = 93;height = 44 ;break;case 2:width = 93;height = 44 ;break;case 3:width = 93;height = 44 ;break;case 4:width = 93;height = 44 ;break;case 5:width = 93;height = 44 ;break;case 6:width = 93;height = 44 ;break;case 7:width = 107;height = 50;break;case 8:width = 107;height = 50;break;case 9:width = 119;height = 50;break;case 10:width = 119;height = 50;break;case 11:width = 136;height = 58;break;case 12:width = 136;height = 58;break;case 13:width = 136;height = 58;break;case 14:width = 136;height = 58;break;case 15:width = 136;height = 58;break;case 16:width = 136;height = 58;break;case 17:width = 136;height = 58;break;case 18:width = 136;height = 58;break;default: throw new RuntimeException("姓名字數不符合要求");}Map<Integer,SignImage.Params> reParentMap = com.google.common.collect.Maps.newHashMap();SignImage.Params returnParams = new SignImage.Params();returnParams.setHeight(height);returnParams.setWidth(width);reParentMap.put(length,returnParams);return reParentMap;}@Data@NoArgsConstructor@AllArgsConstructor@ToString@Builder@Accessors(chain = true)@EqualsAndHashCodestatic class Params {int width;int height;}public static void main(String[] args) {SignImage.createPersonalSeal("張大牛","E:\\test2\\grp.png","CN310110HAJ403CFD383");}一下為參考的資源;
private static void transparent() throws IOException {int width = 400;int height = 300; // 創建BufferedImage對象BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 獲取Graphics2DGraphics2D g2d = image.createGraphics();// ---------- 增加下面的代碼使得背景透明 ----------------- // image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); // g2d.dispose(); // g2d = image.createGraphics(); // ---------- 背景透明代碼結束 ----------------- // g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.5f));// 畫一個矩形g2d.fillRect(0, 0, width, height);// 去除鋸齒(當設置的字體過大的時候,會出現鋸齒)g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);Color color = new Color(255, 0, 0);g2d.setColor(color);g2d.fillRect(0, 0, 8, height);g2d.fillRect(0, 0, width, 8);g2d.fillRect(0, height - 8, width, height);g2d.fillRect(width - 8, 0, width, height); // 畫圖g2d.setColor(new Color(255, 0, 0));g2d.setStroke(new BasicStroke(10));g2d.drawString("name", 100, 100); //釋放對象g2d.dispose();BufferedImage bufferedImage = alphaProcess(image); // 保存文件ImageIO.write(bufferedImage, "png", new File("d:/test/test.png"));bufferedImage.flush();}?
總結
以上是生活随笔為你收集整理的java.awt.Graphics2D 生成图片--个人章的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HashTable 和HashMap区别
- 下一篇: 如何解决微服务架构中的雪崩问题?