Android in the actual development of a lot of time to deal with the image, the summary of the Bitmaputils class includes a few features:
1.Android Image Reflection,
2.Android image blur processing,
3.Android Picture fillet processing,
4. The picture rotates at a certain angle along the y axis,
5.Android add a border to the picture.
The next step is to go directly to the code, which has some explanation in the code . You can use it directly.
/* * @Title: Bitmaputils.java * @Copyright: Corporation. Ltd. Copyright 1998-2018, All rights reserved * @Description:todo< Please describe what this file is doing > * @author: XJP * @data: September 18, 2014 Afternoon 10:10:30 * @version: V1.0 */package com.mktech.bitmaputils;import Android.annotation.suppresslint;import Android.annotation.targetapi;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapshader;import Android.graphics.camera;import Android.graphics.canvas;import Android.graphics.lineargradient;import Android.graphics.matrix;import Android.graphics.paint;import Android.graphics.porterduffxfermode;import Android.graphics.bitmap.config;import Android.graphics.Rect;import Android.graphics.rectf;import Android.graphics.shader;import Android.graphics.shader.tilemode;import Android.os.build;import Android.renderscript.allocation;import Android.renderscript.element;import Android.renderscript.renderscript;import Android.renderscript.scriptintrinsicblur;import android.util.Log;/** * todo< Please describe what this class is for > * * @author XJP * @data: September 18, 2014 Morning 10:10:30 * @version: V1.0 */public class Bitmaputils {p Rivate static final String TAG = "Bitmaputils";/** * todo< Create reflection Image > * @throw * @return Bitmap * @param srcbitmap The Bitmap of the source picture * @param reflectionheight image Reflection height */public static Bitmap createreflectedbitmap (Bitmap srcbitmap,int reflectio nheight) {if (null = = Srcbitmap) {log.e (TAG, "The Srcbitmap is null"); return null;} The gap between the reflection bitmap and original bitmap.final int reflection_gap = 0;int Srcwidth = Srcbitmap.getwidt h (); int srcheight = Srcbitmap.getheight (); if (0 = = Srcwidth | | srcheight = = 0) {log.e (TAG, "The Srcbitmap is null"); return null;} The Matrixmatrix matrix = new Matrix (); Matrix.prescale (1,-1); try {//The reflection bitmap, width is same with Origina L ' s, height is//half of original ' s. Bitmap Reflectionbitmap = Bitmap.createbitmap (srcbitmap, 0, Srcheight-reflectionheight,srcwidth, ReflectionHeight, Matrix, False); if (null = = Reflectionbitmap) {LOG.E (TAG, "Create The Reflectionbitmap is failed"); return null;} Create the bitmap which contains original and reflection bitmap. Bitmap bitmapwithreflection = Bitmap.createbitmap (srcwidth, srcheight + reflectionheight,bitmap.config.argb_8888); (Null = = Bitmapwithreflection) {return null;} Prepare the canvas to draw stuff. Canvas canvas = new canvas (bitmapwithreflection);//Draw the original Bitmap.canvas.drawBitmap (srcbitmap, 0, 0, NULL);//D Raw The Reflection Bitmap.canvas.drawBitmap (reflectionbitmap, 0, srcheight + reflection_gap,null); Paint paint = new paint ();p Aint.setantialias (true); LinearGradient shader = new LinearGradient (0, Srcheight, 0,bitmapwithreflection.getheight () + Reflection_gap, 0X70FFFFFF, 0X00FFFFFF, Tilemode.mirror);p aint.setshader (shader);p Aint.setxfermode (New Porterduffxfermode ( Android.graphics.PorterDuff.Mode.DST_IN)); Canvas.save ();//Draw the linear shader.canvas.drawRect (0, Srcheight, Srcwidth,bitmapwithreflection.getheight () + reflectiOn_gap, paint); if (reflectionbitmap! = null &&!reflectionbitmap.isrecycled ()) {reflectionbitmap.recycle (); Reflectionbitmap = null;} Canvas.restore (); return bitmapwithreflection;} catch (Exception e) {e.printstacktrace ();} LOG.E (TAG, "Create The Reflectionbitmap is failed"); return null;} /** * * todo< picture fillet processing > * @throw * @return Bitmap * @param srcbitmap source Picture Bitmap * @param ret rounded degrees */public STA Tic Bitmap getroundimage (Bitmap srcbitmap, float ret) {if (null = = Srcbitmap) {log.e (TAG, "The Srcbitmap is Null "); return null; } int bitwidth = Srcbitmap.getwidth (); int bithight = Srcbitmap.getheight (); Bitmapshader Bitmapshader = new Bitmapshader (Srcbitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) ; Paint paint = new paint (); Paint.setantialias (TRUE); Paint.setshader (Bitmapshader); RECTF RECTF = new RECTF (0, 0, Bitwidth, BithigHT); Bitmap Outbitmap = Bitmap.createbitmap (Bitwidth, Bithight, config.argb_8888); Canvas canvas = new canvas (OUTBITMAP); Canvas.drawroundrect (RECTF, ret, ret, paint); Canvas.save (); Canvas.restore (); return outbitmap; }/** * * todo< picture is rotated along the Y axis at a certain angle > * @throw * @return Bitmap * @param srcbitmap source image Bitma P * @param reflectionheight the height of the image reflection * @param rotate Picture Rotation angle */public static Bitmap Skewimage (Bitmap srcbit Map, float rotate, int reflectionheight) {if (null = = Srcbitmap) {log.e (TAG, "The Srcbitmap is null"); return null;} Bitmap Reflectebitmap = Createreflectedbitmap (Srcbitmap, Reflectionheight), if (null = = Reflectebitmap) {log.e (TAG, " Failed to Createreflectedbitmap "); return null;} int wbitmap = Reflectebitmap.getwidth (), int hbitmap = Reflectebitmap.getheight (); Float ScaleWidth = ((float) ()/Wbitma P;float SCAleheight = ((float))/hbitmap; Matrix matrix = new Matrix (); Matrix.postscale (ScaleWidth, scaleheight); reflectebitmap = Bitmap.createbitmap ( Reflectebitmap, 0, 0, Wbitmap, Hbitmap, matrix,true); Camera Localcamera = new camera (); Localcamera.save (); Matrix Localmatrix = new Matrix (); Localcamera.rotatey (rotate); Localcamera.getmatrix (Localmatrix); Localcamera.restore (); Localmatrix.pretranslate (-reflectebitmap.getwidth () >> 1,-reflectebitmap.getheight () >> 1); Bitmap localBitmap2 = Bitmap.createbitmap (reflectebitmap, 0, 0,reflectebitmap.getwidth (), Reflectebitmap.getheight () , localmatrix,true); Bitmap localBitmap3 = Bitmap.createbitmap (Localbitmap2.getwidth (), Localbitmap2.getheight (), Bitmap.Config.ARGB_ 8888); Canvas Localcanvas = new canvas (LOCALBITMAP3); Paint Localpaint = new paint (); Localpaint.setantialias (true); Localpaint.setfilterbitmap (true); Localcanvas.drawbitmap (LOCALBITMAP2, 0.0F, 0.0F, localpaint); if (null! = Reflectebitmap &&!) Reflectebitmap.isrecycled ()) {RefleCtebitmap.recycle (); reflectebitmap = null;} if (null! = LOCALBITMAP2 &&!localbitmap2.isrecycled ()) {localbitmap2.recycle (); localBitmap2 = null;} Localcanvas.save (); Localcanvas.restore (); return localBitmap3;} /** * * todo< image blur processing > * @throw * @return Bitmap * @param Bitmap Source image * @param radius the Radius of the blur supported range 0 < radius <= * @param Context Context */@TargetApi (Build.version_code S.JELLY_BEAN_MR1) @SuppressLint ("Newapi") public static Bitmap Blurbitmap (Bitmap bitmap,float radius,context Context) { Let's create an empty bitmap with the same size of the bitmap we want to blur bitmap Outbitmap = B Itmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); Instantiate a new renderscript renderscript rs = renderscript.create (context); Create an intrinsic Blur Script using the Renderscript SCRIPTINTRINSICBLur Blurscript = scriptintrinsicblur.create (RS, Element.u8_4 (RS)); Create the Allocations (in/out) with the Renderscript and the in/out bitmaps Allocation allin = Allocation.crea Tefrombitmap (rs, bitmap); Allocation Allout = Allocation.createfrombitmap (RS, outbitmap); Set the radius of the blur if (Radius >) {radius = 25.0f; }else if (radius <= 0) {radius = 1.0f; } blurscript.setradius (RADIUS); Perform the Renderscript blurscript.setinput (Allin); Blurscript.foreach (Allout); Copy the final bitmap created by the-Allocation to the Outbitmap Allout.copyto (OUTBITMAP); Recycle the original bitmap bitmap.recycle (); bitmap = null; After finishing everything, we destroy the Renderscript. Rs.destroy (); return outbitmap; }/** * todo< add a border of the specified color to the picture > * @param srcbitmap original picture * @param borderWidth Border Width * @pa Color value for RAM color border * @return */public static Bitmap Addframebitmap (Bitmap srcbitmap,int borderwidth,int Colo R) {if (Srcbitmap = = null) {LOG.E (TAG, "The Srcbitmap or borderbitmap is null"); return null; } int newwidth = Srcbitmap.getwidth () + borderWidth; int newheight = srcbitmap.getheight () + borderWidth; Bitmap Outbitmap = Bitmap.createbitmap (Newwidth, Newheight, config.argb_8888); Canvas canvas = new canvas (OUTBITMAP); Rect rec = canvas.getclipbounds (); rec.bottom--; rec.right--; Paint paint = new paint (); Sets the border color paint.setcolor (color); Paint.setstyle (Paint.Style.STROKE); Set Border width paint.setstrokewidth (borderWidth); Canvas.drawrect (REC, paint); Canvas.drawbitmap (Srcbitmap, BORDERWIDTH/2, BORDERWIDTH/2, NULL); CAnvas.save (Canvas.all_save_flag); Canvas.restore (); if (Srcbitmap!=null &&!srcbitmap.isrecycled ()) {srcbitmap.recycle (); Srcbitmap = null; } return Outbitmap; } }The above code can be validated directlyuse.
Share .
End.
Image processing Class Bitmaputils can be used in Android