2019独角兽企业重金招聘Python工程师标准>>>
//1:声明图片的大小int width = 60;int height = 30;//2:声明内存中的图片BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//3:获取gGraphics g = img.getGraphics();//4:设置背景g.setColor(Color.WHITE);g.fillRect(0, 0, width, height);g.setFont(new Font("宋体", Font.BOLD, 18));//A-Z0-9//生成4个字符String b = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";char[] a = new char[4]; Random r = new Random();for (int i = 0; i < a.length; i++) { int rand = (int) (Math.random() * b.length()); a[i] = b.charAt(rand); } for(int i=0;i<a.length;i++){g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));g.drawString(""+a[i],i*15,10+r.nextInt(20));} /* Random r = new Random();for(int i=0;i<4;i++){int a = r.nextInt(10);System.out.print(a);g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));g.drawString(""+a[i],i*15,10+r.nextInt(20));}*/ for(int i=0;i<10;i++){g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));g.drawLine(r.nextInt(width), r.nextInt(height),r.nextInt(width), r.nextInt(height));}g.dispose();ImageIO.write(img, "JPEG", new FileOutputStream("d:/a/a.jpg"));}