当前位置: 代码迷 >> 综合 >> SpringMVC先将图片进行合成然后下载
  详细解决方案

SpringMVC先将图片进行合成然后下载

热度:32   发布时间:2024-01-18 03:31:33.0
<pre name="code" class="java"> /*** 将图片进行合成* @param bigPath 主图图片路径* @param smallPath 商品图片路径* @param erweimaPath 二维码图片路径*/@RequestMapping("download.do")public  void overlapImage(HttpServletResponse response,String bigPath, String erWeiMaPath,String smallPath,Country country,SellerItemModel sim,SellerWarehouse sellerWarehouse) {try {BufferedImage big = ImageIO.read(new File(bigPath));URL url=new URL(erWeiMaPath);BufferedImage erweima = ImageIO.read(url.openStream());URL url1=new URL(smallPath);BufferedImage small = ImageIO.read(url1.openStream());//通图片的路径去读取图片// BufferedImage small= ImageIO.read(new File(smallPath));int width=2015; int height=1136;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;g.drawImage(image, 0, 0,null);g.drawImage(small, x+320, y-5, 800, 600, null);g.drawImage(erweima, x1-575, y1+100, 596, 596, null);Font font=new Font("宋体",Font.PLAIN , 25);g.setFont(font);g.setPaint(Color.DARK_GRAY);int numWidth =x+320;int numHright=y+650;int num=0;g.drawString("商品名称:"+sim.getName(), numWidth,numHright);  num += 50;  g.setPaint(Color.DARK_GRAY);Font font1=new Font("宋体",Font.BOLD , 25);g.setFont(font1);g.drawString("售价:"+new java.text.DecimalFormat("#.00").format(sim.getMarketPrice())+"RMB", numWidth,numHright+num); num += 50; Font font2=new Font("宋体",Font.PLAIN , 25);g.setFont(font2);g.setPaint(Color.DARK_GRAY);g.drawString("原产地:"+country.getZhName(),numWidth, numHright+num); num += 50;  g.drawString("配送方式:"+sellerWarehouse.getName(),numWidth, numHright+num); g.dispose();response.setCharacterEncoding("utf-8");response.setContentType("multipart/form-data");response.setHeader("Content-disposition", "attachment; filename="  + new String((sim.getName()+".jpg").getBytes("utf-8"), "ISO8859-1"));ImageIO.write(bufferedImage2, "jpg", response.getOutputStream());} catch (Exception e) {e.printStackTrace();}}



  相关解决方案