Home >Web Front-end >uni-app >How uniapp converts binary files into images

How uniapp converts binary files into images

PHPz
PHPzOriginal
2023-04-17 11:28:493332browse

With the increasing popularity of mobile applications, in order to improve the response speed of applications, developers have begun to consider using binary files to store static resources such as image resources required by applications. Once the file type changes to a binary file, it cannot be displayed directly on the page. This article will introduce how to convert binary files into images for display directly on the front-end page.

1. What is Uniapp

Uniapp is a development framework based on Vue.js, which is mainly used to quickly develop native applications, H5, etc. for multiple platforms. The binary file in the Uniapp framework is to obtain the binary stream in the back-end database, and then dynamically generate the resource address on the front-end through an ajax request. Due to the special nature of binary files, when the front end requests these resources, some additional processing operations may be required to successfully obtain the resource content.

2. The necessity of converting binary files into images

For some mobile WEB applications, in order to improve the access speed and performance of the page, in some cases, image resources are often saved as binary files. form instead of the traditional image URL. However, this method will cause certain difficulties in front-end development work, because the front-end cannot render these images by simply using the tag. In this case, converting the binary file into an image will facilitate the front-end display of images without affecting file performance.

3. Methods of converting binary files into images

There are two main methods of converting binary files into images: using JavaScript or back-end program parsing.

  1. Use JavaScript to parse

Using JavaScript to parse binary files and convert them into images is a lightweight and reliable solution. The implementation process of this method is as follows:

First, obtain the binary file returned by the backend.

Use JS's usually built-in Buffer object and canvas element to convert binary files into PNG or JPEG images.

Finally, use img or canvas elements to display images.

Code example of this method:

var imageBuffer = new Buffer(binaryData, 'binary');
var img = new Image;
img.src = 'data:image/png;base64,' + imageBuffer.toString('base64');
  1. Use back-end program to parse

Developers can actively parse binary resource files through back-end program into a picture and cache it to the front-end server so that the front-end page can call it at any time.

Implementation steps of this method:

The back-end program requests the data stream of the binary file.

The back-end program converts the data stream into image files and then stores them on the front-end server.

The front-end page requests pictures and calls the picture resources on the front-end server.

The biggest advantage of this method is that it can cache images, thereby reducing the number of requests to the back-end server and improving the response speed of the application. However, the disadvantage of this approach is that it requires additional server support and requires more code.

4. Summary

This article introduces two methods of converting binary files in the uniapp framework into images: JavaScript parsing and back-end program parsing. The former is suitable for reducing the number of requests to the back-end server and improving the response speed of the application; the latter requires additional server support and code volume. Whichever approach is chosen, it needs to be carefully evaluated and selected prior to development in order to be fully prepared for application development and performance.

The above is the detailed content of How uniapp converts binary files into images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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