JAVA------20.圖片中插文字和圖片
阿新 • • 發佈:2017-06-07
dap 圖片 logs test http 合成 over cal ray
import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.net.URL; import javax.imageio.ImageIO; public class PictureDemo { /** * 將圖片進行合成 * @param bigPath 主圖圖片路徑 * @paramsmallPath 商品圖片路徑 * @param erweimaPath 二維碼圖片路徑 */ public static final void overlapImage(String bigPath, String smallPath,String erweimaPath) { try { BufferedImage big = ImageIO.read(new File(bigPath)); URL url = new URL("http://mjj.dapail.com/app/moneyUrl.png"); BufferedImage small= ImageIO.read(url.openStream()); BufferedImage erweima = ImageIO.read(new File(erweimaPath)); /*int width=2015; int height=1136;*/ int width=400; int height=600; Image image=big.getScaledInstance(width, height, Image.SCALE_SMOOTH); BufferedImage bufferedImage2=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics2D g = bufferedImage2.createGraphics(); /* int x = 707; int y = 268; int x1 = 684; int y1 = 245;*/ int x = 200; int y = 250; int x1 = 200; int y1 = 250; g.drawImage(image, 0, 0,null); // g.drawImage(small,x1-200, y1-250, 400, 200, null); //紅包 g.drawImage(small, x, y+100, 50, 50, null); //g.drawImage(erweima, x1-575, y1+100, 596, 596, null); //二維碼 g.drawImage(erweima, x1-100, y1+80, 200, 200, null); Font font=new Font("黑體",Font.PLAIN , 25); g.setFont(font); g.setPaint(Color.DARK_GRAY); int numWidth =x-70; int numHright=y+50; int num=0; // g.drawString("商品名稱:" , numWidth,numHright); //g.drawString("測試棋牌室" , numWidth,numHright); String name="測試測試測試測試試試"; if (name.length()==2) { numWidth =x-20; } else if(name.length()==3) { numWidth =x-35; }else if(name.length()==4) { numWidth =x-50; }else if(name.length()==5) { numWidth =x-65; }else if(name.length()==6) { numWidth =x-75; }else if(name.length()==7) { numWidth =x-90; }else if(name.length()==8) { numWidth =x-105; } else if(name.length()==9) { numWidth =x-115; } else if(name.length()==10) { numWidth =x-130; } else if(name.length()==11) { numWidth =x-145; } else if(name.length()==12) { numWidth =x-160; } g.drawString(name , numWidth,numHright); /* g.setPaint(Color.DARK_GRAY); Font font1=new Font("宋體",Font.BOLD , 15); g.setFont(font1); numWidth=numWidth-25; g.drawString("江蘇美聯信息科技有限公司" , numWidth,numHright+280); */ /*num += 50; Font font2=new Font("宋體",Font.PLAIN , 40); g.setFont(font2); g.setPaint(Color.DARK_GRAY); g.drawString("原產地:", numWidth, numHright+num); num += 50; g.drawString("配送方式:",numWidth, numHright+num); */ g.dispose(); ImageIO.write(bufferedImage2, "jpg", new File("E:/20170429/2017410備份/TestImage/4.jpg")); } catch (Exception e) { e.printStackTrace(); } } public static final void main(String[] args) { overlapImage("E:/20170429/2017410備份/TestImage/bg.png", "E:/20170429/2017410備份/TestImage/2.png","E:/20170429/2017410備份/TestImage/2.png"); } }
JAVA------20.圖片中插文字和圖片