JavaIO stream learning notes 03

Keywords: Java Database MySQL

Do the last task, create a new file, put in the QR code and output:

First, let's talk about how to make java generate QR codes. There are actually many open source tools here. I recommend using Zxing. First, let's explain how to use Zxing:

First visit https://github.com/zxing , and download:


After downloading, unzip it, as shown in the following figure:

In the zxing master directory, you can see all the source files, of which the core and javase directories are the most important. We use the files in these two directories to generate QR codes in Java.:

Next, we will type the files in the core and javase directories into a jar package by creating a new Java project zxing, as shown in the following figure:

Then we copy the entire com directory under the core directory to the src Directory:

After copying, the src directory is shown in the following figure:

Then we copy the com directory under the javase directory, and you will be prompted whether to overwrite it. We can click "Yes":

An error will be reported after copying, but don't worry about it.

Now let's package. Right click the project and click "Export..." in the right-click menu:

Select "JAR file" in the pop-up dialog box and click "Next":

Select the packaging path and packaging name, as shown in the figure below, and click "Finish":

There will be a warning. Let's ignore it and click "OK".

Let's go to the E:\Zxing directory and find that the zxing-3.2.1.jar package has been generated. Later, we can use this jar package to generate QR code.

Reward: if I don't want to realize the above steps by myself, I have generated it myself:

Link: https://pan.baidu.com/s/1301kKQjdqpRZEnOmolaRWg 
Extraction code: xzii 
--From Baidu online disk super member V3 Sharing

Split line-------------------------------------------------------------

Next, explain the code: (import zxing.jar into IDEA by yourself):

First, we need to generate a new file:

  File file = new File("G:\\number.txt");
        FileWriter writer = null;
        writer = new FileWriter(file,false);
        if (file.exists()){
            System.out.println("Created successfully!");
        }

Then we write the value to:

   
        String result = "100011720988";
        writer.append(result);
        writer.flush();

Read file contents and print:

   InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
        BufferedReader br = new BufferedReader(isr);
        String lineTxt = null;
        String line1 = br.readLine();
        System.out.println(line1);

Modify file content:

    String str = "https://item.csdn.com/"+result+".html";
        writer.append(str);
        writer.flush();
        String line2 = br.readLine();
        System.out.println(line2);

Generate QR Code:

   int width  = 300;  //Width of QR code picture
        int height = 300; //Height of QR code picture
        String format = "png";  //QR code format
        String content = line2;

        //Define QR code content parameters
        HashMap hints = new HashMap();
        //Set character set encoding format
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //Set the fault tolerance level, where we use the M level
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        //Set border spacing
        hints.put(EncodeHintType.MARGIN, 2);
        try {
            //Specify QR code content
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
            //Specify the path to save the generated picture
            Path newfile = new File("H:\\imooc.png").toPath();
            //Generate QR code
            MatrixToImageWriter.writeToPath(bitMatrix, format, newfile);
        } catch (Exception e) {
            e.printStackTrace();
        }

All codes:

package Task 11 IO flow;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.*;
import java.nio.file.Path;
import java.util.HashMap;

/**
 * @author ${Fan Taozhi}
 * @Description
 * @create 2021-11-26 10:56
 */
public class Test9 {
    public static void main(String[] args) throws IOException {

        /**
         * Generate a new file and add content
         */

        File file = new File("G:\\number.txt");
        FileWriter writer = null;
        writer = new FileWriter(file,false);
        if (file.exists()){
            System.out.println("Created successfully!");
        }

        String result = "100011720988";
        writer.append(result);
        writer.flush();
        /**
         * Read file contents and print
         */
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
        BufferedReader br = new BufferedReader(isr);
        String lineTxt = null;
//        while ((lineTxt = br.readLine()) != null) {
//            System.out.println(lineTxt);
//        }
        String line1 = br.readLine();
        System.out.println(line1);

        /**
         * Modify file content
         */
        String str = "https://item.csdn.com/"+result+".html";
        writer.append(str);
        writer.flush();
//        while ((lineTxt = br.readLine()) != null) {
//            System.out.println(lineTxt);
//        }
        String line2 = br.readLine();
        System.out.println(line2);


        /**
         * Generate QR code
         */

        int width  = 300;  //Width of QR code picture
        int height = 300; //Height of QR code picture
        String format = "png";  //QR code format
        String content = line2;

        //Define QR code content parameters
        HashMap hints = new HashMap();
        //Set character set encoding format
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        //Set the fault tolerance level, where we use the M level
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        //Set border spacing
        hints.put(EncodeHintType.MARGIN, 2);
        try {
            //Specify QR code content
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hints);
            //Specify the path to save the generated picture
            Path newfile = new File("H:\\imooc.png").toPath();
            //Generate QR code
            MatrixToImageWriter.writeToPath(bitMatrix, format, newfile);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Operation results:

Posted by robertaccettura on Fri, 26 Nov 2021 09:38:14 -0800