BitmapEditor elegant and efficient compression of Android picture frame

About 

BitmapEditor is a simple and efficient image compression processing tools, it can be done: 

    to receive from File, Bitmap, resID, ByteBuffer, byte image data [] is then converted into a
     byte [], Bitmap, File 
    the picture resolution zoom 
    the picture compression memory size 

    to change the picture color format 

    Why use it? It has a bit: 
    responsive function design (design reference Glide), to facilitate the call 
    synchronization process, the asynchronous 
    conveniently introduced using 

for convenience into the use of all the codes are put BitmapEditor.java files, and all are introduced Android native functions, all you just need to copy the code to use. 

Tips: Here the picture is loaded asynchronous processing is used to open the Thread run, 
while providing Rxjava as asynchronous processing load mode, where the specific details, refer to Demo 

BitmapEditor Demo and use the following address: HTTPS: // github.com/DiskyZhs / BitmapEdtior 

currently, the tool has just completed the preparation, there may be some consider inappropriate and unreasonable, welcome that. 
Use 
initialization BitmapEditor, 

call 

BitmapEditor.init ()

Using the function to load from data from different sources 

BitmapEditor.init (). From (String filePtah) 

BitmapEditor.init (). From (File File) 

BitmapEditor.init (). From ( byte [] bmpData) 

BitmapEditor.init (). from (the ByteBuffer bmpBuffer) 

BitmapEditor.init (). from (Bitmap BMP) 

BitmapEditor.init (). from ( int resId, the context context)
 

Add image processing 

after completing the operation to select the data source is added to the picture 
paserResolution function 

using function paserResolution to compress the image resolution, parameter units PX 

BitmapEditor.init (). from (BMP) .paserResolution ( 1920, 1080 ) 

here on the image to zoom back directly in accordance with the desired resolution, so the final picture aspect ratio may changes 
setDesireRatio function 

using setDesireRatio function to set the desired aspect ratio, picture resolution when you zoom when 

   `` ` 
BitmapEditor.init (). from (bmp) .paserResolution (1920, 1080) .setDesireRatio (1.60 ) 


    BitmapEditor.init (). From (BMP) .setDesireRatio ( 0.75 ) 

passed in parameter is the aspect ratio, the picture is scaled width / height 
setResolutionLarger function 

when you set the Ratio is aspect ratio when using setResolutionLarger function to set a scaled picture resolution is greater than the resolution given 

BitmapEditor.init () 
.from (BMP) 
.paserResolution ( 1920, 1080 ) 
.setDesireRatio ( 1.60 ) 
.setResolutionLarger ( to true ) 

here means that when you choose the pictures you expect zoom inconsistent picture resolution, if 
setResolutionLarger ( to true ), that is to say after the next picture you zoom resolution is greater than you expect resolution. 
vice versa. 
paserResolutionKeepRatio function

If you want to follow a certain aspect ratio to scale the image, you can use paserResolutionKeepRatio ( int width, int height, float ratio, boolean isLargerResolution) function. 

That is, if I want to follow the current picture width / height 1.60, after scaling resolutions greater than 1920, 1080 , I can be as follows 

BitmapEditor.init () 
.from (BMP) 
.paserResolution ( 1920, 1080 ) 
.setDesireRatio ( 1.60 ) 
.setResolutionLarger ( to true )
 

the above function is equivalent to 

BitmapEditor.init () 
.from (BMP) 
.paserResolutionKeepRatio ( 1920,1080,1.60, to true )
 

LimitSize function 

if you expect compressed to less than the size of the Bitmap 1M, you can use the function LimitSize follows 

BitmapEditor.init () 
.from (BMP) 
.limitSize ( 1024 ) 

parameter units kb 
setColorMode function 

here is the set color format image compression, and is currently optionally ARGB_8888 RGB_565, as 
BitmapEditor.init () 
.from (BMP) 
.setColorMode (BitmapEditor.COLOR_MODE_RGB565) 

Note, change the color format here will lead to the final size of the generated images, usually in order to reduce the size of the picture, the picture is set to RGB565 color format 
to select the output image format and asynchronous synchronization 

you can use asBmp (), asFile () and asByteArray () function to choose different output formats 
the default is to use asynchronous loading, so you need to get results in the callback process pictures 

you can use listener callback function to add, as follows 

    BitmapEditor.init () 
    .from (BMP) 
    .paserResolution ( 1920, 1080 ) 
    .setDesireRatio ( 1.60 ) 
    .setResolutionLarger ( to true ) 
    .listener (listener) 
    .asBmp ();


Or
 
    BitmapEditor.init () 
    .from (BMP)
    .paserResolution ( 1920, 1080 ) 
    .setDesireRatio ( 1.60 ) 
    .setResolutionLarger ( to true ) 
    .asBmp (listener); 

if you need to synchronize loaded directly get the results, you can use 
asBmpSync (), asFileSync () and asByteArraySync () function 
you directly after obtaining the results of change, as 

    Bitmap result = 
    BitmapEditor.init () 
    .from (BMP) 
    .paserResolution ( 1920,1080 ) 
    .setDesireRatio ( 1.60 ) 
    .setResolutionLarger ( to true ) 
    .asBmpSync (); 


of course, is a function of thread blocking. 
Note that 

all of the processing of the picture is a hot start, only when the generated function call As the picture when it will take effect.

Secondly, in order to facilitate asynchronous processing load, provide a framework for the use of Rxjava loaded asynchronously, if you use in your project, it needs to import your own framework 

BitmapEditor Demo and use the following address: HTTPS: // github.com/DiskyZhs/BitmapEdtior 

Finally, there are many problems, welcome, I will be timely modifications! 
---------------- 
Disclaimer: This article is CSDN blogger "W -OMW 'original article, follow the BY-4.0 CC SA copyright agreement, reproduced, please attach the original source and link this statement. 
Original link: HTTPS: // blog.csdn.net/zhs4430169/article/details/71427514

 

Guess you like

Origin www.cnblogs.com/Free-Thinker/p/12486962.html