android 处理图片工具
packagecom.wireme.activity; importjava.io.ByteArrayOutputStream; importjava.io.File; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importandroid.graphics.Bitmap; importandroid.graphics.BitmapFactory; importandroid.graphics.Canvas; importandroid.graphics.ColorMatrix; importandroid.graphics.ColorMatrixColorFilter; importandroid.graphics.Paint; importandroid.graphics.PorterDuffXfermode; importandroid.graphics.Rect; importandroid.graphics.RectF; importandroid.graphics.Bitmap.Config; importandroid.graphics.PorterDuff.Mode; importandroid.graphics.drawable.BitmapDrawable; importandroid.graphics.drawable.Drawable; /* * *处理图片的工具类. */ public classimageTool{ public staticfinal intLEFT= 0; public staticfinal intRIGHT= 1; public staticfinal intTOP= 3; public staticfinal intBOTTOM= 4; /* * */ /* * *图片去色,返回灰度图片 * *@parambmpOriginal *传入的图片 *@return去色后的图片 */ public staticBitmaptoGrayscale(BitmapbmpOriginal){ intwidth,height; height=bmpOriginal.getHeight(); width=bmpOriginal.getWidth(); BitmapbmpGrayscale=Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565); Canvasc= newCanvas(bmpGrayscale); Paintpaint= newPaint(); ColorMatrixcm= newColorMatrix(); cm.setSaturation( 0); ColorMatrixColorFilterf= newColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0,paint); returnbmpGrayscale; } /* * */ /* * *去色同时加圆角 * *@parambmpOriginal *原图 *@parampixels *圆角弧度 *@return修改后的图片 */ public staticBitmaptoGrayscale(BitmapbmpOriginal, intpixels){ returntoRoundCorner(toGrayscale(bmpOriginal),pixels); } /* * */ /* * *把图片变成圆角 * *@parambitmap *需要修改的图片 *@parampixels *圆角的弧度 *@return圆角图片 */ public staticBitmaptoRoundCorner(Bitmapbitmap, intpixels){ Bitmapoutput=Bitmap.createBitmap(bitmap.getWidth(),bitmap .getHeight(),Config.ARGB_8888); Canvascanvas= newCanvas(output); final intcolor= 0xff424242; finalPaintpaint= newPaint(); finalRectrect= newRect( 0, 0,bitmap.getWidth(),bitmap.getHeight()); finalRectFrectF= newRectF(rect); final floatroundPx=pixels; paint.setAntiAlias( true); canvas.drawARGB( 0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF,roundPx,roundPx,paint); paint.setXfermode( newPorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap,rect,rect,paint); returnoutput; } /* * */ /* * *使圆角功能支持BitampDrawable * *@parambitmapDrawable *@parampixels *@return */ public staticBitmapDrawabletoRoundCorner(BitmapDrawablebitmapDrawable, intpixels){ Bitmapbitmap=bitmapDrawable.getBitmap(); bitmapDrawable= newBitmapDrawable(toRoundCorner(bitmap,pixels)); returnbitmapDrawable; } /* * *读取路径中的图片,然后将其转化为缩放后的bitmap * *@parampath */ public static voidsaveBefore(Stringpath){ BitmapFactory.Optionsoptions= newBitmapFactory.Options(); options.inJustDecodeBounds= true; // 获取这个图片的宽和高 Bitmapbitmap=BitmapFactory.decodeFile(path,options); // 此时返回bm为空 options.inJustDecodeBounds= false; // 计算缩放比 intbe=( int)(options.outHeight/( float) 200); if(be<= 0) be= 1; options.inSampleSize= 2; // 图片长宽各缩小二分之一 // 重新读入图片,注意这次要把options.inJustDecodeBounds设为false哦 bitmap=BitmapFactory.decodeFile(path,options); intw=bitmap.getWidth(); inth=bitmap.getHeight(); System. out.println(w+ " "+h); // savePNG_After(bitmap,path); saveJPGE_After(bitmap,path); } /* * *保存图片为PNG * *@parambitmap *@paramname */ public static voidsavePNG_After(Bitmapbitmap,Stringname){ Filefile= newFile(name); try{ FileOutputStream out= newFileOutputStream(file); if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){ out.flush(); out.close(); } } catch(FileNotFoundExceptione){ e.printStackTrace(); } catch(IOExceptione){ e.printStackTrace(); } } /* * *保存图片为JPEG * *@parambitmap *@parampath */ public static voidsaveJPGE_After(Bitmapbitmap,Stringpath){ Filefile= newFile(path); try{ FileOutputStream out= newFileOutputStream(file); if(bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)){ out.flush(); out.close(); } } catch(FileNotFoundExceptione){ e.printStackTrace(); } catch(IOExceptione){ e.printStackTrace(); } } /* * *水印 * *@parambitmap *@return */ public staticBitmapcreateBitmapForWatermark(Bitmapsrc,Bitmapwatermark){ if(src== null){ return null; } intw=src.getWidth(); inth=src.getHeight(); intww=watermark.getWidth(); intwh=watermark.getHeight(); // createthenewblankbitmap Bitmapnewb=Bitmap.createBitmap(w,h,Config.ARGB_8888); // 创建一个新的和SRC长度宽度一样的位图 Canvascv= newCanvas(newb); // drawsrcinto cv.drawBitmap(src, 0, 0, null); // 在0,0坐标开始画入src // drawwatermarkinto cv.drawBitmap(watermark,w-ww+ 5,h-wh+ 5, null); // 在src的右下角画入水印 // saveallclip cv.save(Canvas.ALL_SAVE_FLAG); // 保存 // store cv.restore(); // 存储 returnnewb; } /* * *图片合成 * *@return */ public staticBitmappotoMix( intdirection,Bitmap...bitmaps){ if(bitmaps.length<= 0){ return null; } if(bitmaps.length== 1){ returnbitmaps[ 0]; } BitmapnewBitmap=bitmaps[ 0]; // newBitmap=createBitmapForFotoMix(bitmaps[0],bitmaps[1],direction); for( inti= 1;i<bitmaps.length;i++){ newBitmap=createBitmapForFotoMix(newBitmap,bitmaps[i],direction); } returnnewBitmap; } private staticBitmapcreateBitmapForFotoMix(Bitmapfirst,Bitmapsecond, intdirection){ if(first== null){ return null; } if(second== null){ returnfirst; } intfw=first.getWidth(); intfh=first.getHeight(); intsw=second.getWidth(); intsh=second.getHeight(); BitmapnewBitmap= null; if(direction==LEFT){ newBitmap=Bitmap.createBitmap(fw+sw,fh>sh?fh:sh, Config.ARGB_8888); Canvascanvas= newCanvas(newBitmap); canvas.drawBitmap(first,sw, 0, null); canvas.drawBitmap(second, 0, 0, null); } else if(direction==RIGHT){ newBitmap=Bitmap.createBitmap(fw+sw,fh>sh?fh:sh, Config.ARGB_8888); Canvascanvas= newCanvas(newBitmap); canvas.drawBitmap(first, 0, 0, null); canvas.drawBitmap(second,fw, 0, null); } else if(direction==TOP){ newBitmap=Bitmap.createBitmap(sw>fw?sw:fw,fh+sh, Config.ARGB_8888); Canvascanvas= newCanvas(newBitmap); canvas.drawBitmap(first, 0,sh, null); canvas.drawBitmap(second, 0, 0, null); } else if(direction==BOTTOM){ newBitmap=Bitmap.createBitmap(sw>fw?sw:fw,fh+sh, Config.ARGB_8888); Canvascanvas= newCanvas(newBitmap); canvas.drawBitmap(first, 0, 0, null); canvas.drawBitmap(second, 0,fh, null); } returnnewBitmap; } /* * *将Bitmap转换成指定大小 * *@parambitmap *@paramwidth *@paramheight *@return */ public staticBitmapcreateBitmapBySize(Bitmapbitmap, intwidth, intheight){ returnBitmap.createScaledBitmap(bitmap,width,height, true); } /* * *Drawable转Bitmap * *@paramdrawable *@return */ public staticBitmapdrawableToBitmapByBD(Drawabledrawable){ BitmapDrawablebitmapDrawable=(BitmapDrawable)drawable; returnbitmapDrawable.getBitmap(); } /* * *Bitmap转Drawable * *@parambitmap *@return */ public staticDrawablebitmapToDrawableByBD(Bitmapbitmap){ Drawabledrawable= newBitmapDrawable(bitmap); returndrawable; } /* * *byte[]转bitmap * *@paramb *@return */ public staticBitmapbytesToBimap( byte[]b){ if(b.length!= 0){ returnBitmapFactory.decodeByteArray(b, 0,b.length); } else{ return null; } } /* * *bitmap转byte[] * *@parambm *@return */ public static byte[]bitmapToBytes(Bitmapbm){ ByteArrayOutputStreambaos= newByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100,baos); returnbaos.toByteArray(); } } 本文转自 terry_龙 51CTO博客,原文链接:http://blog.51cto.com/terryblog/791516 ,如需转载请自行联系原作者