zoukankan      html  css  js  c++  java
  • java给图片写正反字体,并将二维码写到图片上,代码实现

    /**
         * @param filePath
         *            源图片路径
         * @param markContent
         *            图片中添加内容
         * @param outPath
         *            输出图片路径 字体颜色等在函数内部实现的
         *            
         * @param 
         */
        // 给jpg添加文字
        public  boolean createStringMark(String url , String filePath, String markContent, int R,int G,int B,String choosefont) {
            ImageIcon imgIcon = new ImageIcon(filePath);
            Image theImg = imgIcon.getImage();
            int width = theImg.getWidth(null) == -1 ? 200 : theImg.getWidth(null);
            int height = theImg.getHeight(null) == -1 ? 200 : theImg.getHeight(null);
            System.out.println(width);
            System.out.println(height);
            System.out.println(theImg);
            BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bimage.createGraphics();
     
            //Color mycolor1 = Color.white;
            Color mycolor1 = new Color(255,255,255);
            g.setColor(mycolor1);
            g.setBackground(Color.red);
            g.drawImage(theImg, 0, 0, null); // 方法在画布上绘制图像、画布或视频。
            //Font font=new Font("黑体", Font.BOLD, 200);  
            Font font=new Font(choosefont, Font.BOLD, 200);  
            font = Font(200);
            g.setFont(font); // 字体、字型、字号  三个字350
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            // 计算文字长度,计算居中的x点坐标
            FontMetrics fm = g.getFontMetrics(font);
            int textWidth = fm.stringWidth(markContent);
            int widthX = (width - textWidth) / 2;
            // 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。 
            g.drawString(markContent,widthX,1130);  
            
            Color mycolor = new Color(R,G,B);
            g.setColor(mycolor);
            g.setBackground(Color.red);
            //g.drawImage(theImg, 0, 0, null);
            Font rotatefont=new Font(choosefont, Font.BOLD, 200);  
            rotatefont =  Font(200);
            g.setFont(rotatefont); // 字体、字型、字号  三个字350
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            FontMetrics rotatefm = g.getFontMetrics(font);
            int rotatetextWidth = rotatefm.stringWidth(markContent);
            int rotatewidthX = (width - rotatetextWidth) / 2;
            //画倒转180度的字体
            g.translate(rotatewidthX+rotatetextWidth, 350); 
            g.rotate(Math.toRadians(180)); 
            g.drawString(markContent, 0, 0); 
            g.dispose();
            try {
                long currenttime = System.currentTimeMillis();
                String pathdate = new SimpleDateFormat("yyyy/MM/dd/HH/hh/mm/ss").format(new Date());
                String path = "F:\pic2018\"+pathdate;
                File file = new File(path);
                if (!file.exists()) {
                    file.mkdirs();
                }
                QRCodeUtil qr = new QRCodeUtil();
                String qrCodepath = qr.Util(url, pathdate);
                String outPath = path+"\"+currenttime+".jpg";
                FileOutputStream out = new FileOutputStream(outPath); // 先用一个特定的输出文件名
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
                param.setQuality(100, true); //
                encoder.encode(bimage, param);
                
                InputStream is = new FileInputStream(outPath);
                //通过JPEG图象流创建JPEG数据流解码器
                JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
                //解码当前JPEG数据流,返回BufferedImage对象
                BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
                //得到画笔对象
                Graphics g2 = buffImg.getGraphics();
     
                //小图片的路径
                ImageIcon QrcodeimgIcon = new ImageIcon(qrCodepath);
                //得到Image对象。
                Image img = QrcodeimgIcon.getImage();
                //将小图片绘到大图片上,5,300 .表示你的小图片在大图片上的位置。
                g2.drawImage(img, 50, 1560, null);
                //设置颜色。
                g2.setColor(Color.WHITE);
                g2.dispose();
                OutputStream os = new FileOutputStream(path+"\"+currenttime+"zong"+".jpg");
                //创键编码器,用于编码内存中的图象数据。
                JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
                en.encode(buffImg);
                is.close();
                os.close();
                
                out.close();
            } catch (Exception e) {
                return false;
            }
            return true;
        }
         // 第一个参数是外部字体名,第二个是字体大小
        public static Font loadFont(String fontFileName, float fontSize){
            try{
                File file = new File(fontFileName);
                FileInputStream aixing = new FileInputStream(file);
                Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
                Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
                aixing.close();
                return dynamicFontPt;
            }
            catch (Exception e)// 异常处理
            {
                e.printStackTrace();
                return new Font("宋体", Font.BOLD, 200);
            }
        }
        public  Font Font(int fontsize) {
            String root = System.getProperty("user.dir");// 项目根目录路径
            Font font = ImageEdit.loadFont(root + "/FZXBSJW.TTF", fontsize);// 调用
            return font;// 返回字体

  • 相关阅读:
    微信小程序入门
    rem js相关
    移动端rem.js使用方法
    手机访问PC网站自动跳转到手机版
    当 return 遇到 try
    Ubuntu apt 使用代理
    shell 十进制数字转十六进制字符串并将结果保存到变量
    (二)一起学 Java Collections Framework 源码之 AbstractCollection
    (一)一起学 Java Collections Framework 源码之 概述
    解决 meld 出现 locale.setlocale(locale.LC_ALL,'') 失败的问题
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/12079376.html
Copyright © 2011-2022 走看看