`

emf格式图片,费了3天时间

 
阅读更多
public static byte[] createEmfTag(String sRand) throws Exception{
		Random random = new Random();// 创建一个随机类
		Vector<EMFTag> emf = new Vector<EMFTag>();
		// 创建边框
		RoundRect rr=new RoundRect(new Rectangle(10, 10, 119, 59) ,new Dimension(1,1));
		emf.add(rr);
		// 创建字体,字体的大小应该根据图片的高度来定。
//		LogFontW font = new LogFontW(-82, 0, 0, 0, 0, false, true, false,0, 3, 2, 1, 0x22, "Times New Roman"); 
//		Panose panose = new Panose();
//		ExtLogFontW extFont = new ExtLogFontW(font, "", "", 0, 0, 0, new byte[] {0, 0, 0, 0}, 0, panose); 
//		ExtCreateFontIndirectW ecfi=new ExtCreateFontIndirectW(1,extFont);
//		emf.add(ecfi);
		// 设置文字的颜色
		SetTextColor st=new SetTextColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
		emf.add(st);
		//设置验证码
		TextW tw=new TextW(new Point(20, 30),sRand, 0,  new Rectangle(1, 1, 11, 5), new int[] { 30, 30, 30, 0, 0 });
		emf.add(new ExtTextOutW(new Rectangle(0,0,120,60), 2, 120, 60, tw));
		//随机加入10条干扰线
		for (int i = 0; i < 10; i++) {
			LineTo lt=new LineTo(new Point(random.nextInt(150),random.nextInt(80)));
			emf.add(lt);
		}
		//随机产生100个干扰点
		for (int i = 0; i < 100; i++) {
			emf.add(new SetPixelV(new Point(random.nextInt(120),random.nextInt(60)),getRandColor(160, 200)));
		}
		emf.add(new EOF());
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try{
			EMFOutputStream out = new EMFOutputStream(os, new Rectangle(10, 10, 119, 59),
					new EMFHandleManager(), "", "", new Dimension(120, 60));
			for (int i = 0; i < emf.size(); i++) {
				out.writeTag((org.freehep.util.io.Tag)emf.get(i));
			}
			out.close();
			return os.toByteArray();
		}catch(Exception ex){
			throw ex;
		}finally{
			if (os != null)
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
	}
	
	public static Color getRandColor(int lower, int upper) {
		Random random = new Random();
		if (upper > 255)
			upper = 255;
		if (upper < 1)
			upper = 1;
		if (lower < 1)
			lower = 1;
		if (lower > 255)
			lower = 255;
		int r = lower + random.nextInt(upper - lower);
		int g = lower + random.nextInt(upper - lower);
		int b = lower + random.nextInt(upper - lower);
		return new Color(r, g, b);
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics