java 图片合成

java 图片的合成

package com.jeeplus.mob.common;

import com.jeeplus.config.properties.JeePlusProperites; 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.http.HttpRequest;
import sun.nio.cs.UnicodeEncoder;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ChartGraphics {

    private BufferedImage image;

    private int imageWidth = 424;  //图片的宽度

    private int imageHeight = 845; //图片的高度

	//图片切圆
    public static BufferedImage DrawAcircle(String url) throws Exception {
        BufferedImage avatarImage = ImageIO.read(new URL(url));
        int width = avatarImage.getWidth();
        // 透明底的图片
        BufferedImage formatAvatarImage = new BufferedImage(width, width, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = formatAvatarImage.createGraphics();
        //把图片切成一个圓
        {
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            //留一个像素的空白区域,这个很重要,画圆的时候把这个覆盖
            int border = 1;
            //图片是一个圆型
            Ellipse2D.Double shape = new Ellipse2D.Double(border, border, width - border * 2, width - border * 2);
            //需要保留的区域
            graphics.setClip(shape);
            graphics.drawImage(avatarImage, border, border, width - border * 2, width - border * 2, null);
            graphics.dispose();
        }
        //在圆图外面再画一个圆
        {
            //新创建一个graphics,这样画的圆不会有锯齿
            graphics = formatAvatarImage.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int border = 3;
            //画笔是4.5个像素,BasicStroke的使用可以查看下面的参考文档
            //使画笔时基本会像外延伸一定像素,具体可以自己使用的时候测试
            Stroke s = new BasicStroke(4.5F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
            graphics.setStroke(s);
            graphics.setColor(Color.WHITE);
            graphics.drawOval(border, border, width - border * 2, width - border * 2);
            graphics.dispose();
        }
        return formatAvatarImage;
    }


    /**  图片合成  上面加下面2张图合成 并底部附上二维码 图片中插入文字描述
	**/
    public static String graphicsGeneration(String mapUrl,Map<String, Object> paramat, String qr) throws IOException {
        int imageWidth = 0;  //图片的宽度
        int imageHeight = 0; //图片的高度
        int H_mainPic = 0;  //地图的高
        int H_footPic = 0; //
         int W_footPic = 0; //
        BufferedImage image;
        BufferedImage bimg = null;
		//画布大小由图片的本身尺寸决定  如果不是图片本尺寸生成的图片会模糊
        try {
            URL url = new URL(mapUrl);
            bimg = ImageIO.read(url);
            imageWidth = bimg.getWidth();
            imageHeight = bimg.getHeight();
            H_mainPic = bimg.getHeight();

        } catch (Exception e) {}
           BufferedImage dimg = null;
           try {
               URL url = new URL("底部图链接");
               dimg = ImageIO.read(url);
               imageHeight += dimg.getHeight();
               H_footPic = dimg.getHeight();
               W_footPic = dimg.getWidth();
           } catch (Exception e) {}
           //使用地图的宽  和  地图的高 加 底部图的高
           //定义图片的宽宽度
           image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);

           //*********************** 插入地图
           Graphics mainPic = image.getGraphics();
            if(bimg!=null){
                mainPic.drawImage(bimg, 0,0, imageWidth, H_mainPic, null);
                mainPic.dispose();
            }
          /****插入底部图片***/
           Graphics footPic = image.getGraphics();
           if(dimg!=null){
               footPic.drawImage(dimg, 0,H_mainPic, W_footPic, H_footPic, null);
               footPic.dispose();
           }
           //***********************设置头像
           /**人物头像**/
           Graphics headPic = image.getGraphics();
           BufferedImage bimg1 = null;
           try {
//               URL url = new URL(paramat.get("headUrl1").toString());
//               bimg1 = ImageIO.read(url);
               bimg1 = DrawAcircle(paramat.get("headUrl").toString());
           } catch (Exception e) {}
           if(bimg1!=null){
               headPic.drawImage(bimg1, 30,600, 100, 100, null);//设置头像的位置
               headPic.dispose();
           }

           
        Graphics2D tip = image.createGraphics();
           // 抗锯齿
           tip.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //设置字体颜色,先设置颜色,再填充内容 
        //设置字体
        Font tipFont = new Font("微软雅黑 Light", Font.PLAIN, 25); 
        tip.setFont(tipFont);
         tip.setColor(new Color(90, 90, 90));
        tip.drawString(paramat.get("nikename").toString(), 60, 720);
    
          //合成二维码
           Graphics qcodeImg = image.getGraphics();
           BufferedImage codeimg = null;
           try {
               URL url = new URL(qr);
               codeimg = ImageIO.read(url);
           } catch (Exception e) {}
           if(codeimg!=null){
               qcodeImg.drawImage(codeimg, 530,1260, 150, 150, null);
               qcodeImg.dispose();
           }
           String logoPathDir = "/FileUpload/pic/"+ DateUtil.dateToStr(new Date(), 2);
           String logoRealPathDir = JeePlusProperites.newInstance().getUserfilesBaseDir();
//        String logoRealPathDir = "D:/upload/";
           logoRealPathDir = logoRealPathDir + logoPathDir;
           String fileName = StringHelper.GetGUID();
           File logoSaveFile = new File(logoRealPathDir);
           if (!logoSaveFile.exists()) {
               logoSaveFile.mkdirs();
           }
//        createImage(logoRealPathDir+"/"+fileName+".png");
        ImageIO.write(image, "png", new File(logoRealPathDir+"/"+fileName+".png")); 
        return url;
    }
  

    public void LimpidLabel(Image img, float alpha) {
        int w, h;
        try
        {
            w = img.getWidth((ImageObserver) this);               //读取图片长度
            h = img.getHeight((ImageObserver) this);          //读取图片宽度
            GraphicsConfiguration gc = new JFrame().getGraphicsConfiguration();        // 本地图形设备
            Image image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);        //建立透明画布
            Graphics2D g = (Graphics2D) image.getGraphics();                           //在画布上创建画笔
            Composite a = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f); //指定透明度为alpha
            g.setComposite(a);
            g.drawImage(img, 0, 0, (ImageObserver) this);           //将image画到g画笔所在的画布上
            g.dispose();                           //释放内存

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    /**
     * 图片设置圆角
     * @param srcImage
     * @return
     * @throws IOException
     */
    public static BufferedImage setRadius(BufferedImage srcImage) throws IOException{
        int radius = (srcImage.getWidth() + srcImage.getHeight()) / 6;
        return setRadius(srcImage, radius, 2, 5);
    }
    /**
     * 图片设置圆角
     * @param srcImage
     * @param radius
     * @param border
     * @param padding
     * @return
     * @throws IOException
     */
    public static BufferedImage setRadius(BufferedImage srcImage, int radius, int border, int padding) throws IOException{
        int width = srcImage.getWidth();
        int height = srcImage.getHeight();
        int canvasWidth = width + padding * 2;
        int canvasHeight = height + padding * 2;

        BufferedImage image = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();
        gs.setComposite(AlphaComposite.Src);
        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setColor(Color.WHITE);
        gs.fill(new RoundRectangle2D.Float(0, 0, canvasWidth, canvasHeight, radius, radius));
        gs.setComposite(AlphaComposite.SrcAtop);
        gs.drawImage(setClip(srcImage, radius), padding, padding, null);
        if(border !=0){
            gs.setColor(Color.GRAY);
            gs.setStroke(new BasicStroke(border));
            gs.drawRoundRect(padding, padding, canvasWidth - 2 * padding, canvasHeight - 2 * padding, radius, radius);
        }
        gs.dispose();
        return image;
    }

    /**
     * 图片切圆角
     * @param srcImage
     * @param radius
     * @return
     */
    public static BufferedImage setClip(BufferedImage srcImage, int radius){
        int width = srcImage.getWidth();
        int height = srcImage.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();
        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, radius, radius));
        gs.drawImage(srcImage, 0, 0, null);
        gs.dispose();
        return image;
    }

     
  

    /**
     * 坐标转换,腾讯地图转换成百度地图坐标
     * @param lat 腾讯纬度
     * @param lon 腾讯经度
     * @return 返回结果:经度,纬度
     */
    public static  String  map_tx2bd(double lat, double lon) {
        double bd_lat;
        double bd_lon;
        double x_pi = 3.14159265358979324;
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        bd_lon = z * Math.cos(theta) + 0.0065;
        bd_lat = z * Math.sin(theta) + 0.006;

        return  bd_lon + "," + bd_lat;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值