多张图片四个方位的图片合成,改变bitmap大小,图片去色等功能
- packagecom.dzh.operateimage;
- importandroid.graphics.Bitmap;
- importandroid.graphics.Bitmap.Config;
- importandroid.graphics.BitmapFactory;
- importandroid.graphics.Canvas;
- importandroid.graphics.ColorMatrix;
- importandroid.graphics.ColorMatrixColorFilter;
- importandroid.graphics.Paint;
- importandroid.graphics.PorterDuff.Mode;
- importandroid.graphics.PorterDuffXfermode;
- importandroid.graphics.Rect;
- importandroid.graphics.RectF;
- importandroid.graphics.drawable.BitmapDrawable;
- importandroid.graphics.drawable.Drawable;
- importjava.io.ByteArrayOutputStream;
- importjava.io.File;
- importjava.io.FileNotFoundException;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- /**
- *处理图片的工具类.
- */
- publicclassImageTools{
- publicstaticfinalintLEFT=0;
- publicstaticfinalintRIGHT=1;
- publicstaticfinalintTOP=3;
- publicstaticfinalintBOTTOM=4;
- /***/
- /**
- *图片去色,返回灰度图片
- *
- *@parambmpOriginal传入的图片
- *@return去色后的图片
- */
- publicstaticBitmaptoGrayscale(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修改后的图片
- */
- publicstaticBitmaptoGrayscale(BitmapbmpOriginal,intpixels){
- returntoRoundCorner(toGrayscale(bmpOriginal),pixels);
- }
- /***/
- /**
- *把图片变成圆角
- *
- *@parambitmap需要修改的图片
- *@parampixels圆角的弧度
- *@return圆角图片
- */
- publicstaticBitmaptoRoundCorner(Bitmapbitmap,intpixels){
- Bitmapoutput=Bitmap
- .createBitmap(bitmap.getWidth(),bitmap.getHeight(),Config.ARGB_8888);
- Canvascanvas=newCanvas(output);
- finalintcolor=0xff424242;
- finalPaintpaint=newPaint();
- finalRectrect=newRect(0,0,bitmap.getWidth(),bitmap.getHeight());
- finalRectFrectF=newRectF(rect);
- finalfloatroundPx=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
- */
- publicstaticBitmapDrawabletoRoundCorner(BitmapDrawablebitmapDrawable,intpixels){
- Bitmapbitmap=bitmapDrawable.getBitmap();
- bitmapDrawable=newBitmapDrawable(toRoundCorner(bitmap,pixels));
- returnbitmapDrawable;
- }
- /**
- *读取路径中的图片,然后将其转化为缩放后的bitmap
- *
- *@parampath
- */
- publicstaticvoidsaveBefore(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
- */
- publicstaticvoidsavePNG_After(Bitmapbitmap,Stringname){
- Filefile=newFile(name);
- try{
- FileOutputStreamout=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
- */
- publicstaticvoidsaveJPGE_After(Bitmapbitmap,Stringpath){
- Filefile=newFile(path);
- try{
- FileOutputStreamout=newFileOutputStream(file);
- if(bitmap.compress(Bitmap.CompressFormat.JPEG,100,out)){
- out.flush();
- out.close();
- }
- }catch(FileNotFoundExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- /**
- *水印
- *
- *@parambitmap
- *@return
- */
- publicstaticBitmapcreateBitmapForWatermark(Bitmapsrc,Bitmapwatermark){
- if(src==null){
- returnnull;
- }
- 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
- */
- publicstaticBitmappotoMix(intdirection,Bitmap...bitmaps){
- if(bitmaps.length<=0){
- returnnull;
- }
- 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;
- }
- privatestaticBitmapcreateBitmapForFotoMix(Bitmapfirst,Bitmapsecond,intdirection){
- if(first==null){
- returnnull;
- }
- 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);
- }elseif(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);
- }elseif(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);
- }elseif(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
- */
- publicstaticBitmapcreateBitmapBySize(Bitmapbitmap,intwidth,intheight)
- {
- returnBitmap.createScaledBitmap(bitmap,width,height,true);
- }
- /**
- *Drawable转Bitmap
- *
- *@paramdrawable
- *@return
- */
- publicstaticBitmapdrawableToBitmapByBD(Drawabledrawable){
- BitmapDrawablebitmapDrawable=(BitmapDrawable)drawable;
- returnbitmapDrawable.getBitmap();
- }
- /**
- *Bitmap转Drawable
- *
- *@parambitmap
- *@return
- */
- publicstaticDrawablebitmapToDrawableByBD(Bitmapbitmap){
- Drawabledrawable=newBitmapDrawable(bitmap);
- returndrawable;
- }
- /**
- *byte[]转bitmap
- *
- *@paramb
- *@return
- */
- publicstaticBitmapbytesToBimap(byte[]b){
- if(b.length!=0){
- returnBitmapFactory.decodeByteArray(b,0,b.length);
- }else{
- returnnull;
- }
- }
- /**
- *bitmap转byte[]
- *
- *@parambm
- *@return
- */
- publicstaticbyte[]bitmapToBytes(Bitmapbm){
- ByteArrayOutputStreambaos=newByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.PNG,100,baos);
- returnbaos.toByteArray();
- }
- }