This time I will bring you H5 to call the camera to take pictures and compress pictures. What are the precautions for H5 to call the camera to take pictures and compress pictures? The following is a practical case, let's take a look.
Organize the documents, search out an example code of H5 calling the camera to take pictures and compress the pictures, sort it out and streamline it a little for sharing.
Background
I recently made an h5 page. The main function is to call the camera to take pictures or select pictures from the album and compress the pictures to base64 and then upload them to the backend server. The server then returns the recognition results.
The main functional points of the front end are:
- How to call the camera in H5
- How to compress pictures
- Picture to base64
H5 Call camera/album
The easiest way to call the camera is to use input file[camera ]
Properties:
<input type="file" capture=camera accept="image/*">//相机
<input type="file" accept="image/*">//相册
Copy after login
There is still a problem with the compatibility of this method. The camera can be opened normally on an iPhone, but after clicking on an Android phone, the camera, gallery, and file manager are displayed. and other mixed options. I searched a lot on the Internet but found no good solution, so I can only continue writing. . .
Image compression
Image compression requires
FileReader and
The FileReader object allows a web application to asynchronously read the contents of a file stored on the computer, using a File or Blob object to specify the file or data to be read.