Android 图片操作

Bitmap.createBitmap函数有6个重载方法

位图剪切参考重载方法4和6,重载方法6比较简单

1.public static Bitmap createBitmap (Bitmap src)

从原位图src复制出一个新的位图,和原始位图相同

2.public static Bitmap createBitmap (int[] colors, int width, int height, Bitmap.Config config)

这个函数根据颜色数组来创建位图,注意:颜色数组的长度>=width*height

此函数创建位图的过程可以简单概括为为:
更加width和height创建空位图,然后用指定的颜色数组colors来从左到右从上至下一次填充颜色。
config是一个枚举,可以用它来指定位图“质量”。
Bitmap.Config  config:一个枚举类型的配置,可以定义截到的新位图的质量

3.public static Bitmap createBitmap (int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
此方法与2类似,但我还不明白offset和stride的作用。

4.public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

从原始位图剪切图像,这是一种高级的方式。可以用Matrix(矩阵)来实现旋转等高级方式截图
    参数说明:
        Bitmap source:要从中截图的原始位图
        int x:起始x坐标
        int y:起始y坐标
        int width:要截的图的宽度
        int height:要截的图的宽度
        boolean filter:是否可编辑
    返回值:
        返回一个剪切好的Bitmap

5.public static Bitmap createBitmap (int width, int height, Bitmap.Config config)

根据参数创建新位图

6.public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)
简单的剪切图像的方法,可以参考上面的4.


Drawable Bitmap 相互转化:


     /**
      * Drawable to Bitmap
      */ 

     public static Bitmap drawableToBitmap(Drawable drawable) {    
        int width = drawable.getIntrinsicWidth();    
        int height = drawable.getIntrinsicHeight();    
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);    
        Canvas canvas = new Canvas(bitmap);    
        drawable.setBounds(0, 0, width, height);    
        drawable.draw(canvas);    
        return bitmap;    
         
     }   

     /**
      * Bitmap to Drawable
      * @param bitmap
      * @param mcontext
      * @return
      */  
     public static Drawable bitmapToDrawble(Bitmap bitmap,Context mcontext){  
         Drawable drawable = new BitmapDrawable(mcontext.getResources(), bitmap);  
         return drawable;  
     }  
     


[java]
view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.      * 图片反转 
  3.      * @param img 
  4.      * @return 
  5.      */  
  6.     public Bitmap toturn(Bitmap img){  
  7.         Matrix matrix = new Matrix();  
  8.         matrix.postRotate(90); /*旋转90度*/  
  9.         int width = bitmap.getWidth();  
  10.         int height =bitmap.getHeight();  
  11.         img = Bitmap.createBitmap(img, 00, width, height, matrix, true);  
  12.         return img;  
  13.     }  
  14.     /** 
  15.      * 图片缩放 
  16.      * @param bigimage 
  17.      * @param newWidth 
  18.      * @param newHeight 
  19.      * @return 
  20.      */  
  21.     public Bitmap tochange(Bitmap bigimage,int newWidth,int newHeight){  
  22.         // 获取这个图片的宽和高  
  23.         int width = bigimage.getWidth();  
  24.         int height = bigimage.getHeight();  
  25.         // 创建操作图片用的matrix对象  
  26.         Matrix matrix = new Matrix();  
  27.         // 计算缩放率,新尺寸除原始尺寸  
  28.         float scaleWidth = ((float) newWidth)/width;  
  29.         float scaleHeight = ((float) newHeight)/height;  
  30.         // 缩放图片动作  
  31.         matrix.postScale(scaleWidth, scaleHeight);  
  32.         Bitmap bitmap = Bitmap.createBitmap(bigimage, 00, width, height,matrix, true);  
  33.         return bitmap;  
  34.     }  


  1. /** 
  2.          * 程序切割图片 
  3.          * @param bitmap 
  4.          * @param x 
  5.          * @param y 
  6.          * @param w 
  7.          * @param h 
  8.          * @return 
  9.          */  
  10.         public Bitmap BitmapClipBitmap(Bitmap bitmap,int x, int y, int w, int h) {  
  11.             return  Bitmap.createBitmap(bitmap, x, y, w, h);  
  12.         }  
  1. /** 
  2.      * 图片叠加 
  3.      * @param b 
  4.      * @return 
  5.      */  
  6.     public Bitmap diejia(Bitmap b){  
  7.           
  8.         if(!b.isMutable()){  
  9.             //设置图片为背景为透明  
  10.             b = b.copy(Bitmap.Config.RGB_565, true);//  
  11.         }  
  12.         Canvas canvas = new Canvas(b);  
  13.         Bitmap lock=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);  
  14.         //叠加新图b2  
  15.         //注意此时绘制坐标是相对于图片b  
  16.         canvas.drawBitmap(lock, 00null);  
  17.         canvas.save(Canvas.ALL_SAVE_FLAG);  
  18.         canvas.restore();  
  19.         lock.recycle();  
  20.         lock=null;  
  21.         return b;  
  22.     }  

图片居中叠加:

  1. Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {  
  2.         final int bitmapWidth = bitmap.getWidth();  
  3.         final int bitmapHeight = bitmap.getHeight();  
  4.    
  5.         if (bitmapWidth < width || bitmapHeight < height) {  
  6.             int color = context.getResources().getColor(R.color.window_background);  
  7.    
  8.             Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,  
  9.                     bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);  
  10.             centered.setDensity(bitmap.getDensity());  
  11.             Canvas canvas = new Canvas(centered);  
  12.             canvas.drawColor(color);  
  13.             canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,  
  14.                     null);  
  15.    
  16.             bitmap = centered;  
  17.         }  
  18.    
  19.         return bitmap;  
  20.     }  



对图像进行相关参数转换,重新形成新的图片数据参数展示形式(Drawble)

  1. /** 
  2.     * Create a drawable from file path name. 
  3.     */  
  4. public Drawable createFromPath(String pathName) {  
  5.     if (pathName == null) {  
  6.         return null;  
  7.     }  
  8.   
  9.     BitmapFactory.Options opts = new BitmapFactory.Options();  
  10.     opts.inJustDecodeBounds = true;  
  11.   
  12.     opts.inJustDecodeBounds = true;  
  13.     BitmapFactory.decodeFile(pathName, opts);  
  14.   
  15.     opts.inSampleSize = computeSampleSize(opts, -11280 * 720);  
  16.     opts.inJustDecodeBounds = false;  
  17.   
  18.     Bitmap bm = BitmapFactory.decodeFile(pathName, opts);  
  19.   
  20.     if (bm != null) {  
  21.         return drawableFromBitmap(null, bm, nullnull, pathName);  
  22.     }  
  23.   
  24.     return null;  
  25. }  
  26.   
  27. private Drawable drawableFromBitmap(Resources res, Bitmap bm, byte[] np,  
  28.         Rect pad, String srcName) {  
  29.   
  30.     if (np != null) {  
  31.         return new NinePatchDrawable(res, bm, np, pad, srcName);  
  32.     }  
  33.   
  34.     return new BitmapDrawable(res, bm);  
  35. }  
  36.   
  37. public int computeSampleSize(BitmapFactory.Options options,  
  38.         int minSideLength, int maxNumOfPixels) {  
  39.     int initialSize = computeInitialSampleSize(options, minSideLength,  
  40.             maxNumOfPixels);  
  41.   
  42.     int roundedSize;  
  43.     if (initialSize <= 8) {  
  44.         roundedSize = 1;  
  45.         while (roundedSize < initialSize) {  
  46.             roundedSize <<= 1;  
  47.         }  
  48.     } else {  
  49.         roundedSize = (initialSize + 7) / 8 * 8;  
  50.     }  
  51.   
  52.     return roundedSize;  
  53. }  
  54.   
  55. private int computeInitialSampleSize(BitmapFactory.Options options,  
  56.         int minSideLength, int maxNumOfPixels) {  
  57.     double w = options.outWidth;  
  58.     double h = options.outHeight;  
  59.   
  60.     int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math  
  61.             .sqrt(w * h / maxNumOfPixels));  
  62.     int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(  
  63.             Math.floor(w / minSideLength), Math.floor(h / minSideLength));  
  64.   
  65.     if (upperBound < lowerBound) {  
  66.         // return the larger one when there is no overlapping zone.  
  67.         return lowerBound;  
  68.     }  
  69.   
  70.     if ((maxNumOfPixels == -1) && (minSideLength == -1)) {  
  71.         return 1;  
  72.     } else if (minSideLength == -1) {  
  73.         return lowerBound;  
  74.     } else {  
  75.         return upperBound;  
  76.     }  
  77. }  


  1. /**  
  2.     * create the bitmap from a byte array  
  3.     *生成水印图片  
  4.     * @param src the bitmap object you want proecss  
  5.     * @param watermark the water mark above the src  
  6.     * @return return a bitmap object ,if paramter's length is 0,return null  
  7.     */    
  8.    private Bitmap createBitmap( Bitmap src, Bitmap watermark )    
  9.    {    
  10.        String tag = "createBitmap";    
  11.        Log.d( tag, "create a new bitmap" );    
  12.        if( src == null )    
  13.        {    
  14.            return null;    
  15.        }    
  16.     
  17.        int w = src.getWidth();    
  18.        int h = src.getHeight();    
  19.        int ww = watermark.getWidth();    
  20.        int wh = watermark.getHeight();    
  21.        //create the new blank bitmap    
  22.        Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图    
  23.        Canvas cv = new Canvas( newb );    
  24.        //draw src into    
  25.        cv.drawBitmap( src, 00null );//在 0,0坐标开始画入src    
  26.        //draw watermark into    
  27.        cv.drawBitmap( watermark, w - ww + 5, h - wh + 5null );//在src的右下角画入水印    
  28.        //save all clip    
  29.        cv.save( Canvas.ALL_SAVE_FLAG );//保存    
  30.        //store    
  31.        cv.restore();//存储    
  32.        return newb;    
  33.    }    

水印图片2:

  1. public static Bitmap createWaterMakerBitmap(Bitmap bitmap, Bitmap watermaker){  
  2.        return createWaterMakerBitmap(bitmap, watermaker, null);  
  3.    }  
  4.   
  5.    public static Bitmap createWaterMakerBitmap(Bitmap bitmap, Bitmap watermaker, String title){  
  6.        Context context = GOApplication.getInstance();  
  7.        return createWaterMakerBitmap(bitmap, watermaker, title,  
  8.                TypefaceHelper.get(context, context.getString(R.string.config_default_font)));  
  9.    }  
  10.   
  11.    public static Bitmap createWaterMakerBitmap(Bitmap bitmap, Bitmap watermaker, String title, Typeface typeface){  
  12.        if(!TextUtils.isEmpty(title)) {  
  13.            return createWaterMakerBitmap(bitmap, watermaker, title, getDefaultPaint(), typeface);  
  14.        }  
  15.        return createWaterMakerBitmap(bitmap, watermaker, nullnullnull);  
  16.    }  
  17.   
  18. public static Bitmap createWaterMakerBitmap(Bitmap bitmap, Bitmap watermaker, String title, Paint paint,Typeface typeface){  
  19.        if(bitmap == null  
  20.                || (watermaker == null && TextUtils.isEmpty(title))) return bitmap;//nothing need todohere  
  21.        int width = bitmap.getWidth();  
  22.        int height = bitmap.getHeight();  
  23.        if(width>0 && height>0) {  
  24.            try {  
  25.                Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);  
  26.                Canvas canvas    = new Canvas(newBitmap);  
  27.                canvas.drawBitmap(bitmap, 00null);//draw src  
  28.                if(watermaker != null){  
  29.                    canvas.drawBitmap(watermaker, 00null);  
  30.                }  
  31.                if(!TextUtils.isEmpty(title)){  
  32.                    Paint mpaint = paint;  
  33.                    if(mpaint == null){  
  34.                        mpaint = getDefaultPaint();  
  35.                    }  
  36.                    if(typeface != null){  
  37.                        mpaint.setTypeface(typeface);  
  38.                    }  
  39.                    mpaint.setTextAlign(Paint.Align.CENTER);  
  40.                    canvas.drawText(title, width/3, height*3/4, mpaint);  
  41.                }  
  42.                canvas.save(Canvas.ALL_SAVE_FLAG);  
  43.                canvas.restore();  
  44.                return newBitmap;  
  45.            }catch (Throwable e){}  
  46.        }  
  47.        return null;  
  48.    }  
  49.   
  50.    public static Paint getDefaultPaint(){  
  51.        Paint paint = new Paint();  
  52.        paint.setColor(Color.parseColor("#ffff6600"));// #141,141,141  
  53.        paint.setTextSize(100);  
  54.        paint.setAntiAlias(true);  
  55.        paint.setStrokeWidth(10);  
  56.        paint.setMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.SOLID));  
  57.        return paint;  
  58.    }  



更多处理

  1. /**圆角处理 
  2.      * 实际上是在原图片上画了一个圆角遮罩。对于paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
  3.      * 方法我刚看到也是一知半解Mode.SRC_IN参数是个画图模式,该类型是指只显 示两层图案的交集部分,且交集部位只显示上层图像。 
  4.      * 实际就是先画了一个圆角矩形的过滤框,于是形状有了,再将框中的内容填充为图片 
  5.      * @param bitmap 
  6.      * @param roundPx 
  7.      * @return 
  8.      */  
  9.     public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx)  
  10.     {  
  11.    
  12.         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
  13.                 bitmap.getHeight(), Config.ARGB_8888);  
  14.         Canvas canvas =new Canvas(output);  
  15.    
  16.         final int color =0xff424242;  
  17.         final Paint paint =new Paint();  
  18.         final Rect rect =new Rect(0,0, bitmap.getWidth(), bitmap.getHeight());  
  19.         final RectF rectF =new RectF(rect);  
  20.    
  21.         paint.setAntiAlias(true);  
  22.         canvas.drawARGB(0,0,0,0);  
  23.         paint.setColor(color);  
  24.         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  25.    
  26.         paint.setXfermode(new android.graphics.PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));  
  27.         canvas.drawBitmap(bitmap, rect, rect, paint);  
  28.    
  29.         return output;  
  30.     }  
  31.     /** 
  32.      * 灰白处理 
  33.         就是利用了ColorMatrix 类自带的设置饱和度的方法setSaturation()。 
  34.         不过其方法内部实现的更深一层是利用颜色矩阵的乘法实现的,对于颜色矩阵的乘法下面还有使用 
  35.      * @param bmpOriginal 
  36.      * @return 
  37.      */  
  38.     public static Bitmap toGrayscale(Bitmap bmpOriginal)  
  39.     {  
  40.         int width, height;  
  41.         height = bmpOriginal.getHeight();  
  42.         width = bmpOriginal.getWidth();  
  43.    
  44.         Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,  
  45.                 Bitmap.Config.RGB_565);  
  46.         Canvas c =new Canvas(bmpGrayscale);  
  47.         Paint paint =new Paint();  
  48.         ColorMatrix cm =new ColorMatrix();  
  49.         cm.setSaturation(0);  
  50.         ColorMatrixColorFilter f =new ColorMatrixColorFilter(cm);  
  51.         paint.setColorFilter(f);  
  52.         c.drawBitmap(bmpOriginal,0,0, paint);  
  53.         return bmpGrayscale;  
  54.     }  
  55.       
  56.     /** 
  57.      * 黑白处理 
  58.     这张图片不同于灰白处理的那张,不同之处是灰白处理虽然没有了颜色, 
  59.     但是黑白的程度层次依然存在,而此张图片连层次都没有了,只有两个区别十分明显的黑白 颜色。 
  60.     实现的算法也很简单,对于每个像素的rgb值求平均数,如果高于100算白色,低于100算黑色。 
  61.     不过感觉100这个标准值太大了,导致图片白色区 域太多,把它降低点可能效果会更好 
  62.      * @param mBitmap 
  63.      * @return 
  64.      */  
  65.     public static Bitmap toblackAndwhite(Bitmap mBitmap)  
  66.     {  
  67.         int mBitmapWidth =0;  
  68.         int mBitmapHeight =0;  
  69.    
  70.         mBitmapWidth = mBitmap.getWidth();  
  71.         mBitmapHeight = mBitmap.getHeight();  
  72.         Bitmap bmpReturn = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight,  
  73.                 Bitmap.Config.ARGB_8888);  
  74.         int iPixel =0;  
  75.         for(int i =0; i < mBitmapWidth; i++)  
  76.         {  
  77.             for(int j =0; j < mBitmapHeight; j++)  
  78.             {  
  79.                 int curr_color = mBitmap.getPixel(i, j);  
  80.    
  81.                 int avg = (Color.red(curr_color) + Color.green(curr_color) + Color  
  82.                         .blue(curr_color)) /3;  
  83.                 if(avg >=100)  
  84.                 {  
  85.                     iPixel =255;  
  86.                 }  
  87.                 else  
  88.                 {  
  89.                     iPixel =0;  
  90.                 }  
  91.                 int modif_color = Color.argb(255, iPixel, iPixel, iPixel);  
  92.    
  93.                 bmpReturn.setPixel(i, j, modif_color);  
  94.             }  
  95.         }  
  96.         return bmpReturn;  
  97.     }  
  98.     /** 
  99.      * 镜像处理 
  100.      * 原理就是将原图片反转一下,调整一 下它的颜色作出倒影效果,再将两张图片续加在一起, 
  101.      * 不过如果在反转的同时再利用Matrix加上一些倾斜角度就更好了,不过那样做的话加工后的图片的高度需要同比例计算出来, 
  102.      * 不能简单的相加了,否则就图片大小就容不下现有的像素内容。 
  103.      * @param bitmap 
  104.      * @return 
  105.      */  
  106.     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap)  
  107.     {  
  108.         final int reflectionGap =4;  
  109.         int width = bitmap.getWidth();  
  110.         int height = bitmap.getHeight();  
  111.    
  112.         Matrix matrix =new Matrix();  
  113.         matrix.preScale(1, -1);  
  114.    
  115.         Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height /2,  
  116.                 width, height /2, matrix,false);  
  117.    
  118.         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
  119.                 (height + height /2), Config.ARGB_8888);  
  120.    
  121.         Canvas canvas =new Canvas(bitmapWithReflection);  
  122.         canvas.drawBitmap(bitmap,0,0,null);  
  123.         Paint deafalutPaint =new Paint();  
  124.         canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);  
  125.    
  126.         canvas.drawBitmap(reflectionImage,0, height + reflectionGap,null);  
  127.    
  128.         Paint paint =new Paint();  
  129.         LinearGradient shader =new LinearGradient(0, bitmap.getHeight(),0,  
  130.                 bitmapWithReflection.getHeight() + reflectionGap,0x70ffffff,  
  131.                 0x00ffffff, TileMode.CLAMP);  
  132.         paint.setShader(shader);  
  133.         // Set the Transfer mode to be porter duff and destination in  
  134.         paint.setXfermode(new android.graphics.PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));  
  135.         // Draw a rectangle using the paint with our linear gradient  
  136.         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
  137.                 + reflectionGap, paint);  
  138.    
  139.         return bitmapWithReflection;  
  140.     }  
  141.     /** 
  142.      * 加旧处理 
  143.      * @param bitmap 
  144.      * @return 
  145.      */  
  146.     public static Bitmap toOldBitmap(Bitmap bitmap)  
  147.     {  
  148.         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
  149.                 bitmap.getHeight(), Config.RGB_565);  
  150.    
  151.         Canvas canvas =new Canvas(output);  
  152.    
  153.         Paint paint =new Paint();         
  154.         ColorMatrix cm =new ColorMatrix();  
  155.         float[] array = {1,0,0,0,50,  
  156.                 0,1,0,0,50,  
  157.                 0,0,1,0,0,  
  158.                 0,0,0,1,0};  
  159.         cm.set(array);  
  160.         paint.setColorFilter(new ColorMatrixColorFilter(cm));  
  161.    
  162.         canvas.drawBitmap(bitmap,0,0, paint);  
  163.         return output;  
  164.     }  
  165.     /** 
  166.      *   浮雕处理 
  167.      * 观察浮雕就不难发现,其实浮雕的特点就是在颜色有跳变的地方就刻条痕迹。127,127,127为深灰色, 
  168.      * 近似于石头的颜色,此处取该颜色为底色。算法是将上一个点的rgba值减去当前点的rgba值然后加上127得到当前点的颜色。 
  169.      * @param mBitmap 
  170.      * @return 
  171.      */  
  172.     public static Bitmap to_embossment(Bitmap mBitmap)  
  173.     {  
  174.            
  175.    
  176.         int mBitmapWidth =0;  
  177.         int mBitmapHeight =0;  
  178.    
  179.         mBitmapWidth = mBitmap.getWidth();  
  180.         mBitmapHeight = mBitmap.getHeight();  
  181.         Bitmap bmpReturn = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight,  
  182.                 Bitmap.Config.RGB_565);  
  183.         int preColor =0;  
  184.         int prepreColor =0;  
  185.         preColor = mBitmap.getPixel(0,0);  
  186.    
  187.         for(int i =0; i < mBitmapWidth; i++)  
  188.         {  
  189.             for(int j =0; j < mBitmapHeight; j++)  
  190.             {  
  191.                 int curr_color = mBitmap.getPixel(i, j);  
  192.                 int r = Color.red(curr_color) - Color.red(prepreColor) +127;  
  193.                 int g = Color.green(curr_color) - Color.red(prepreColor) +127;  
  194.                 int b = Color.green(curr_color) - Color.blue(prepreColor) +127;  
  195.                 int a = Color.alpha(curr_color);  
  196.                 int modif_color = Color.argb(a, r, g, b);  
  197.                 bmpReturn.setPixel(i, j, modif_color);  
  198.                 prepreColor = preColor;  
  199.                 preColor = curr_color;  
  200.             }  
  201.         }  
  202.    
  203.         Canvas c =new Canvas(bmpReturn);  
  204.         Paint paint =new Paint();  
  205.         ColorMatrix cm =new ColorMatrix();  
  206.         cm.setSaturation(0);  
  207.         ColorMatrixColorFilter f =new ColorMatrixColorFilter(cm);  
  208.         paint.setColorFilter(f);  
  209.         c.drawBitmap(bmpReturn,0,0, paint);  
  210.    
  211.         return bmpReturn;  
  212.     }  
  213.     /** 
  214.      *   油画处理 
  215.      * 其实油画因为是用画笔画的,彩笔画的时候没有那么精确会将本该这点的颜色滑到另一个点处。 
  216.      * 算法实现就是取一个一定范围内的随机数,每个点的颜色是该点减去随机数坐标后所得坐标的颜色。 
  217.      * @param bmpSource 
  218.      * @return 
  219.      */  
  220.     public static Bitmap to_oilPainting(Bitmap bmpSource)  
  221.     {  
  222.         Bitmap bmpReturn = Bitmap.createBitmap(bmpSource.getWidth(),  
  223.                 bmpSource.getHeight(), Bitmap.Config.RGB_565);  
  224.         int color =0;  
  225.         int Radio =0;  
  226.         int width = bmpSource.getWidth();  
  227.         int height = bmpSource.getHeight();  
  228.    
  229.         Random rnd =new Random();  
  230.         int iModel =10;  
  231.         int i = width - iModel;  
  232.         while(i >1)  
  233.         {  
  234.             int j = height - iModel;  
  235.             while(j >1)  
  236.             {  
  237.                 int iPos = rnd.nextInt(100000) % iModel;  
  238.                 color = bmpSource.getPixel(i + iPos, j + iPos);  
  239.                 bmpReturn.setPixel(i, j, color);  
  240.                 j = j -1;  
  241.             }  
  242.             i = i -1;  
  243.         }  
  244.         return bmpReturn;  
  245.     }  
  246.       
  247.     /** 
  248.      *   模糊处理 
  249.      * 算法实现其实是取每三点的平均值做为当前点颜色,这样看上去就变得模糊了。 
  250.      * 这个算法是三点的平均值,如果能够将范围扩大,并且不是单纯的平均值, 
  251.      * 而是加权 平均肯定效果会更好。不过处理速度实在是太慢了,而Muzei这种软件在处理的时候 
  252.      * ,不仅仅速度特别快,而且还有逐渐变模糊的变化过程,显然人家不是用这 种算法实现的。 
  253.      * 他们的实现方法正在猜测中,实现后也来更新。 
  254.      * @param bmpSource 
  255.      * @param Blur 
  256.      * @return 
  257.      */  
  258.     public static Bitmap blurBitmap(Bitmap bmpSource,int Blur)  
  259.     {  
  260.         int mode =5;  
  261.         Bitmap bmpReturn = Bitmap.createBitmap(bmpSource.getWidth(),  
  262.                 bmpSource.getHeight(), Bitmap.Config.ARGB_8888);  
  263.         int pixels[] =new int[bmpSource.getWidth() * bmpSource.getHeight()];  
  264.         int pixelsRawSource[] =new int[bmpSource.getWidth()  
  265.                 * bmpSource.getHeight() *3];  
  266.         int pixelsRawNew[] =new int[bmpSource.getWidth()  
  267.                 * bmpSource.getHeight() *3];  
  268.    
  269.         bmpSource.getPixels(pixels,0, bmpSource.getWidth(),0,0,  
  270.                 bmpSource.getWidth(), bmpSource.getHeight());  
  271.    
  272.         for(int k =1; k <= Blur; k++)  
  273.         {  
  274.                
  275.             for(int i =0; i < pixels.length; i++)  
  276.             {  
  277.                 pixelsRawSource[i *3+0] = Color.red(pixels[i]);  
  278.                 pixelsRawSource[i *3+1] = Color.green(pixels[i]);  
  279.                 pixelsRawSource[i *3+2] = Color.blue(pixels[i]);  
  280.             }  
  281.                
  282.             int CurrentPixel = bmpSource.getWidth() *3+3;  
  283.            
  284.             for(int i =0; i < bmpSource.getHeight() -3; i++)  
  285.             {  
  286.                 for(int j =0; j < bmpSource.getWidth() *3; j++)  
  287.                 {  
  288.                     CurrentPixel +=1;  
  289.                     int sumColor =0;  
  290.                     sumColor = pixelsRawSource[CurrentPixel  
  291.                             - bmpSource.getWidth() *3];  
  292.                     sumColor = sumColor + pixelsRawSource[CurrentPixel -3];  
  293.                     sumColor = sumColor + pixelsRawSource[CurrentPixel +3];  
  294.                     sumColor = sumColor  
  295.                             + pixelsRawSource[CurrentPixel  
  296.                                     + bmpSource.getWidth() *3];  
  297.                     pixelsRawNew[CurrentPixel] = Math.round(sumColor /4);  
  298.                 }  
  299.             }  
  300.    
  301.             for(int i =0; i < pixels.length; i++)  
  302.             {  
  303.                 pixels[i] = Color.rgb(pixelsRawNew[i *3+0],  
  304.                         pixelsRawNew[i *3+1], pixelsRawNew[i *3+2]);  
  305.             }  
  306.         }  
  307.    
  308.         bmpReturn.setPixels(pixels,0, bmpSource.getWidth(),0,0,  
  309.                 bmpSource.getWidth(), bmpSource.getHeight());  
  310.         return bmpReturn;  
  311.     }  
  312.     /** 
  313.      * 图片合并 
  314.      * @param bitmap1 
  315.      * @param bitmap2 
  316.      * @param path 
  317.      * @return 
  318.      * @throws FileNotFoundException 
  319.      */  
  320.     public static Bitmap toJoinbitmap(Bitmap bitmap1,Bitmap bitmap2,String path) throws FileNotFoundException{  
  321.           
  322.         Bitmap bitmap3 = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), bitmap1.getConfig());  
  323.         Canvas canvas =new Canvas(bitmap3);  
  324.         canvas.drawBitmap(bitmap1,new Matrix(),null);  
  325.         canvas.drawBitmap(bitmap2,120,350,null); //120、350为bitmap2写入点的x、y坐标  
  326.         //将合并后的bitmap3保存为png图片到本地  
  327.         FileOutputStream out =new FileOutputStream(path+"/image3.png");  
  328.         bitmap3.compress(Bitmap.CompressFormat.PNG,90, out);  
  329.         return bitmap3;  
  330.     }  
  331.       
  332.     /**文字保存为png图片 
  333.      * @param path 文件保存路径 
  334.      * @param data 保存数据 
  335.      * @throws FileNotFoundException  
  336.      * */  
  337.     public static void textToImage(String path,ArrayList<String> data) throws FileNotFoundException{  
  338.           
  339.             int height = data.size()*20;     //图片高  
  340.             Bitmap bitmap = Bitmap.createBitmap(270,height, Config.ARGB_8888);  
  341.             Canvas canvas = new Canvas(bitmap);  
  342.             canvas.drawColor(Color.WHITE);   //背景颜色  
  343.                
  344.             Paint p = new Paint();  
  345.             p.setColor(Color.BLACK);   //画笔颜色  
  346.             p.setTextSize(15);         //画笔粗细  
  347.             for(int i=0;i<data.size();i++){  
  348.                 canvas.drawText(data.get(i),20,(i+1)*20,p);  
  349.             }  
  350.             FileOutputStream    out=new FileOutputStream(path);  
  351.             bitmap.compress(Bitmap.CompressFormat.PNG,90, out);  
  352.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值