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;// 返回字体
	}

效果图

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现身份证正反扫描合并到一张图片上,可以分为以下几个步骤: 1. 读取身份证正面图像和反面图像。 2. 对身份证正反面图像进行灰度化和二值化处理,使其变成黑白图像。 3. 分别使用轮廓检测算法(如findContours函数)找到正面图像和反面图像的轮廓。 4. 对正反面图像的轮廓分别进行筛选,只保留符合要求的轮廓(如形状、大小等)。 5. 使用透视变换(如warpPerspective函数)将正反面图像变换到目标区域。 6. 将变换后的正反面图像与背景图像合并到一张图片上。 下面是一个示例代码: ```python import cv2 import numpy as np # 读取身份证正面图像和反面图像 id_card_front = cv2.imread("id_card_front.jpg") id_card_back = cv2.imread("id_card_back.jpg") bg_img = cv2.imread("bg.jpg") # 对身份证正反面图像进行灰度化和二值化处理 gray_front = cv2.cvtColor(id_card_front, cv2.COLOR_BGR2GRAY) gray_back = cv2.cvtColor(id_card_back, cv2.COLOR_BGR2GRAY) ret, binary_front = cv2.threshold(gray_front, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) ret, binary_back = cv2.threshold(gray_back, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) # 找到身份证正反面图像的轮廓 contours_front, hierarchy = cv2.findContours(binary_front, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours_back, hierarchy = cv2.findContours(binary_back, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 筛选符合要求的轮廓 for contour in contours_front: if cv2.contourArea(contour) > 10000: x, y, w, h = cv2.boundingRect(contour) if w > 200 and h > 200: # 使用透视变换将身份证正面图像变换到目标区域 src_points = np.array([[x, y], [x + w, y], [x + w, y + h], [x, y + h]], dtype=np.float32) dst_points = np.array([[500, 500], [1000, 500], [1000, 1000], [500, 1000]], dtype=np.float32) M = cv2.getPerspectiveTransform(src_points, dst_points) id_card_front_transformed = cv2.warpPerspective(id_card_front, M, (1500, 1500)) break for contour in contours_back: if cv2.contourArea(contour) > 10000: x, y, w, h = cv2.boundingRect(contour) if w > 200 and h > 200: # 使用透视变换将身份证反面图像变换到目标区域 src_points = np.array([[x, y], [x + w, y], [x + w, y + h], [x, y + h]], dtype=np.float32) dst_points = np.array([[500, 1200], [1000, 1200], [1000, 1500], [500, 1500]], dtype=np.float32) M = cv2.getPerspectiveTransform(src_points, dst_points) id_card_back_transformed = cv2.warpPerspective(id_card_back, M, (1500, 1500)) break # 将变换后的正反面图像与背景图像合并到一张图片上 bg_img[500:2000, 500:2000] = id_card_front_transformed bg_img[1200:2700, 500:2000] = id_card_back_transformed cv2.imshow("merged image", bg_img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 请注意,上述代码仅供参考,具体实现可能需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值