Java随机生成中文汉字

Java随机生成中文汉字
/**
* 原理是从汉字区位码找到汉字。在汉字区位码中分高位与底位, 且其中简体又有繁体。位数越前生成的汉字繁体的机率越大。
* 所以在本例中高位从171取,底位从161取, 去掉大部分的繁体和生僻字。但仍然会有!!
*
*/
@Test
public void create() throws Exception {
String str = null;
int hightPos, lowPos; // 定义高低位
Random random = new Random();
hightPos = (176 + Math.abs(random.nextInt(39)));//获取高位值
lowPos = (161 + Math.abs(random.nextInt(93)));//获取低位值
byte[] b = new byte[2];
b[0] = (new Integer(hightPos).byteValue());
b[1] = (new Integer(lowPos).byteValue());
str = new String(b, "GBk");//转成中文
System.err.println(str);
}

/**
* 旋转和缩放文字
* 必须要使用Graphics2d类
*/
public void trans(HttpServletRequest req, HttpServletResponse resp) throws Exception{
int width=88;
int height=22;
BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(new Font("黑体",Font.BOLD,17));
Random r = new Random();
for(int i=0;i<4;i++){
String str = ""+r.nextInt(10);
AffineTransform aff = new AffineTransform();
aff.rotate(Math.random(),i*18,height-5);
aff.scale(0.6+Math.random(), 0.6+Math.random());
g2d.setTransform(aff);
g2d.drawString(str,i*18,height-5);
System.err.println(">:"+str);
}
g2d.dispose();
ImageIO.write(img, "JPEG",resp.getOutputStream());
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值