CN117372551A - Image compression method, apparatus, device, storage medium, and program product - Google Patents

Image compression method, apparatus, device, storage medium, and program product Download PDF

Info

Publication number
CN117372551A
CN117372551A CN202311139473.5A CN202311139473A CN117372551A CN 117372551 A CN117372551 A CN 117372551A CN 202311139473 A CN202311139473 A CN 202311139473A CN 117372551 A CN117372551 A CN 117372551A
Authority
CN
China
Prior art keywords
image
compression
compressed image
canvas
initial
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202311139473.5A
Other languages
Chinese (zh)
Inventor
杨春晓
胡小磊
杨旭
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Bank of China Ltd
Original Assignee
Bank of China Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Bank of China Ltd filed Critical Bank of China Ltd
Priority to CN202311139473.5A priority Critical patent/CN117372551A/en
Publication of CN117372551A publication Critical patent/CN117372551A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06TIMAGE DATA PROCESSING OR GENERATION, IN GENERAL
    • G06T9/00Image coding
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/448Execution paradigms, e.g. implementations of programming paradigms
    • G06F9/4482Procedural

Abstract

The present application relates to an image compression method, apparatus, device, storage medium, and program product, and it is to be noted that the application of the image compression method, apparatus, device, storage medium, and program product is not limited, and the application may be applied to the financial field, and may be applied to any field other than the financial field. The method comprises the following steps: acquiring an initial compressed image, wherein the initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, the image data volume of the initial compressed image is larger than a preset data volume threshold, then detecting whether the image size of the initial compressed image accords with Canvas compression conditions, and if the image size of the initial compressed image accords with the Canvas compression conditions, performing Canvas compression on the initial compressed image to obtain a target compressed image, wherein the image data volume of the target compressed image is smaller than or equal to the preset data volume threshold. The compression reliability can be improved by adopting the method.

Description

Image compression method, apparatus, device, storage medium, and program product
Technical Field
The image compression method, apparatus, device, storage medium and program product according to the present application may be used in the financial field, and may be used in any field other than the financial field, and the application fields of the image compression method, apparatus, device, storage medium and program product according to the present application are not limited.
Background
In some online services, the terminal is typically required to transmit or store relevant images uploaded by the user, such as authentication services, online registration services, etc. Currently, since the transmission interface has a limited requirement on the size of the uploaded image and a limited storage space, the terminal needs to perform compression processing on the image in the process of providing the online service.
In the related art, a terminal generally calls a native API interface to compress an acquired image, taking an H5 applet as an example, and after a user inputs an image through an input control of an applet page, the terminal calls the native API interface of the H5 applet to compress the image.
However, the above-described image compression method has a problem of poor compression reliability.
Disclosure of Invention
In view of the foregoing, it is desirable to provide an image compression method, apparatus, device, storage medium, and program product capable of improving compression reliability.
In a first aspect, the present application provides an image compression method. The method comprises the following steps:
acquiring an initial compressed image, wherein the initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, and the image data volume of the initial compressed image is larger than a preset data volume threshold;
Detecting whether the image size of the initial compressed image meets Canvas compression conditions or not;
and if the image size of the initial compressed image meets the Canvas compression condition, carrying out Canvas compression processing on the initial compressed image to obtain a target compressed image, wherein the image data size of the target compressed image is smaller than or equal to a preset data size threshold.
In one embodiment, the Canvas compression processing is performed on the initial compressed image to obtain a target compressed image, including:
performing cyclic Canvas compression processing on the basis of the initial compressed image according to a preset image size compression ratio until a target compressed image is obtained;
wherein the image size compression ratio includes at least an image width compression ratio.
In one embodiment, according to a preset image size compression ratio, performing a cyclic Canvas compression process based on an initial compressed image until a target compressed image is obtained, including:
in a first cycle process, performing width compression processing on the first cycle compressed image according to an image width compression ratio, and performing length compression processing on the first cycle compressed image according to the image width compression ratio to obtain compressed data, wherein in the first cycle process, the first cycle compressed image is an initial compressed image;
Carrying out Canvas drawing processing on the compressed data to obtain a Canvas compressed image corresponding to the current circulation process;
and if the image data amount of the image after the Canvas compression processing is smaller than or equal to a preset data amount threshold value, taking the image after the Canvas compression processing as a target compressed image.
In one embodiment, the method further comprises:
and if the image data quantity of the image after Canvas compression is larger than a preset data quantity threshold, carrying out Canvas compression on the image after Canvas compression as a first cycle compression image corresponding to the next cycle process until a target compression image is obtained.
In one embodiment, detecting whether the image size of the initially compressed image meets Canvas compression conditions comprises:
detecting whether an image width value of an initial compressed image is larger than a preset image width value threshold value;
if the image width value of the initial compressed image is smaller than or equal to the image width value threshold, determining that the image size of the initial compressed image meets Canvas compression conditions.
In one embodiment, the method further comprises:
if the image width value of the initial compressed image is larger than the image width value threshold, compressing the image width of the initial compressed image to the image width indicated by the image width value threshold, and compressing the image length of the initial compressed image according to the compression ratio of the image width of the initial compressed image to obtain an intermediate compressed image;
And if the image data amount of the intermediate compressed image is larger than a preset data amount threshold, carrying out Canvas compression processing on the intermediate compressed image to obtain a target compressed image.
In one embodiment, the method further comprises:
and if the image data amount of the intermediate compressed image is smaller than or equal to a preset data amount threshold value, taking the intermediate compressed image as a target compressed image.
In one embodiment, acquiring an initial compressed image includes:
acquiring an original image based on the H5 page, wherein the original image has preset image quality;
and calling a preset API interface to perform cyclic quality compression processing based on the original image according to a preset image quality compression value until an initial compressed image is obtained.
In one embodiment, according to a preset image quality compression value, calling a preset API interface to perform cyclic quality compression processing based on an original image until an initial compressed image is obtained, including:
performing quality compression processing on the second cyclic compressed image according to the image quality compression value in a first cyclic process to obtain a quality compressed image, wherein the second cyclic compressed image is an original image in a first cyclic process;
And if the image quality of the image after the quality compression processing is zero and the image data amount of the image after the quality compression processing is larger than a preset data amount threshold value, taking the image after the quality compression processing as an initial compressed image.
In one embodiment, the method further comprises:
and if the image quality of the image after the quality compression processing is not zero and the image data amount of the image after the quality compression processing is larger than a preset data amount threshold value, taking the image after the quality compression processing as a second cycle compression image corresponding to the next cycle process to perform the quality compression processing until an initial compression image is obtained.
In a second aspect, the present application also provides an image compression apparatus. The device comprises:
the acquisition module is used for acquiring an initial compressed image, wherein the initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, and the image data size of the initial compressed image is larger than a preset data size threshold;
the detection module is used for detecting whether the image size of the initial compressed image accords with Canvas compression conditions;
and the compression module is used for carrying out Canvas compression processing on the initial compressed image to obtain a target compressed image if the image size of the initial compressed image meets Canvas compression conditions, wherein the image data size of the target compressed image is smaller than or equal to a preset data size threshold.
In a third aspect, embodiments of the present application provide a computer device comprising a memory storing a computer program and a processor implementing the steps of the method of the first aspect as described above when the processor executes the computer program.
In a fourth aspect, embodiments of the present application provide a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, implements the steps of the method of the first aspect described above.
In a fifth aspect, the present application also provides a computer program product. The computer program product comprising a computer program which, when executed by a processor, implements the steps of the method of the first aspect as described above.
According to the image compression method, the device, the equipment, the storage medium and the program product, the initial compression image is obtained by calling the preset API interface to perform quality compression on the original image, the image data size of the initial compression image is larger than the preset data size threshold, then whether the image size of the initial compression image meets Canvas compression conditions is detected, if the image size of the initial compression image meets the Canvas compression conditions, canvas compression processing is performed on the initial compression image to obtain the target compression image, and the image data size of the target compression image is smaller than or equal to the preset data size threshold. In this way, under the condition that the image data amount of the obtained initial compressed image still cannot be smaller than or equal to the preset data amount threshold value through the preset API interface, the embodiment of the application further performs Canvas compression processing on the initial compressed image to obtain the target compressed image with the image data amount smaller than or equal to the preset data amount threshold value, where the preset data amount threshold value may be, for example, an image data amount limiting value of the transmission interface on the uploaded image, or may be also an image data amount limiting value of the storage space of the terminal or the server on the uploaded image, so that the problem that the image transmission failure or the storage failure caused by the fact that the image compression reliability is poor (for example, the image data amount of the initial compressed image is still greater than the preset data amount threshold value) is avoided.
Drawings
FIG. 1 is a diagram of an application environment for an image compression method in one embodiment;
FIG. 2 is a flow chart of an image compression method in one embodiment;
FIG. 3 is a flow chart illustrating step 203 in another embodiment;
FIG. 4 is a flow chart of step 301 in another embodiment;
FIG. 5 is a flow chart of step 202 in another embodiment;
FIG. 6 is a schematic flow chart of preprocessing an initial compressed image by a terminal according to another embodiment;
FIG. 7 is a flow chart of step 201 in another embodiment;
FIG. 8 is a flowchart of a terminal compressing the original image in accordance with another embodiment;
FIG. 9 is a block diagram showing the structure of an image compression apparatus in one embodiment;
FIG. 10 is an internal block diagram of a computer device in one embodiment;
FIG. 11 is an internal block diagram of a computer device in one embodiment.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application will be further described in detail with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the present application.
In some online services, the terminal is typically required to transmit or store relevant images uploaded by the user, such as authentication services, online registration services, etc. Currently, since the transmission interface has a limited requirement on the size of the uploaded image and a limited storage space, the terminal needs to perform compression processing on the image in the process of providing the online service.
In the related art, a terminal generally calls a native API interface to compress an acquired image, taking an H5 applet as an example, and after a user inputs an image through an input control of an applet page, the terminal calls the native API interface of the H5 applet to compress the image.
However, the above-described image compression method has a problem of poor compression reliability.
In view of this, an embodiment of the present application proposes an image compression method, apparatus, device, storage medium, and program product, where an initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, and then detecting whether an image size of the initial compressed image meets Canvas compression conditions, and if the image size of the initial compressed image meets Canvas compression conditions, performing Canvas compression on the initial compressed image to obtain a target compressed image, where an image data size of the target compressed image is smaller than or equal to a preset data size threshold. In this way, under the condition that the image data amount of the obtained initial compressed image still cannot be smaller than or equal to the preset data amount threshold value through the preset API interface, the embodiment of the application further performs Canvas compression processing on the initial compressed image to obtain the target compressed image with the image data amount smaller than or equal to the preset data amount threshold value, where the preset data amount threshold value may be, for example, an image data amount limiting value of the transmission interface on the uploaded image, or may be also an image data amount limiting value of the storage space of the terminal or the server on the uploaded image, so that the problem that the image transmission failure or the storage failure caused by the fact that the image compression reliability is poor (for example, the image data amount of the initial compressed image is still greater than the preset data amount threshold value) is avoided.
The image compression method provided by the embodiment of the application can be applied to an application environment shown in fig. 1. Wherein the terminal 102 communicates with the server 104 via a network. The data storage system may store data that the server 104 needs to process. The data storage system may be integrated on the server 104 or may be located on a cloud or other network server. The terminal 102 may be, but not limited to, various personal computers, notebook computers, smart phones, tablet computers, internet of things devices, and portable wearable devices, where the internet of things devices may be smart speakers, smart televisions, smart air conditioners, smart vehicle devices, and the like. The portable wearable device may be a smart watch, smart bracelet, headset, or the like. The server 104 may be implemented as a stand-alone server or as a server cluster of multiple servers.
In one embodiment, as shown in fig. 2, an image compression method is provided, and the method is applied to the terminal 102 in fig. 1 for illustration, and includes the following steps:
in step 201, an initial compressed image is acquired.
In some online services, the terminal is usually required to transmit or store the relevant image uploaded by the user, so that when the relevant online service is provided, the terminal acquires an uncompressed original image.
Regarding the manner in which the terminal acquires the original image, optionally, a designated storage location is preset, and the terminal reads the image stored in the designated storage location as the original image; optionally, the terminal deploys an image uploading control on the current page, and acquires the original image input by the user through the image uploading control. Here, the method for acquiring the original image is not limited.
When the terminal acquires the original image and performs local storage processing, the storage space of the terminal generally has a certain limit on the data size (namely the image data amount) of the image, when the terminal acquires the original image and performs transmission, the transmission interface may have a certain limit on the image data amount of the transmission image, at this time, the terminal judges the size of the original image according to the limit of the storage space or the limit of the transmission interface by a preset data amount threshold, and optionally, when the image data amount of the original image is smaller than or equal to the preset data amount threshold, the terminal performs local storage processing or transmission processing on the original image; alternatively, the terminal needs to compress the original image when the image data amount of the original image is greater than a preset data amount threshold.
In the embodiment of the application, the initial compressed image is obtained by calling a preset API interface to perform quality compression on the original image, that is, after the terminal acquires the original image through the above manner, the terminal calls the preset API interface to perform quality compression on the original image.
Taking an H5 applet as an example, after the terminal obtains an original image input by a user through an image upload control disposed on an applet page, the terminal may call a native API interface (wx.com) of the H5 applet to perform quality compression on the obtained original image, to obtain an initial compressed image.
Wherein the image data amount of the initial compressed image is greater than a preset data amount threshold.
In some cases, the capability of compressing an image by a preset API interface is limited, so that the original image after being compressed often cannot be compressed to a data size smaller than or equal to a preset data amount threshold, so that an initial compressed image which still cannot meet the preset data amount threshold after being quality compressed by the preset API interface is further compressed.
Step 202, it is detected whether the image size of the initial compressed image meets Canvas compression conditions.
And the terminal calls a Canvas API interface, and presets Canvas compression conditions according to the size limit of the current page.
Illustratively, the compression condition of the Canvas is that the image width is less than 800px, and the terminal judges whether the initial compressed image meets the compression condition of the Canvas according to the image size of the initial compressed image.
In a possible implementation manner, when the width of the initial compressed image is smaller than 800px, the terminal may determine that the image size of the initial compressed image meets Canvas compression conditions, and then directly perform Canvas compression on the initial compressed image; in another possible embodiment, when the width of the initial compressed image is greater than 800px, the terminal may determine that the image size of the initial compressed image does not conform to the Canvas compression conditions, at which point the terminal needs to pre-process the initial compressed image to conform to the Canvas compression conditions.
Regarding the preprocessing mode, taking the Canvas compression condition as an image width less than 800px as an example, the terminal performs size compression on the initial compressed image, firstly compresses the width to 800px, then compresses the length of the initial compressed image to a corresponding length according to the ratio of the compressed width, and exemplarily, the initial compressed image has a width of 1000px and a length of 1500px, the terminal sets the width of the initial compressed image to 800px and the compression ratio to 80%, and then compresses the length of the initial compressed image to 1200px according to the compression ratio.
After the initial compression image accords with the compression condition of Canvas, optionally, the terminal directly carries out Canvas compression on the initial compression image subjected to size compression; optionally, in order to make the initial compressed image conform to the compression condition of Canvas, the terminal performs size compression on the initial compressed image when performing preprocessing, in order to avoid resource waste, the terminal determines the initial compressed image after size compression according to a preset data volume threshold, if the data size of the initial compressed image after size compression is smaller than or equal to the preset data volume threshold, the terminal performs local storage processing or transmission processing on the initial compressed image, and if the data size of the initial compressed image after size compression is larger than the preset data volume threshold, the terminal performs Canvas compression on the initial compressed image.
And 203, if the image size of the initial compressed image meets the Canvas compression condition, performing Canvas compression processing on the initial compressed image to obtain a target compressed image.
Canvas is an API interface, which is often used to draw a corresponding image according to data, and the data size of the image can be reduced in the drawing process, so in the embodiment of the application, the terminal draws an initial compressed image by calling the Canvas API, so as to achieve the purpose of further compression.
And the terminal performs Canvas compression processing on the acquired initial compressed image, namely, generates an image data stream which can be processed by the Canvas API according to the initial compressed image, and then calls the Canvas API to draw according to the image data stream so as to obtain a target compressed image, wherein the image data amount of the target compressed image is smaller than or equal to a preset data amount threshold value.
After obtaining the target compressed image, the terminal optionally performs body storage processing on the target compressed image, and optionally, the terminal uploads the target compressed image to the server through the transmission interface.
In a possible implementation manner, after the terminal performs a round of Canvas compression on the initial compressed image, an intermediate compressed image with the image data volume smaller than or equal to a preset data volume threshold value can be obtained, and the terminal can perform local storage processing or transmission processing by taking the intermediate compressed image as a target compressed image; in another possible implementation manner, the image data amount of the intermediate compressed image obtained after the terminal performs one round of Canvas compression on the initial compressed image is greater than a preset data amount threshold, at this time, the terminal needs to further perform Canvas compression on the intermediate compressed image, and finally obtains the intermediate compressed image with the image data amount smaller than or equal to the preset data amount threshold, and the terminal can perform local storage processing or transmission processing on the intermediate compressed image as the target compressed image.
According to the image compression method, the initial compression image is obtained by calling the preset API interface to perform quality compression on the original image, the image data size of the initial compression image is larger than the preset data size threshold, whether the image size of the initial compression image meets Canvas compression conditions or not is detected, and if the image size of the initial compression image meets the Canvas compression conditions, canvas compression processing is performed on the initial compression image to obtain the target compression image, wherein the image data size of the target compression image is smaller than or equal to the preset data size threshold. In this way, under the condition that the image data amount of the obtained initial compressed image still cannot be smaller than or equal to the preset data amount threshold value through the preset API interface, the embodiment of the application further performs Canvas compression processing on the initial compressed image to obtain the target compressed image with the image data amount smaller than or equal to the preset data amount threshold value, where the preset data amount threshold value may be, for example, an image data amount limiting value of the transmission interface on the uploaded image, or may be also an image data amount limiting value of the storage space of the terminal or the server on the uploaded image, so that the problem that the image transmission failure or the storage failure caused by the fact that the image compression reliability is poor (for example, the image data amount of the initial compressed image is still greater than the preset data amount threshold value) is avoided.
In one embodiment, referring to fig. 3, referring to the embodiment shown in fig. 2, the embodiment of the present application refers to a process of performing Canvas compression on an initial compressed image to obtain a target compressed image. As shown in fig. 3, step 203 may include step 301 shown in fig. 3.
Step 301, performing cyclic Canvas compression processing based on the initial compressed image according to a preset image size compression ratio until a target compressed image is obtained.
In a possible implementation manner, after the terminal performs a round of Canvas compression on the initial compressed image, an intermediate compressed image with the image data volume smaller than or equal to a preset data volume threshold value can be obtained, and the terminal can perform local storage processing or transmission processing by taking the intermediate compressed image as a target compressed image; in another possible implementation manner, the image data size of the intermediate compressed image obtained after the terminal performs a round of Canvas compression on the initial compressed image is greater than a preset data size threshold, at this time, the terminal needs to further perform Canvas compression on the intermediate compressed image, and finally obtains the intermediate compressed image with the image data size less than or equal to the preset data size threshold.
In one possible implementation, the terminal further performs Canvas compression on the initial compressed image, where the image size compression ratio includes at least an image width compression ratio, and in each cycle of Canvas compression, the terminal adjusts the size of the intermediate compressed image according to the preset image size compression ratio, and then performs Canvas compression on the intermediate compressed image after performing the size compression to obtain an intermediate compressed image with smaller data size, so that after a few cycles, an intermediate compressed image with an image data size less than or equal to a preset data size threshold is obtained as a target compressed image.
In this way, in the above embodiment, the terminal calls the Canvas API to perform the Canvas compression process on the initial compressed image cycle Canvas according to the preset image size compression ratio, so as to finally obtain the target compressed image with the image data volume smaller than or equal to the preset data volume threshold, thereby improving the compression reliability and avoiding the situation that the user re-uploads the original image multiple times due to failure of the image uploading server or storage failure.
In one embodiment, referring to fig. 4, based on the embodiment shown in fig. 3, the embodiment of the present application refers to a process of performing a cyclic Canvas compression process based on an initial compressed image according to a preset image size compression ratio until a target compressed image is obtained. As shown in fig. 4, step 301 may include steps 401 through 403 shown in fig. 4.
Step 401, in a one-time cyclic process, performing a width compression process on the first cyclic compressed image according to an image width compression ratio, and performing a length compression process on the first cyclic compressed image according to the image width compression ratio, to obtain compressed data.
Wherein, in the first cycle process, the first cycle compressed image is an initial compressed image.
For a first cyclic process, the terminal firstly performs size processing on the first cyclic compressed image according to a preset image width compression ratio, and the image width compression ratio is illustratively 20%, that is, the size ratio of the first cyclic compressed image needs to be reduced by 20% at first by the cyclic terminal of each round, for example, the width of the first cyclic compressed image is 800px, the length of the first cyclic compressed image is 1200px, the width and the length of the first cyclic compressed image are respectively reduced by 20% according to the image width compression ratio, and the obtained image size is 640px and 960px.
And then, the terminal processes the first cycle compressed image after size compression to obtain a data stream which can be used by the Canvas API, namely compressed data.
And step 402, carrying out Canvas drawing processing on the compressed data to obtain a Canvas compression processed image corresponding to the current circulation process.
And the terminal calls a Canvas API to draw the compressed data, and the drawn image is the image after Canvas compression processing corresponding to the previous cycle process.
Step 403, if the image data size of the image after Canvas compression is less than or equal to the preset data size threshold, the image after Canvas compression is taken as the target compressed image.
After one cycle is finished, the terminal needs to judge the image obtained in the current cycle according to a preset data quantity threshold value.
When the image data volume of the image after Canvas compression is smaller than or equal to a preset data volume threshold value, the image after Canvas compression is used as a target compressed image; and if the image data quantity of the image after Canvas compression is larger than a preset data quantity threshold, carrying out Canvas compression on the image after Canvas compression as a first cycle compression image corresponding to the next cycle process until a target compression image is obtained.
In this way, in the above embodiment, the terminal compresses the initial compressed image according to the image width compression ratio by using the cyclic Canvas compression method, instead of directly setting a larger compression size or setting a smaller compression size, so as to avoid the problem that the one-time compression is unsuccessful, improve the compression reliability, and avoid the problem that the user uploads the image for many times due to compression failure.
In one embodiment, based on the embodiment shown in FIG. 2, referring to FIG. 5, the present embodiment relates to a process of detecting whether the image size of an initially compressed image meets Canvas compression conditions. As shown in fig. 5, step 202 may include step 501 and step 502 shown in fig. 5.
In step 501, it is detected whether the image width value of the initial compressed image is greater than a preset image width value threshold.
In one possible implementation, before the terminal compresses the initial compressed image through the Canvas API, it is first required to determine whether the size of the initial compressed image meets the Canvas compression condition, where the Canvas compression condition is that the image width value is less than or equal to 800px.
Step 502, if the image width value of the initial compressed image is less than or equal to the image width value threshold, determining that the image size of the initial compressed image meets the Canvas compression condition.
And under the condition that the image width value of the initial compressed image is smaller than or equal to the image width value threshold, representing that the initial compressed image meets Canvas compression conditions, and at the moment, the terminal directly performs Canvas compression on the initial compressed image.
In this way, the terminal determines an initial compressed image capable of Canvas compression according to Canvas compression conditions.
In one embodiment, referring to fig. 6, based on the embodiment shown in fig. 5, the embodiment of the present application refers to a process in which a terminal performs preprocessing on an initial compressed image in a case where an image width value of the initial compressed image is greater than an image width value threshold. As shown in fig. 6, the process includes steps 601 and 602.
And 601, if the image width value of the initial compressed image is greater than the image width value threshold, compressing the image width of the initial compressed image to the image width indicated by the image width value threshold, and performing compression processing on the image length of the initial compressed image according to the compression ratio of the image width of the initial compressed image to obtain an intermediate compressed image.
For example, when the image width indicated by the image width threshold is 800px and the actual width of the initial compressed image is 1000px and the length is 1500px, the terminal first compresses the width of the initial compressed image to 800px, then divides the compression ratio, that is, 800px by 1000px to 80%, and multiplies 80% by the length, that is, 1500px by 80%, to obtain the intermediate compressed image, where the length of the initial compressed image after compression should be 1200px, and the terminal compresses the length of the initial compressed image to 1200px, and the size of the intermediate compressed image should be 800px and 1200px.
Step 602, if the image data amount of the intermediate compressed image is greater than the preset data amount threshold, canvas compression is performed on the intermediate compressed image to obtain a target compressed image.
In a possible implementation manner, the size compression also causes the image data size of the image to be smaller, so, in order to avoid resource waste, the terminal judges the intermediate compressed image subjected to the size compression according to a preset data size threshold, and if the image data size of the intermediate compressed image is larger than the preset data size threshold, the terminal performs Canvas compression on the initial compressed image; and if the image data amount of the intermediate compressed image is smaller than or equal to the preset data amount threshold, taking the intermediate compressed image as a target compressed image, and locally storing the target compressed image or carrying out transmission processing by the terminal.
In this way, in the above embodiment, the terminal performs preprocessing on the initial compressed image according to the Canvas compression condition, performs primary size compression on the initial compressed image which does not meet the Canvas compression condition to obtain the intermediate compressed image, and performs primary judgment on the intermediate compressed image according to the preset data amount threshold, if the image data amount of the intermediate compressed image is less than or equal to the preset data amount threshold, the intermediate compressed image is uploaded to the server as the target compressed image, so that the waste of compression resources is avoided, and the compression efficiency is improved.
In one embodiment, referring to fig. 7, based on the embodiment shown in fig. 2, the embodiment of the present application relates to a process of acquiring an initial compressed image. As shown in fig. 7, step 201 includes step 701 and step 702.
Step 701, acquiring an original image based on an H5 page.
In one possible implementation, the terminal acquires an original image, i.e., an image that has not been compressed, based on the H5 page, wherein the original image has a preset image quality.
The image quality is a parameter referred to when the terminal performs quality compression on the original image by calling a preset API interface, and the preset image quality is that of the original image, namely the image quality parameter of the original image, and in one possible implementation, the value of the preset image quality is 100.
For the mode of acquiring the original image by the terminal, optionally, deploying an image uploading control on the H5 page, and acquiring the original image input by the user by the terminal through the image uploading control; alternatively, a designated storage location is preset, and the terminal reads the image stored in the designated storage location as an original image. The manner in which the terminal acquires the original image is not limited herein.
Step 702, calling a preset API interface to perform cyclic quality compression processing based on the original image according to a preset image quality compression value until an initial compressed image is obtained.
In one possible implementation, the image quality compression value is a value of the image quality reduction during each cycle of the quality compression process.
For example, for a primary cycle quality compression process, the terminal presets an image quality compression value of 20, the preset image quality value of the original image quality is 100, the image quality of the original image is reduced to 80 first, then, the terminal calls a preset API to perform quality compression on the original image with the image quality value of 80, and if the image data amount of the compressed image is greater than a preset data amount threshold value, the next cycle quality compression is performed.
In one possible implementation manner, in the process of the cyclic quality compression processing, if the image data amount of the image obtained by the terminal after one cycle quality compression processing is less than or equal to a preset data amount threshold, the image is used as a target compressed image to perform local storage processing or transmission processing; if the terminal performs one-time cyclic quality compression, the image quality value of the original image is 0, but the image data amount of the image after quality compression is still larger than the preset data amount threshold, the terminal takes the compressed image as an initial compressed image, and performs Canvas compression on the initial compressed image to obtain a target compressed image smaller than or equal to the preset data amount threshold.
In this way, in the above embodiment, the terminal does not perform Canvas compression on the acquired original image at the first time, but performs quality compression on the original image by calling the preset API interface, and if the image obtained by quality compression can meet the preset data amount threshold, the image obtained by quality compression is directly used as the target compressed image to perform local storage processing or transmission processing, so that the risk of distortion of the compressed image is reduced, and the reliability of compression is improved.
In one embodiment, based on the embodiment shown in fig. 7, referring to fig. 8, the embodiment of the application refers to a process of calling a preset API interface to perform cyclic quality compression processing based on an original image according to a preset image quality compression value until an initial compressed image is obtained. As shown in fig. 8, step 802 includes step 801 and step 802.
In step 801, in a one-time cyclic process, quality compression processing is performed on the second cyclic compressed image according to the image quality compression value, so as to obtain a quality compressed image.
It can be understood that firstly, judging the size of the image data of the original image, if the image data of the original image is smaller than or equal to a preset data amount threshold, the terminal does not need to compress the original image at this time, and directly stores the original image locally or transmits the original image; if the image data amount of the original image is larger than the preset data amount threshold, the terminal calls a preset API interface to perform quality compression on the original image.
When the terminal calls a preset API interface to perform cyclic quality compression on the original image, the terminal takes the original image as a second cyclic compressed image and starts cyclic quality compression. For one round of cyclic quality compression, the terminal reduces the image quality value of the second compressed image by a preset image quality compression value, then performs quality compression on the second compressed image according to the reduced image quality value to obtain an intermediate quality compressed image, judges the image data size of the intermediate quality compressed image, and takes the intermediate quality compressed image as a target compressed image for local storage processing or transmission processing if the image data size of the intermediate quality compressed image is smaller than or equal to a preset data size threshold value; and if the image data amount of the intermediate quality compressed image is larger than a preset data amount threshold value, taking the intermediate quality compressed image as a second cycle compressed image of the next cycle to perform quality compression.
Step 802, if the image quality of the image after the quality compression processing is zero and the image data amount of the image after the quality compression processing is greater than the preset data amount threshold, the image after the quality compression processing is used as the initial compressed image.
When the terminal compresses the intermediate quality compressed image for a certain time, the image quality of the intermediate quality compressed image is 0, and the image data volume is still larger than the preset data volume threshold, the preset API interface is represented as incapable of compressing the image data volume of the original image to the preset data volume threshold, at this time, the terminal takes the intermediate quality compressed image after the cyclic quality compression processing as an initial compressed image, calls a Canvas API, and compresses the initial compressed image by Canvas.
And if the image quality of the image after the quality compression processing is not zero and the image data amount of the image after the quality compression processing is larger than a preset data amount threshold value, taking the image after the quality compression processing as a second cycle compression image corresponding to the next cycle process to perform the quality compression processing until an initial compression image is obtained.
In this way, in the above embodiment, the terminal continuously performs quality compression on the obtained original image by calling the preset API interface, and if the image obtained by quality compression can meet the preset data amount threshold, the image obtained by quality compression is directly used as the target compressed image to be locally stored or transmitted, so that the risk of distortion of the compressed image is reduced, and the reliability of compression is improved.
In one embodiment, an image compression method is provided for a terminal 102, including the steps of:
and a step a, acquiring an original image based on the H5 page, wherein the original image has preset image quality.
And b, performing quality compression processing on the second cyclic compressed image according to the image quality compression value in the first cyclic process to obtain a quality compressed image, wherein the second cyclic compressed image is an original image in the first cyclic process.
And c, if the image quality of the image after the quality compression processing is zero and the image data amount of the image after the quality compression processing is larger than a preset data amount threshold value, taking the image after the quality compression processing as an initial compressed image.
And d, if the image quality of the image after the quality compression processing is not zero and the image data amount of the image after the quality compression processing is larger than a preset data amount threshold, taking the image after the quality compression processing as a second cycle compression image corresponding to the next cycle process to perform the quality compression processing until an initial compression image is obtained.
The initial compressed image is obtained by calling a preset API interface to perform quality compression on the original image, and the image data size of the initial compressed image is larger than a preset data size threshold.
And e, detecting whether the image width value of the initial compressed image is larger than a preset image width value threshold value.
And f, if the image width value of the initial compressed image is smaller than or equal to the image width value threshold, determining that the image size of the initial compressed image meets the Canvas compression condition.
And g, if the image width value of the initial compressed image is larger than the image width value threshold, compressing the image width of the initial compressed image to the image width indicated by the image width value threshold, and compressing the image length of the initial compressed image according to the compression ratio of the image width of the initial compressed image to obtain an intermediate compressed image.
And h, if the image data quantity of the intermediate compressed image is larger than a preset data quantity threshold value, carrying out Canvas compression processing on the intermediate compressed image to obtain a target compressed image.
And i, if the image data amount of the intermediate compressed image is smaller than or equal to a preset data amount threshold value, taking the intermediate compressed image as a target compressed image.
And j, in the first circulation process, performing width compression processing on the first circulation compressed image according to the image width compression ratio, and performing length compression processing on the first circulation compressed image according to the image width compression ratio to obtain compressed data, wherein in the first circulation process, the first circulation compressed image is an initial compressed image.
And step k, carrying out Canvas drawing processing on the compressed data to obtain a Canvas compression processed image corresponding to the current circulation process.
And step l, if the image data amount of the image after the Canvas compression processing is smaller than or equal to a preset data amount threshold value, taking the image after the Canvas compression processing as a target compressed image.
And m, if the image data quantity of the image after Canvas compression processing is larger than a preset data quantity threshold value, carrying out Canvas compression processing on the image after Canvas compression processing as a first cycle compression image corresponding to the next cycle process until a target compression image is obtained.
Wherein the image size compression ratio includes at least an image width compression ratio.
Wherein the image data amount of the target compressed image is less than or equal to a preset data amount threshold.
It should be understood that, although the steps in the flowcharts related to the embodiments described above are sequentially shown as indicated by arrows, these steps are not necessarily sequentially performed in the order indicated by the arrows. The steps are not strictly limited to the order of execution unless explicitly recited herein, and the steps may be executed in other orders. Moreover, at least some of the steps in the flowcharts described in the above embodiments may include a plurality of steps or a plurality of stages, which are not necessarily performed at the same time, but may be performed at different times, and the order of the steps or stages is not necessarily performed sequentially, but may be performed alternately or alternately with at least some of the other steps or stages.
Based on the same inventive concept, the embodiment of the application also provides an image compression device for realizing the r image compression method. The implementation of the solution provided by the device is similar to the implementation described in the above method, so the specific limitation in one or more embodiments of the image compression device provided below may refer to the limitation of the image compression method hereinabove, and will not be repeated herein.
In one embodiment, as shown in fig. 9, there is provided an image compression apparatus including: an acquisition module 901, a detection module 902, and a compression module 903, wherein:
the acquiring module 901 is configured to acquire an initial compressed image, where the initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, and an image data amount of the initial compressed image is greater than a preset data amount threshold;
a detection module 902, configured to detect whether an image size of the initial compressed image meets Canvas compression conditions;
the compression module 903 performs Canvas compression processing on the initial compressed image if the image size of the initial compressed image meets the Canvas compression condition, so as to obtain a target compressed image, where the image data amount of the target compressed image is less than or equal to the preset data amount threshold.
In one embodiment, the compression module 903 includes:
the cyclic Canvas compression unit is used for carrying out cyclic Canvas compression processing on the basis of the initial compressed image according to a preset image size compression ratio until the target compressed image is obtained;
wherein the image size compression ratio includes at least an image width compression ratio.
In one embodiment, the cyclic Canvas compression unit comprises:
in a first cyclic process, performing width compression processing on a first cyclic compressed image according to the image width compression ratio, and performing length compression processing on the first cyclic compressed image according to the image width compression ratio to obtain compressed data, wherein in the first cyclic process, the first cyclic compressed image is the initial compressed image;
carrying out Canvas drawing processing on the compressed data to obtain a Canvas compressed image corresponding to the current circulation process;
and if the image data quantity of the image after the Canvas compression processing is smaller than or equal to the preset data quantity threshold value, taking the image after the Canvas compression processing as the target compressed image.
In one embodiment, the cyclic Canvas compression unit further comprises:
And if the image data quantity of the image after Canvas compression is larger than the preset data quantity threshold, carrying out Canvas compression on the image after Canvas compression as a first cycle compression image corresponding to the next cycle process until the target compression image is obtained.
In one embodiment, the detection module 902 includes:
a threshold detection unit for detecting whether the image width value of the initial compressed image is larger than a preset image width value threshold;
and the determining unit is used for determining that the image size of the initial compressed image meets the Canvas compression condition if the image width value of the initial compressed image is smaller than or equal to the image width value threshold.
In one embodiment, the detection module 902 further comprises:
the size compression unit compresses the image width of the initial compressed image to the image width indicated by the image width value threshold value if the image width value of the initial compressed image is larger than the image width value threshold value, and compresses the image length of the initial compressed image according to the compression ratio of the image width of the initial compressed image to obtain an intermediate compressed image;
And the Canvas compression unit is used for carrying out Canvas compression processing on the intermediate compressed image to obtain the target compressed image if the image data quantity of the intermediate compressed image is larger than the preset data quantity threshold value.
In one embodiment, the detection module 902 further comprises:
and the judging unit is used for taking the intermediate compressed image as the target compressed image if the image data amount of the intermediate compressed image is smaller than or equal to the preset data amount threshold value.
In one embodiment, the acquisition module 901 includes:
an original image acquisition unit for acquiring the original image based on an H5 page, wherein the original image has preset image quality;
and the quality compression unit calls the preset API interface to perform cyclic quality compression processing based on the original image according to a preset image quality compression value until the initial compressed image is obtained.
In one embodiment, the mass compression unit comprises:
performing quality compression processing on a second cyclic compressed image according to the image quality compression value in a first cyclic process to obtain a quality compressed image, wherein in the first cyclic process, the second cyclic compressed image is the original image;
And if the image quality of the image after the quality compression processing is zero and the image data amount of the image after the quality compression processing is larger than the preset data amount threshold value, taking the image after the quality compression processing as the initial compression image.
In one embodiment, the mass compression unit further comprises:
and if the image quality of the image after the quality compression processing is not zero and the image data amount of the image after the quality compression processing is larger than the preset data amount threshold value, taking the image after the quality compression processing as a second cycle compression image corresponding to the next cycle process to perform the quality compression processing until the initial compression image is obtained.
The respective modules in the above-described image compression apparatus may be implemented in whole or in part by software, hardware, and combinations thereof. The above modules may be embedded in hardware or may be independent of a processor in the computer device, or may be stored in software in a memory in the computer device, so that the processor may call and execute operations corresponding to the above modules.
In one embodiment, a computer device is provided, which may be a server, and the internal structure of which may be as shown in fig. 10. The computer device includes a processor, a memory, an Input/Output interface (I/O) and a communication interface. The processor, the memory and the input/output interface are connected through a system bus, and the communication interface is connected to the system bus through the input/output interface. Wherein the processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, computer programs, and a database. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage media. The database of the computer device is for storing image compression data. The input/output interface of the computer device is used to exchange information between the processor and the external device. The communication interface of the computer device is used for communicating with an external terminal through a network connection. The computer program is executed by a processor to implement an image compression method.
In one embodiment, a computer device is provided, which may be a terminal, and the internal structure thereof may be as shown in fig. 11. The computer device includes a processor, a memory, an input/output interface, a communication interface, a display unit, and an input means. The processor, the memory and the input/output interface are connected through a system bus, and the communication interface, the display unit and the input device are connected to the system bus through the input/output interface. Wherein the processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage media. The input/output interface of the computer device is used to exchange information between the processor and the external device. The communication interface of the computer device is used for carrying out wired or wireless communication with an external terminal, and the wireless mode can be realized through WIFI, a mobile cellular network, NFC (near field communication) or other technologies. The computer program is executed by a processor to implement an image compression method. The display unit of the computer device is used for forming a visual picture, and can be a display screen, a projection device or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer equipment can be a touch layer covered on the display screen, can also be a key, a track ball or a touch pad arranged on the shell of the computer equipment, and can also be an external keyboard, a touch pad or a mouse and the like.
It will be appreciated by those skilled in the art that the structures shown in fig. 10 and 11 are block diagrams of only some of the structures associated with the present application and are not intended to limit the computer device to which the present application may be applied, and that a particular computer device may include more or fewer components than shown, or may combine certain components, or have a different arrangement of components.
In one embodiment, a computer device is provided comprising a memory and a processor, the memory having stored therein a computer program, the processor when executing the computer program performing the steps of:
acquiring an initial compressed image, wherein the initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, and the image data size of the initial compressed image is larger than a preset data size threshold;
detecting whether the image size of the initial compressed image meets Canvas compression conditions or not;
and if the image size of the initial compressed image meets the Canvas compression condition, carrying out Canvas compression processing on the initial compressed image to obtain a target compressed image, wherein the image data size of the target compressed image is smaller than or equal to the preset data size threshold.
In one embodiment, the processor when executing the computer program further performs the steps of:
performing cyclic Canvas compression processing on the basis of the initial compressed image according to a preset image size compression ratio until the target compressed image is obtained;
wherein the image size compression ratio includes at least an image width compression ratio.
In one embodiment, the processor when executing the computer program further performs the steps of:
in a first cyclic process, performing width compression processing on a first cyclic compressed image according to the image width compression ratio, and performing length compression processing on the first cyclic compressed image according to the image width compression ratio to obtain compressed data, wherein in the first cyclic process, the first cyclic compressed image is the initial compressed image;
carrying out Canvas drawing processing on the compressed data to obtain a Canvas compressed image corresponding to the current circulation process;
and if the image data quantity of the image after the Canvas compression processing is smaller than or equal to the preset data quantity threshold value, taking the image after the Canvas compression processing as the target compressed image.
In one embodiment, the processor when executing the computer program further performs the steps of:
And if the image data quantity of the image after Canvas compression is larger than the preset data quantity threshold, carrying out Canvas compression on the image after Canvas compression as a first cycle compression image corresponding to the next cycle process until the target compression image is obtained.
In one embodiment, the processor when executing the computer program further performs the steps of:
detecting whether the image width value of the initial compressed image is larger than a preset image width value threshold value or not;
and if the image width value of the initial compressed image is smaller than or equal to the image width value threshold, determining that the image size of the initial compressed image meets the Canvas compression condition.
In one embodiment, the processor when executing the computer program further performs the steps of:
if the image width value of the initial compressed image is larger than the image width value threshold, compressing the image width of the initial compressed image to the image width indicated by the image width value threshold, and compressing the image length of the initial compressed image according to the compression ratio of the image width of the initial compressed image to obtain an intermediate compressed image;
And if the image data amount of the intermediate compressed image is larger than the preset data amount threshold, carrying out Canvas compression processing on the intermediate compressed image to obtain the target compressed image.
In one embodiment, the processor when executing the computer program further performs the steps of:
and if the image data amount of the intermediate compressed image is smaller than or equal to the preset data amount threshold, taking the intermediate compressed image as the target compressed image.
In one embodiment, the processor when executing the computer program further performs the steps of:
acquiring the original image based on an H5 page, wherein the original image has preset image quality;
and calling the preset API interface to perform cyclic quality compression processing based on the original image according to a preset image quality compression value until the initial compressed image is obtained.
In one embodiment, the processor when executing the computer program further performs the steps of:
performing quality compression processing on a second cyclic compressed image according to the image quality compression value in a first cyclic process to obtain a quality compressed image, wherein in the first cyclic process, the second cyclic compressed image is the original image;
And if the image quality of the image after the quality compression processing is zero and the image data amount of the image after the quality compression processing is larger than the preset data amount threshold value, taking the image after the quality compression processing as the initial compression image.
In one embodiment, the processor when executing the computer program further performs the steps of:
and if the image quality of the image after the quality compression processing is not zero and the image data amount of the image after the quality compression processing is larger than the preset data amount threshold value, taking the image after the quality compression processing as a second cycle compression image corresponding to the next cycle process to perform the quality compression processing until the initial compression image is obtained.
In one embodiment, a computer readable storage medium is provided having a computer program stored thereon, which when executed by a processor, performs the steps of:
acquiring an initial compressed image, wherein the initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, and the image data size of the initial compressed image is larger than a preset data size threshold;
detecting whether the image size of the initial compressed image meets Canvas compression conditions or not;
And if the image size of the initial compressed image meets the Canvas compression condition, carrying out Canvas compression processing on the initial compressed image to obtain a target compressed image, wherein the image data size of the target compressed image is smaller than or equal to the preset data size threshold.
In one embodiment, the computer program when executed by the processor further performs the steps of:
performing cyclic Canvas compression processing on the basis of the initial compressed image according to a preset image size compression ratio until the target compressed image is obtained;
wherein the image size compression ratio includes at least an image width compression ratio.
In one embodiment, the computer program when executed by the processor further performs the steps of:
in a first cyclic process, performing width compression processing on a first cyclic compressed image according to the image width compression ratio, and performing length compression processing on the first cyclic compressed image according to the image width compression ratio to obtain compressed data, wherein in the first cyclic process, the first cyclic compressed image is the initial compressed image;
carrying out Canvas drawing processing on the compressed data to obtain a Canvas compressed image corresponding to the current circulation process;
And if the image data quantity of the image after the Canvas compression processing is smaller than or equal to the preset data quantity threshold value, taking the image after the Canvas compression processing as the target compressed image.
In one embodiment, the computer program when executed by the processor further performs the steps of:
and if the image data quantity of the image after Canvas compression is larger than the preset data quantity threshold, carrying out Canvas compression on the image after Canvas compression as a first cycle compression image corresponding to the next cycle process until the target compression image is obtained.
In one embodiment, the computer program when executed by the processor further performs the steps of:
detecting whether the image width value of the initial compressed image is larger than a preset image width value threshold value or not;
and if the image width value of the initial compressed image is smaller than or equal to the image width value threshold, determining that the image size of the initial compressed image meets the Canvas compression condition.
In one embodiment, the computer program when executed by the processor further performs the steps of:
if the image width value of the initial compressed image is larger than the image width value threshold, compressing the image width of the initial compressed image to the image width indicated by the image width value threshold, and compressing the image length of the initial compressed image according to the compression ratio of the image width of the initial compressed image to obtain an intermediate compressed image;
And if the image data amount of the intermediate compressed image is larger than the preset data amount threshold, carrying out Canvas compression processing on the intermediate compressed image to obtain the target compressed image.
In one embodiment, the computer program when executed by the processor further performs the steps of:
and if the image data amount of the intermediate compressed image is smaller than or equal to the preset data amount threshold, taking the intermediate compressed image as the target compressed image.
In one embodiment, the computer program when executed by the processor further performs the steps of:
acquiring the original image based on an H5 page, wherein the original image has preset image quality;
and calling the preset API interface to perform cyclic quality compression processing based on the original image according to a preset image quality compression value until the initial compressed image is obtained.
In one embodiment, the computer program when executed by the processor further performs the steps of:
performing quality compression processing on a second cyclic compressed image according to the image quality compression value in a first cyclic process to obtain a quality compressed image, wherein in the first cyclic process, the second cyclic compressed image is the original image;
And if the image quality of the image after the quality compression processing is zero and the image data amount of the image after the quality compression processing is larger than the preset data amount threshold value, taking the image after the quality compression processing as the initial compression image.
In one embodiment, the computer program when executed by the processor further performs the steps of:
and if the image quality of the image after the quality compression processing is not zero and the image data amount of the image after the quality compression processing is larger than the preset data amount threshold value, taking the image after the quality compression processing as a second cycle compression image corresponding to the next cycle process to perform the quality compression processing until the initial compression image is obtained.
In one embodiment, a computer program product is provided comprising a computer program which, when executed by a processor, performs the steps of:
acquiring an initial compressed image, wherein the initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, and the image data size of the initial compressed image is larger than a preset data size threshold;
detecting whether the image size of the initial compressed image meets Canvas compression conditions or not;
And if the image size of the initial compressed image meets the Canvas compression condition, carrying out Canvas compression processing on the initial compressed image to obtain a target compressed image, wherein the image data size of the target compressed image is smaller than or equal to the preset data size threshold.
In one embodiment, the computer program when executed by the processor further performs the steps of:
performing cyclic Canvas compression processing on the basis of the initial compressed image according to a preset image size compression ratio until the target compressed image is obtained;
wherein the image size compression ratio includes at least an image width compression ratio.
In one embodiment, the computer program when executed by the processor further performs the steps of:
in a first cyclic process, performing width compression processing on a first cyclic compressed image according to the image width compression ratio, and performing length compression processing on the first cyclic compressed image according to the image width compression ratio to obtain compressed data, wherein in the first cyclic process, the first cyclic compressed image is the initial compressed image;
carrying out Canvas drawing processing on the compressed data to obtain a Canvas compressed image corresponding to the current circulation process;
And if the image data quantity of the image after the Canvas compression processing is smaller than or equal to the preset data quantity threshold value, taking the image after the Canvas compression processing as the target compressed image.
In one embodiment, the computer program when executed by the processor further performs the steps of:
and if the image data quantity of the image after Canvas compression is larger than the preset data quantity threshold, carrying out Canvas compression on the image after Canvas compression as a first cycle compression image corresponding to the next cycle process until the target compression image is obtained.
In one embodiment, the computer program when executed by the processor further performs the steps of:
detecting whether the image width value of the initial compressed image is larger than a preset image width value threshold value or not;
and if the image width value of the initial compressed image is smaller than or equal to the image width value threshold, determining that the image size of the initial compressed image meets the Canvas compression condition.
In one embodiment, the computer program when executed by the processor further performs the steps of:
if the image width value of the initial compressed image is larger than the image width value threshold, compressing the image width of the initial compressed image to the image width indicated by the image width value threshold, and compressing the image length of the initial compressed image according to the compression ratio of the image width of the initial compressed image to obtain an intermediate compressed image;
And if the image data amount of the intermediate compressed image is larger than the preset data amount threshold, carrying out Canvas compression processing on the intermediate compressed image to obtain the target compressed image.
In one embodiment, the computer program when executed by the processor further performs the steps of:
and if the image data amount of the intermediate compressed image is smaller than or equal to the preset data amount threshold, taking the intermediate compressed image as the target compressed image.
In one embodiment, the computer program when executed by the processor further performs the steps of:
acquiring the original image based on an H5 page, wherein the original image has preset image quality;
and calling the preset API interface to perform cyclic quality compression processing based on the original image according to a preset image quality compression value until the initial compressed image is obtained.
In one embodiment, the computer program when executed by the processor further performs the steps of:
performing quality compression processing on a second cyclic compressed image according to the image quality compression value in a first cyclic process to obtain a quality compressed image, wherein in the first cyclic process, the second cyclic compressed image is the original image;
And if the image quality of the image after the quality compression processing is zero, taking the image after the quality compression processing as the initial compressed image.
In one embodiment, the computer program when executed by the processor further performs the steps of:
and if the image quality of the image after the quality compression processing is not zero, taking the image after the quality compression processing as a second cycle compression image corresponding to the next cycle process to perform the quality compression processing until the initial compression image is obtained.
It should be noted that, the user information (including, but not limited to, user equipment information, user personal information, etc.) and the data (including, but not limited to, data for analysis, stored data, presented data, etc.) referred to in the present application are information and data authorized by the user or sufficiently authorized by each party, and the collection, use and processing of the related data are required to comply with the related laws and regulations and standards of the related countries and regions.
Those skilled in the art will appreciate that implementing all or part of the above described methods may be accomplished by way of a computer program stored on a non-transitory computer readable storage medium, which when executed, may comprise the steps of the embodiments of the methods described above. Any reference to memory, database, or other medium used in the various embodiments provided herein may include at least one of non-volatile and volatile memory. The nonvolatile memory may include Read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high density embedded nonvolatile memory, resistive random access memory (ReRAM), magnetic random access memory (MagnetoresistiveRandomAccessMemory, MRAM), ferroelectric memory (FerroelectricRandomAccessMemory, FRAM), phase change memory (PhaseChangeMemory, PCM), graphene memory, and the like. Volatile memory may include random access memory (RandomAccessMemory, RAM) or external cache memory, and the like. By way of illustration, and not limitation, RAM can take many forms, such as static random access memory (StaticRandomAccessMemory, SRAM) or dynamic random access memory (DynamicRandomAccessMemory, DRAM), among others. The databases referred to in the various embodiments provided herein may include at least one of relational databases and non-relational databases. The non-relational database may include, but is not limited to, a blockchain-based distributed database, and the like. The processors referred to in the embodiments provided herein may be general purpose processors, central processing units, graphics processors, digital signal processors, programmable logic units, quantum computing-based data processing logic units, etc., without being limited thereto.
The technical features of the above embodiments may be arbitrarily combined, and all possible combinations of the technical features in the above embodiments are not described for brevity of description, however, as long as there is no contradiction between the combinations of the technical features, they should be considered as the scope of the description.
The above examples only represent a few embodiments of the present application, which are described in more detail and are not to be construed as limiting the scope of the present application. It should be noted that it would be apparent to those skilled in the art that various modifications and improvements could be made without departing from the spirit of the present application, which would be within the scope of the present application. Accordingly, the scope of protection of the present application shall be subject to the appended claims.

Claims (14)

1. An image compression method, the method comprising:
acquiring an initial compressed image, wherein the initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, and the image data size of the initial compressed image is larger than a preset data size threshold;
detecting whether the image size of the initial compressed image meets Canvas compression conditions or not;
And if the image size of the initial compressed image meets the Canvas compression condition, carrying out Canvas compression processing on the initial compressed image to obtain a target compressed image, wherein the image data size of the target compressed image is smaller than or equal to the preset data size threshold.
2. The method of claim 1, wherein the performing Canvas compression on the initial compressed image to obtain a target compressed image comprises:
performing cyclic Canvas compression processing on the basis of the initial compressed image according to a preset image size compression ratio until the target compressed image is obtained;
wherein the image size compression ratio includes at least an image width compression ratio.
3. The method according to claim 2, wherein the performing the cyclic Canvas compression process based on the initial compressed image according to the preset image size compression ratio until the target compressed image is obtained includes:
in a first cyclic process, performing width compression processing on a first cyclic compressed image according to the image width compression ratio, and performing length compression processing on the first cyclic compressed image according to the image width compression ratio to obtain compressed data, wherein in the first cyclic process, the first cyclic compressed image is the initial compressed image;
Carrying out Canvas drawing processing on the compressed data to obtain a Canvas compressed image corresponding to the current circulation process;
and if the image data quantity of the image after the Canvas compression processing is smaller than or equal to the preset data quantity threshold value, taking the image after the Canvas compression processing as the target compressed image.
4. A method according to claim 3, characterized in that the method further comprises:
and if the image data quantity of the image after Canvas compression is larger than the preset data quantity threshold, carrying out Canvas compression on the image after Canvas compression as a first cycle compression image corresponding to the next cycle process until the target compression image is obtained.
5. The method of claim 1, wherein the detecting whether the image size of the initial compressed image meets Canvas compression conditions comprises:
detecting whether the image width value of the initial compressed image is larger than a preset image width value threshold value or not;
and if the image width value of the initial compressed image is smaller than or equal to the image width value threshold, determining that the image size of the initial compressed image meets the Canvas compression condition.
6. The method of claim 5, wherein the method further comprises:
if the image width value of the initial compressed image is larger than the image width value threshold, compressing the image width of the initial compressed image to the image width indicated by the image width value threshold, and compressing the image length of the initial compressed image according to the compression ratio of the image width of the initial compressed image to obtain an intermediate compressed image;
and if the image data amount of the intermediate compressed image is larger than the preset data amount threshold, carrying out Canvas compression processing on the intermediate compressed image to obtain the target compressed image.
7. The method of claim 6, wherein the method further comprises:
and if the image data amount of the intermediate compressed image is smaller than or equal to the preset data amount threshold, taking the intermediate compressed image as the target compressed image.
8. The method of claim 1, wherein the acquiring the initial compressed image comprises:
acquiring the original image based on an H5 page, wherein the original image has preset image quality;
and calling the preset API interface to perform cyclic quality compression processing based on the original image according to a preset image quality compression value until the initial compressed image is obtained.
9. The method of claim 8, wherein the invoking the preset API based on the original image for cyclic quality compression according to a preset image quality compression value until the initial compressed image is obtained comprises:
performing quality compression processing on a second cyclic compressed image according to the image quality compression value in a first cyclic process to obtain a quality compressed image, wherein in the first cyclic process, the second cyclic compressed image is the original image;
and if the image quality of the image after the quality compression processing is zero and the image data amount of the image after the quality compression processing is larger than the preset data amount threshold value, taking the image after the quality compression processing as the initial compression image.
10. The method according to claim 9, wherein the method further comprises:
and if the image quality of the image after the quality compression processing is not zero and the image data amount of the image after the quality compression processing is larger than the preset data amount threshold value, taking the image after the quality compression processing as a second cycle compression image corresponding to the next cycle process to perform the quality compression processing until the initial compression image is obtained.
11. An image compression apparatus, the apparatus comprising:
the acquisition module is used for acquiring an initial compressed image, wherein the initial compressed image is obtained by calling a preset API interface to perform quality compression on an original image, and the image data size of the initial compressed image is larger than a preset data size threshold;
the detection module is used for detecting whether the image size of the initial compressed image accords with Canvas compression conditions or not;
and the compression module is used for carrying out Canvas compression processing on the initial compressed image to obtain a target compressed image if the image size of the initial compressed image meets the Canvas compression condition, wherein the image data amount of the target compressed image is smaller than or equal to the preset data amount threshold value.
12. A computer device comprising a memory and a processor, the memory storing a computer program, characterized in that the processor implements the steps of the method of any one of claims 1 to 10 when the computer program is executed.
13. A computer readable storage medium, on which a computer program is stored, characterized in that the computer program, when being executed by a processor, implements the steps of the method of any of claims 1 to 10.
14. A computer program product comprising a computer program, characterized in that the computer program, when being executed by a processor, implements the steps of the method of any one of claims 1 to 10.
CN202311139473.5A 2023-09-05 2023-09-05 Image compression method, apparatus, device, storage medium, and program product Pending CN117372551A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311139473.5A CN117372551A (en) 2023-09-05 2023-09-05 Image compression method, apparatus, device, storage medium, and program product

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311139473.5A CN117372551A (en) 2023-09-05 2023-09-05 Image compression method, apparatus, device, storage medium, and program product

Publications (1)

Publication Number Publication Date
CN117372551A true CN117372551A (en) 2024-01-09

Family

ID=89391816

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311139473.5A Pending CN117372551A (en) 2023-09-05 2023-09-05 Image compression method, apparatus, device, storage medium, and program product

Country Status (1)

Country Link
CN (1) CN117372551A (en)

Similar Documents

Publication Publication Date Title
CN104869305B (en) Method and apparatus for processing image data
CN111681291A (en) Image processing method, device, equipment and computer readable storage medium
US11514263B2 (en) Method and apparatus for processing image
CN105279006B (en) Screen capture method and terminal based on Android system
CN111163345A (en) Image rendering method and device
CN105827484A (en) Synchronous test image display method and system
CN111080571A (en) Camera shielding state detection method and device, terminal and storage medium
US11481927B2 (en) Method and apparatus for determining text color
CN114418825B (en) Image processing method, image processing device, computer equipment and storage medium
US10878272B2 (en) Information processing apparatus, information processing system, control method, and program
US20170118401A1 (en) Electronic device and method for processing image
US9282331B2 (en) Image processing method and electronic device
US11706463B2 (en) Video synthesis method, apparatus, computer device and readable storage medium
CN110650309B (en) Video conference video image transmission method, terminal and readable storage medium
CN117372551A (en) Image compression method, apparatus, device, storage medium, and program product
US20160277750A1 (en) Method of processing image and electronic device thereof
CN110807300A (en) Image processing method and device, electronic equipment and medium
CN112948046A (en) Screen capturing method, device, equipment and storage medium
CN111243046A (en) Image quality detection method, device, electronic equipment and storage medium
US20180316936A1 (en) System and method for data compression
CN117437178A (en) Image definition measuring method and device
CN115525704A (en) Data processing method and device, storage medium and electronic equipment
CN116016988A (en) Live image processing method, device, electronic equipment and storage medium
CN117119042A (en) Task processing method and device for micro-service, computer equipment and storage medium
CN116755934A (en) Snapshot starting method, device, equipment, storage medium and program product

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination