Java随机生成中文汉字(使用高位低位转码)
1 Java随机生成中文汉字2 /**
3 * 原理是从汉字区位码找到汉字。在汉字区位码中分高位与底位, 且其中简体又有繁体。位数越前生成的汉字繁体的机率越大。
4 * 所以在本例中高位从171取,底位从161取, 去掉大部分的繁体和生僻字。但仍然会有!!
5 *
6 */
7 @Test
8 public void create() throws Exception {
9 String str = null;
10 int hightPos, lowPos; // 定义高低位
11 Random random = new Random();
12 hightPos = (176 + Math.abs(random.nextInt(39)));//获取高位值
13 lowPos = (161 + Math.abs(random.nextInt(93)));//获取低位值
14 byte[] b = new byte[2];
15 b[0] = (new Integer(hightPos).byteValue());
16 b[1] = (new Integer(lowPos).byteValue());
17 str = new String(b, "GBk");//转成中文
18 System.err.println(str);
19 }
20
21 /**
22 * 旋转和缩放文字
23 * 必须要使用Graphics2d类
24 */
25 public void trans(HttpServletRequest req, HttpServletResponse resp) throws Exception{
26 int width=88;
27 int height=22;
28 BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
29 Graphics g = img.getGraphics();
30 Graphics2D g2d = (Graphics2D) g;
31 g2d.setFont(new Font("黑体",Font.BOLD,17));
32 Random r = new Random();
33 for(int i=0;i<4;i++){
34 String str = ""+r.nextInt(10);
35 AffineTransform aff = new AffineTransform();
36 aff.rotate(Math.random(),i*18,height-5);
37 aff.scale(0.6+Math.random(), 0.6+Math.random());
38 g2d.setTransform(aff);
39 g2d.drawString(str,i*18,height-5);
40 System.err.println(">:"+str);
41 }
42 g2d.dispose();
43 ImageIO.write(img, "JPEG",resp.getOutputStream());
44 }
以上是 Java随机生成中文汉字(使用高位低位转码) 的全部内容, 来源链接: utcz.com/z/390770.html