Java is used to generate two-dimensional codes.

Source: Internet
Author: User

Java is used to generate two-dimensional codes.

I saw a video last night about how the web Client made the business card into a QR code. I think it is quite interesting, but I am still a beginner, so I didn't implement it on the web page, the command line program is written.

Although the program looks very short, but there is still a problem in the writing process, the most fatal is

Graphics2D.clearRect(0,0,235,235);
Graphics2D. setColor (Color. BLACK );
The two codes are reversed, causing the generated QR code to be abnormal and cannot be read. after checking for a long time, I didn't see the problem. In fact, if we set the color first and then clean, isn't it equivalent to not setting the color,
Therefore, you must be careful in the future to avoid such low-level mistakes.
 
package com.fantasyado.qrcoder;
import com.swetake.util.Qrcode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Main
{


public static void main(String[] args)
{
// write your code here
String content="";
Scanner sc=new Scanner(System.in);
System.out.println("input your information,and ends with ok");
while(! content.endsWith("ok")){
content=content+"\n";
content=content.concat(sc.next());
}

content=content.substring(1,content.length()-2);
// content=content.replaceAll("ok","");

System.out.println("input your filename ends with EnterKey");
String filename=sc.next();
String filepath="D:\\"+filename+".png";
new Main().drawQRCODE(content, filepath);
System.out.println("draw QR_code successfullly!");
System.out.println(content);

}
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();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.