Disclosure of Invention
The invention provides a method for improving image compression and transmission based on a Huffman algorithm aiming at the problems. The invention utilizes the Huffman algorithm of the library to carry out customized compression processing on the picture according to the requirement, thereby achieving the purpose that the picture is compatible with the full version and the polarized transmission.
In order to achieve the purpose, the specific technical scheme of the invention is as follows:
a method for improving image compression transmission based on a Huffman algorithm comprises the following steps:
s1, reading picture data, scanning the picture data pixel by pixel, and separating a R, G, B channel of each pixel;
s2, reassembling the separated RGB channels into new pixel RGB data;
and S3, carrying out Hash method compression on the new pixel RGB data, and storing the new pixel RGB data into a new file to form compressed picture data.
Preferably, the method for separating the R, G, B channel of each pixel in step S1 is as follows:
shifting the R channel in the pixel to the right by 16 bits and then &0xFF, and storing a new R channel;
shifting the G channel in the pixel to the right by 8 bits and then comparing the G channel with 0xFF, and storing a new G channel;
and finally, performing B channel &0xFF on the pixel.
Preferably, step S3 is specifically:
creating jpeg compression object, storing the file in a directory, and setting compression parameters; and the channel mode of the picture is RGB, starting a Huffman function, starting compression, circularly writing each line of data, and completing compression to release the jpeg object.
Further, the compression parameter is picture width and height.
The picture bitmap is composed of a plurality of pixels, one pixel is composed of four ARGB channels, on the premise that the picture quality is guaranteed, the transparency channel A can be removed, one pixel occupies one quarter of the memory, the volume of one picture is optimized by 25%, and the transmission speed of the picture is favorably improved.
Detailed Description
In order that those skilled in the art will be able to understand and practice the present invention, further description will be given below of embodiments of the present invention.
The invention provides a method for improving image compression transmission based on a Huffman algorithm, which comprises the following steps:
s1, reading picture data, scanning the picture data pixel by pixel, and separating a R, G, B channel of each pixel;
in step S1, the method for separating the R, G, B channel of each pixel includes:
shifting the R channel in the pixel to the right by 16 bits and then &0xFF, and storing a new R channel;
shifting the G channel in the pixel to the right by 8 bits and then comparing the G channel with 0xFF, and storing a new G channel;
and finally, performing B channel &0xFF on the pixel.
S2, reassembling the separated RGB channels into new pixel RGB data;
and S3, carrying out Hash method compression on the new pixel RGB data, and storing the new pixel RGB data into a new file to form compressed picture data.
Wherein, step S3 specifically includes:
creating jpeg compression object, storing files in a directory, and setting compression parameters, such as picture width and height; and the channel mode of the picture is RGB, starting a Huffman function, starting compression, circularly writing each line of data, and completing compression to release the jpeg object.
The principle of the invention is as follows:
1) the picture bitmap is composed of a plurality of pixels, one pixel is composed of argb four channels, on the premise that the picture quality is guaranteed, a transparency channel can be removed, the memory occupied by one pixel is reduced by one quarter, and the volume of one picture is optimized by 25%.
Analyzing bitmap to obtain argb data, obtaining internal information to obtain pixel information in a picture, obtaining each pixel of the picture through double circulation of the width and the height of the picture, shifting an r channel in the pixel to the right by 16 bits and then storing a new r channel by &0xFF, shifting a g channel in the pixel to the right by 8 bits and then storing a new g channel by &0xFF, then shifting a b channel in the pixel by &0xFF, and finally reassembling the rgb data (corresponding to the step S1/2 of the invention).
2) And processing the obtained information of the new picture according to the requirements, such as the width, height, quality and path of the final expected picture, starting Huffman algorithm compression, and storing the compressed information into a new file.
Creating a jpeg compression object (object), storing the file in a directory, setting compression parameters such as the width and the height of a picture, setting the channel mode of the picture to rgb, starting a huffman function, starting compression, circularly writing each line of data, and releasing the jpeg object after the compression is completed (corresponding to step S3 of the present invention).
The above-mentioned embodiments only express several embodiments of the present invention, and the description thereof is more specific and detailed, but not construed as limiting the scope of the present invention. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the inventive concept, which falls within the scope of the present invention. Therefore, the protection scope of the present patent shall be subject to the appended claims.