Small image processing factory for Android

Source: Internet
Author: User

Sometimes, in projects, we need to process images, such as adding a rounded corner or adding a mosaic or watermark. Although it is not difficult, it is still quite tangled, for example, I have summarized the following image processing factory tool categories based on some of the methods on the Internet, and may have more requirements. If necessary, I will go online to find them, continue to improve. I will directly share the Code with you. There are many comments and simple comments, so I will not talk much about it. I will share the Code with you directly:

/*** Image Processing Factory *** @ author way **/public class imageutil {/*** obtain the input stream through path ** @ Param path * @ return input stream * @ throws exception * exception */public static inputstream getrequest (string path) throws exception {URL url = new URL (PATH); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestmethod ("get"); Conn. setconnecttimeout (5000); If (Conn. getresponsecode () = 200) {return Conn. get Inputstream ();} return NULL ;} /*** read binary data from the stream ** @ Param instream * input stream * @ return binary data * @ throws exception */public static byte [] readinputstream (inputstream instream) throws exception {bytearrayoutputstream outsteam = new bytearrayoutputstream (); byte [] buffer = new byte [4096]; int Len = 0; while (LEN = instream. read (buffer ))! =-1) {outsteam. write (buffer, 0, Len);} outsteam. close (); instream. close (); Return outsteam. tobytearray ();}/*** converts a path to a drawable object ** @ Param URL * path * @ return drawable object */public static drawable loadimagefromurl (string URL) {URL m; inputstream I = NULL; try {M = new URL (URL); I = (inputstream) M. getcontent ();} catch (malformedurlexception E1) {e1.printstacktrace ();} catch (ioexception e) {e. printstack Trace ();} drawable d = drawable. createfromstream (I, "src"); Return D ;} /*** convert a path to a drawable object ** @ Param URL * string path * @ return drawable object * @ throws exception */public static drawable getdrawablefromurl (string URL) throws exception {return drawable. createfromstream (getrequest (URL), null );} /*** obtain the bitmap from the path ** @ Param URL * string path * @ return bitmap * @ throws exception */public static bitmap getbitma Pfromurl (string URL) throws exception {byte [] bytes = getbytesfromurl (URL); Return bytetobitmap (bytes );} /*** obtain the rounded corner bitmap from the path ** @ Param URL * string path * @ Param pixels * rounded corner radians * @ return rounded corner bitmap * @ throws exception */public static bitmap getroundbitmapfromurl (string URL, int pixels) throws exception {byte [] bytes = getbytesfromurl (URL); Bitmap bitmap = bytetobitmap (bytes); Return toroundcorner (bitmap, pixels) ;} /*** Obtain the rounded corner drawable object from the path ** @ Param URL * string path * @ Param pixels * rounded corner radians * @ return rounded corner drawable object * @ throws exception */ public static drawable gerounddrawablefromurl (string URL, int pixels) throws exception {byte [] bytes = URLs; bitmapdrawable = (bitmapdrawable) bytetodrawable (bytes); Return toroundcorner (bitmapdrawable, pixels );} /*** obtain binary data from the path ** @ Param URL * String path * @ return binary data * @ throws exception */public static byte [] getbytesfromurl (string URL) throws exception {return readinputstream (getrequest (URL ));} /*** obtain the bitmap from binary data ** @ Param bytearray * binary data * @ return bitmap */public static bitmap bytetobitmap (byte [] bytearray) {If (bytearray. length! = 0) {return bitmapfactory. decodebytearray (bytearray, 0, bytearray. length) ;}else {return NULL ;}} /*** obtain the drawable object from binary data ** @ Param bytearray * binary data * @ return drawable object */public static drawable bytetodrawable (byte [] bytearray) {bytearrayinputstream ins = new bytearrayinputstream (bytearray); Return drawable. createfromstream (INS, null);}/*** convert bitmap to binary data ** @ Param BM * bitmap * @ return binary data */Public Static byte [] bitmap2bytes (Bitmap BM) {bytearrayoutputstream baos = new bytearrayoutputstream (); BM. compress (bitmap. compressformat. PNG, 100, baos); Return baos. tobytearray ();}/*** converts a drawable object to a bitmap ** @ Param drawable * drawable object * @ return bitmap */public static bitmap drawabletobitmap (drawable) {Bitmap bitmap = bitmap. createbitmap (drawable. getintrinsicwidth (), drawable. getintrinsicheight (), Dr Awable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888: bitmap. config. rgb_565); canvas = new canvas (Bitmap); drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight (); drawable. draw (canvas); Return bitmap;}/*** de-color the image, return the grayscale image ** @ Param BMP original * the image that is passed in * @ return after color removal */public static bitmap tograyscale (bitmap BMP original) {int width, height; Height = BMP original. getheight (); width = BMP original. ge Twidth (); bitmap BMP grayscale = bitmap. createbitmap (width, height, bitmap. config. rgb_565); canvas c = new canvas (BMP grayscale); paint = new paint (); colormatrix CM = new colormatrix (); cm. setsaturation (0); colormatrixcolorfilter F = new colormatrixcolorfilter (CM); paint. setcolorfilter (f); C. drawbitmap (BMP original, 0, 0, paint); Return BMP grayscale;}/*** remove color and add rounded corner ** @ Param BMP original * Source image * @ Param pi Xels * rounded corner radians * @ return Modified Image */public static bitmap tograyscale (bitmap BMP original, int pixels) {return toroundcorner (tograyscale (BMP original), pixels );} /*** convert the bitmap into a rounded corner bitmap ** @ Param bitmap * the bitmap to be modified * @ Param pixels * The radian of the rounded corner * @ return rounded corner bitmap */public static bitmap toroundcorner (Bitmap bitmap, int pixels) {bitmap output = bitmap. createbitmap (bitmap. getwidth (), bitmap. getheight (), config. argb_8888); canvas Canvas = new canvas (output); Final int color = 0xff0000242; Final paint = new paint (); Final rect = new rect (0, 0, bitmap. getwidth (), bitmap. getheight (); Final rectf = new rectf (rect); Final float roundpx = pixels; paint. setantialias (true); canvas. drawargb (0, 0, 0, 0); paint. setcolor (color); canvas. drawroundrect (rectf, roundpx, roundpx, paint); paint. setxfermode (New porterduduxfermode (mode. Src_in); canvas. drawbitmap (bitmap, rect, rect, paint); Return output ;} /*** convert bitampdrawable to bitampdrawable of the rounded corner ** @ Param bitmapdrawable * native bitampdrawable object * @ Param pixels * rounded corner radians * @ return rounded corner bitampdrawable object */public static bitmapdrawable trim (bitmapdrawable, int pixels) {Bitmap bitmap = bitmapdrawable. getbitmap (); bitmapdrawable = new bitmapdrawable (toroundcorner (bitmap, P Ixels); Return bitmapdrawable ;} /*** image watermark generation method ** @ Param SRC * Source image bitmap * @ Param watermark * watermark image bitmap * @ return returns a watermark image */public static bitmap createbitmap (Bitmap SRC, bitmap watermark) {If (src = NULL) return NULL; int W = SRC. getwidth (); int H = SRC. getheight (); int WW = watermark. getwidth (); int wh = watermark. getheight (); bitmap newb = bitmap. createbitmap (W, H, config. argb_8888); // create a new one with the same length and width as the SRC one. Canvas CV = new canvas (NEWB); // initialize the canvas cv. drawbitmap (SRC, 0, 0, null); // draw srccv at coordinates 0, 0. drawbitmap (watermark, W-ww + 5, H-Wh + 5, null); // Add the watermark CV to the bottom right corner of SRC. save (canvas. all_save_flag); // save to save the canvas status. After saving, you can call operations such as pan, zoom, rotate, miscut, and crop of the canvas. Cv. Restore (); // storage, used to restore the State saved before the canvas. This prevents operations performed on the canvas after the save operation from affecting subsequent painting. Return newb ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.