Home Java javaTutorial How to correctly generate and display the WeChat applet with parameters QR codes in Java?

How to correctly generate and display the WeChat applet with parameters QR codes in Java?

Apr 19, 2025 pm 04:48 PM
WeChat Browser access qq spring mvc

How to correctly generate and display the WeChat applet with parameters QR codes in Java?

This article introduces how to generate a WeChat applet QR code containing parameters in a Java environment and display it on an HTML page. We will explore how to use Java code to call the WeChat interface, generate QR code, and pass the image data to the front-end for display through Base64 encoding. There were problems with the previous implementation plan, which caused the QR code to be displayed normally. The main reason was that the binary stream data returned by the WeChat interface was improperly processed.

The improved Java backend code focuses on correctly processing the conversion of binary data to Base64 strings:

 String accessToken = getAccessToken(); // The method to get accessToken, omitted here // Call the WeChat interface to generate a QR code URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" accessToken);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

// Send request parameter JSONObject paramJson = new JSONObject();
paramJson.put("scene", "id=1");
paramJson.put("page", "/pages/index/index");
OutputStream outputStream = connection.getOutputStream();
outputStream.write(paramJson.toString().getBytes("UTF-8"));
outputStream.flush();
outputStream.close();


// Get response data InputStream inputStream = connection.getInputStream();
byte[] imageBytes = inputStream.readAllBytes(); // Use readAllBytes() to simplify the reading process inputStream.close();
String base64Image = Base64.getEncoder().encodeToString(imageBytes);

// Return Base64 encoded QR code image data to the front end // ... (Here, according to your backend framework, such as Spring MVC, encapsulate base64Image data into the response) ...
Copy after login

Front-end HTML and JavaScript code are relatively simple, just make sure that the back-end correctly returns Base64-encoded image data. After the backend code is improved, the base64Image string should be returned as response data. After the front-end receives it, use data:image/jpeg;base64, prefix to splice base64Image and assign it to the src attribute of the img tag to correctly display the QR code. Please note that the part of the code that returns base64Image according to your backend framework adjustment, and make sure to set the correct Content-Type to application/json or other types that suit your framework so that the browser can correctly parse the data. Correctly handling the conversion of binary data to Base64 strings is the key to solving the problem.

The above is the detailed content of How to correctly generate and display the WeChat applet with parameters QR codes in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to parse next-auth generated JWT token in Java and get information in it? How to parse next-auth generated JWT token in Java and get information in it? Apr 19, 2025 pm 08:21 PM

In processing next-auth generated JWT...

What are the plugins for wordpress blocking ip What are the plugins for wordpress blocking ip Apr 20, 2025 am 08:27 AM

WordPress IP blocking plugin selection is crucial. The following types can be considered: based on .htaccess: efficient, but complex operation; database operation: flexible, but low efficiency; firewall: high security performance, but complex configuration; self-written: highest control, but requires more technical level.

Web3 trading platform ranking_Web3 global exchanges top ten summary Web3 trading platform ranking_Web3 global exchanges top ten summary Apr 21, 2025 am 10:45 AM

Binance is the overlord of the global digital asset trading ecosystem, and its characteristics include: 1. The average daily trading volume exceeds $150 billion, supports 500 trading pairs, covering 98% of mainstream currencies; 2. The innovation matrix covers the derivatives market, Web3 layout and education system; 3. The technical advantages are millisecond matching engines, with peak processing volumes of 1.4 million transactions per second; 4. Compliance progress holds 15-country licenses and establishes compliant entities in Europe and the United States.

Why can't JavaScript directly obtain hardware information on the user's computer? Why can't JavaScript directly obtain hardware information on the user's computer? Apr 19, 2025 pm 08:15 PM

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

How to solve the problem of RpcContext.getContext().getRemoteAddress() returning empty in HSF framework? How to solve the problem of RpcContext.getContext().getRemoteAddress() returning empty in HSF framework? Apr 19, 2025 pm 09:54 PM

How to get the IP address of the caller who calls this service in the HSF framework? When providing services using the HSF framework, developers may encounter how to get the call to this...

How do Java regular expressions efficiently extract specific URLs in HTML text? How do Java regular expressions efficiently extract specific URLs in HTML text? Apr 19, 2025 pm 07:30 PM

Tips for extracting specific text by Java regular expressions In Java development, it is often necessary to extract the parts we are interested in from a large amount of text data. Regular...

What to do if the USDT transfer address is incorrect? Guide for beginners What to do if the USDT transfer address is incorrect? Guide for beginners Apr 21, 2025 pm 12:12 PM

After the USDT transfer address is incorrect, first confirm that the transfer has occurred, and then take measures according to the error type. 1. Confirm the transfer: view the transaction history, obtain and query the transaction hash value on the blockchain browser. 2. Take measures: If the address does not exist, wait for the funds to be returned or contact customer service; if it is an invalid address, contact customer service and seek professional help; if it is transferred to someone else, try to contact the payee or seek legal help.

See all articles