资源简介
默认会在c盘下生成一张8*10的密保卡jpg图片,每个单元格数字随机,密保卡序列号随机。
代码片段和文件信息
package MTT;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.Random;
import javax.imageio.*;
import java.awt.font.*;
import java.awt.geom.*;
public class CreatePasswordCard {
public static void main(String[] args) throws Exception {
int width = 227;
int height = 357;
String index = “序列号:“;
String[] xindex = { “1“ “2“ “3“ “4“ “5“ “6“ “7“ “8“ };
String[] yindex = { “A“ “B“ “C“ “D“ “E“ “F“ “G“ “H“ “I“ “J“};
// String[] xyindex = { “—“ “|“ };
Random random = new Random();
File file = new File(“c:/image.jpg“);
Font font = new Font(“Serif“ Font.BOLD 10);
BufferedImage bi = new BufferedImage(width height
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0 0 width height);
g2.setPaint(Color.blue);
// g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP
// 0.5f)); // 1.0f为透明度 ,值从0-1.0,依次变得不透明
//g2.drawImage(ImageIO.read(new File(“D:/mibaoka.jpg“)) 0 0 null);
// g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// 生成序列号
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(index context);
double x = (width - 207);
double y = (height - 287);
double ascent = -bounds.getY();
double baseY = y + ascent;
index = index + (random.nextInt(8999) + 1000) + “ “
+ (random.nextInt(8999) + 1000) + “ “
+ (random.nextInt(8999) + 1000) + “ “
+ (random.nextInt(8999) + 1000);
g2.drawString(index (int) x (int) baseY);
//生成X轴上数据
for (int i = 0; i < xindex.length; i++) {
double xx = (width - 222) + 24 * (i + 1);
double yx = (height - 267) + 22;
g2.drawString(xindex[i] (int) xx (int) yx);
}
//生成Y轴上数据
for (int i = 0; i < yindex.length; i++) {
double xy = (width - 222) + 10;
double yy = (height - 247)+ 22 * (i + 1);
g2.drawString(yindex[i] (int) xy (int) yy);
}
// //绘横线线条
// for (int j = 0; j < 11; j++) {
// for (int i = 0; i < 32; i++) {
// double xx = (width - 222) + 10 * (i + 1);
// double yx = (height - 235) + 20 * j;
// g2.drawString(xyindex[0] (int) xx (int) yx);
// }
// }
// //绘纵线线条
// for (int j = 0; j < 9; j++) {
// for (int i = 0; i < 30; i++) {
// double xx = (width - 203) + 25 * j;
// double yx = (height - 235) + 10 * (i + 1);
// g2.drawString(xyindex[1] (int) xx (int) yx);
// }
// }
g2.setPaint(Color.red);
// 生成8*10个00-99随机数
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 8; i++) {
int sj = random.nextInt(99);
double xx = (width - 222) + 24 * (i + 1);
double yx = (height - 225) + 22 * j;
// sj小于两位数前面补0
g2.drawString(sj < 10 ? (“0“ + String.valueOf(sj)) : String
.valueOf(sj) (int) xx (int) yx);
}
}
ImageIO.write(bi “jpg“ file);
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3036 2010-12-16 15:23 CreatePasswordCard.java
----------- --------- ---------- ----- ----
3036 1
- 上一篇:jsp网页制作 通讯录管理系统
- 下一篇:java_ssh2框架_小
评论
共有 条评论