Cómo generar aleatoriamente caracteres chinos de uso común en Java
/**
* El principio es encontrar caracteres chinos a partir del código de ubicación de caracteres chinos. El código de área de caracteres chinos se divide en dígitos altos y dígitos bajos, y entre ellos hay caracteres simplificados y tradicionales. Cuanto mayor sea el número de dígitos, mayor será la probabilidad de generar caracteres chinos tradicionales.
* Entonces, en este ejemplo, el bit alto se toma de 171, el bit bajo se toma de 161 y la mayoría de los caracteres tradicionales y raros se eliminan.
¡Pero todavía los habrá! !
*
*/
@Test
public void create() lanza una excepción {
String str = null;
int hightPos, lowPos; // Definir bits altos y bajos
Aleatorio aleatorio = new Random();
hightPos = (176 Math. abs( random.nextInt(39))); //Obtener el valor alto
lowPos = (161 Math.abs(random.nextInt(93)) //Obtener el valor bajo
byte[] b = nuevo byte[2];
b[0] = (nuevo entero(hightPos).byteValue());
b[1] = (new Integer (lowPos).byteValue());
str = new String(b, "GBk"); //Convertir a chino
System.err.println( str);
}
/**
* Rotar y escalar texto
* Debe usar la clase Graphics2d
* /
public void trans(HttpServletRequest req, HttpServletResponse resp) lanza una excepción{
int width=88
int height=22
p>BufferedImage img = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
Gráficos g = img.getGraphics();
Graphics2D g2d = (Graphics2D) g ;
p>
g2d.setFont(new Font("黑体",Font.BOLD, 17));
Aleatorio r = new Aleatorio(); >
for(int i =0;ilt;4;i){
String str = "" r.nextInt(10);
AffineTransform aff = new AffineTransform( );
aff.rotate(Math.random(), i*18, altura-5
aff.scale(0.6 Math.random(), 0.6 Math.random); ());
g2
d.setTransform(aff);
g2d.drawString(str, i*18, altura-5);
System.err.println("gt;:" str);
}
g2d.dispose();
ImageIO.write(img, "JPEG", resp.getOutputStream()); p> }