Android圖片處理方法大全

Android中對圖片處理應用比較常見,所以整理了一些對圖片的基本操作處理功能方法:

  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.    }    


更多處理

  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.     }  
  353.      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章