标签:
以下源码,注意不要放在同一个文件夹里,选择中文编码格式“utf-8”
注意字符串的读入问题,表示新手做的第一个小玩意很兴奋,虽然抄了一些源码,但扫出来了还是很爽的
package com.kt.twobarimage;
import com.swetake.util.Qrcode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class CreateTwoBarImage {
public void creatTxm(String param) throws Exception {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect(‘M‘);
qrcode.setQrcodeEncodeMode(‘B‘);
qrcode.setQrcodeVersion(7);
byte[] bstr = param.getBytes("UTF-8");
BufferedImage bi = new BufferedImage(139, 139,BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setBackground(Color.WHITE); //背景颜色
g.clearRect(0, 0, 139, 139);
g.setColor(Color.BLACK); //条码颜色
if (bstr.length > 0 && bstr.length < 123) {
boolean[][] b = qrcode.calQrcode(bstr);
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (b[j][i]) {
g.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
}
}
}
}
g.dispose();
bi.flush();
String FilePath = "d:/images/"+param.substring(0,5)+".jpg";
File f = new File(FilePath);
ImageIO.write(bi, "jpg", f);
}
public static void main(String args[]){
DataInputStream din = null;
try{
InputStreamReader is = new InputStreamReader(new FileInputStream("E:/input/test.txt"),"utf-8");
String str1 = "";
String str2 = "";
int i = -1;
while((i=is.read())!=-1){
char m = (char)i;
str2 = String.valueOf(m);
str1 = str1+str2;
}
try {
new CreateTwoBarImage().creatTxm(str1);
} catch (Exception e) {
e.printStackTrace();
}
is.close();
}catch (Exception e){
e.printStackTrace();
}
//最终行为---关闭流~
finally{
try{
if (din != null){
din.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/guoyang134340/p/4681574.html