Step 2 : code Step 3 : Runnable project
Generating and parsing QR codes requires third-party packages : QRCODE.jar. In the upper right corner, you can download
How to implement QR code doesn't matter , Just care about two ways :
Write the string into the QR code , And generate pictures to destFile public static void qrCodeEncode(String encodeddata, File destFile) Send QR code information from imageFile Read it from public static String qrCodeDecode(File imageFile) In the main method is the instance , Write the string into qrcode.png, At the same time from qrcode.png Parse out the string in
import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.imageio.ImageIO; import com.swetake.util.Qrcode; import jp.sourceforge.qrcode.QRCodeDecoder; import jp.sourceforge.qrcode.data.QRCodeImage; import jp.sourceforge.qrcode.exception.DecodingFailedException; public class QRCodeUtil { public static void qrCodeEncode(String encodeddata, File destFile) throws IOException { Qrcode qrcode = new Qrcode(); qrcode.setQrcodeErrorCorrect('M'); // Error correction level (L 7%、M 15%、Q 25%、H 30%) Related to version qrcode.setQrcodeEncodeMode('B'); qrcode.setQrcodeVersion(7); // set up Qrcode Package version byte[] d = encodeddata.getBytes("GBK"); // Character set BufferedImage bi = new BufferedImage(139, 139, BufferedImage.TYPE_INT_RGB); // createGraphics // Create layers Graphics2D g = bi.createGraphics(); g.setBackground(Color.WHITE); // Set the background color ( White ) g.clearRect(0, 0, 139, 139); // Rectangle X、Y、width、height g.setColor(Color.BLACK); // Set image color ( black ) if (d.length > 0 && d.length < 123) { boolean[][] b = qrcode.calQrcode(d); 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); } } } } // Image img = ImageIO.read(new File("D:/tt.png")); logo // g.drawImage(img, 25, 55,60,50, null); g.dispose(); // Release the context of this drawing and all system resources it uses . Call dispose Then , You can no longer use Graphics Object bi.flush(); // Refresh this Image All reconfigurable resources that the object is using ImageIO.write(bi, "png", destFile); // System.out.println("Input Encoded data is:" + encodeddata); } /** * Parse QR code , Return the parsed content * * @param imageFile * @return */ public static String qrCodeDecode(File imageFile) { String decodedData = null; QRCodeDecoder decoder = new QRCodeDecoder(); BufferedImage image = null; try { image = ImageIO.read(imageFile); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } try { decodedData = new String(decoder.decode(new J2SEImage(image)), "GBK"); // System.out.println("Output Decoded Data is:" + decodedData); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return decodedData; } static class J2SEImage implements QRCodeImage { BufferedImage image; public J2SEImage(BufferedImage image) { this.image = image; } public int getWidth() { return image.getWidth(); } public int getHeight() { return image.getHeight(); } public int getPixel(int x, int y) { return image.getRGB(x, y); } } public static void main(String[] args) { String filePath = "d:/qrcode.png"; File qrFile = new File(filePath); // QR code content String encodeddata = "http://inotgo.com"; try { QRCodeUtil.qrCodeEncode(encodeddata, qrFile); } catch (IOException e) { e.printStackTrace(); } // decode String reText = QRCodeUtil.qrCodeDecode(qrFile); System.out.println(reText); } }
In the upper right corner, there is the runnable Project Download corresponding to this knowledge point , I really can't do it myself , Just download and unzip it and compare it .
The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .
![]()
Q & A area
2020-09-28
How to adjust the pixel size of QR code ?
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2020-06-15
The stationmaster greatly Please ask This generated QR code Is there a time limit It's permanent There is still a failure time How long is the effective time ?
1 One answer
sanGuo Jump to the problem location Answer time :2020-07-14
The QR code contains , The string you put in , Any time you scan the QR code, you will get the string you put when generating the QR code . It has nothing to do with timeliness . So , You want to make url Time limited , Have to be in url Set after jump
The answer has been submitted successfully , Auditing . Please
My answer Check the answer record at , thank you
2020-06-03
Exception occurred while parsing
2020-04-11
This is very strong
2020-03-30
Chinese garbled
Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 12 Previous questions , please Click to view
Please... Before asking questions land
The question has been submitted successfully , Auditing . Please
My question Check the question record at , thank you
|