关于_java程序生成二维码_无logo
1.二维码生成方式一
package com.ama.aduan.test;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Scanner;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;//这个包很关键
/**
* @author 手动输入 设置二维码
*
*/
public class TestMaTwo {
public static void main(String[] args) {
String content="";
Scanner sc=new Scanner(System.in);
System.out.println("在控制台编写你的名片信息\n 输入ok后,按enter结束");
while(! content.endsWith("ok")){
content=content+"\n";
content=content.concat(sc.next());
}
content=content.substring(1,content.length()-2);
System.out.println("给生成的二维码设置名字,按enter结束");
String filename=sc.next();
String filepath1="F:/二维码";
File file =new File(filepath1);
if(!file.exists()){
file.mkdir();
}
String filepath=filepath1+"/"+filename+".png";
new TestMain().drawQRCODE(content, filepath);
System.out.println("你的名片生成成功,在你的电脑F!");
}
public void drawQRCODE(String content,String filepath){
try {
Qrcode qrcode=new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(15);
int width= 235;
int height=235;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2=image.createGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0,0,235,235);
g2.setColor(Color.BLACK);
byte[] contentbytes=content.getBytes("utf-8");
boolean[][] codeout= qrcode.calQrcode(contentbytes);
for (int i = 0; i <codeout.length; i++) {
for (int j = 0; j < codeout.length; j++) {
if (codeout[j][i]) g2.fillRect(j*3+2,i*3+2,3,3);
}
}
g2.dispose();
image.flush();
File imgFile = new File(filepath);
ImageIO.write(image, "png", imgFile);
}catch (Exception e){
e.printStackTrace();
}
}
}
展示: