標籤:
以下源碼,注意不要放在同一個檔案夾裡,選擇中文編碼格式“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();
}
}
}
}
java實現從txt檔案讀入資訊輸出二維碼