Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 【57】android圖片印刻,陽刻,素描圖效果處理

【57】android圖片印刻,陽刻,素描圖效果處理

編輯:關於Android編程

介紹我參與開發的妙趣剪紙app使用的圖片處理相關的技術

關於妙趣剪紙,各大android商店都可以下載,下面貼出小米商店的鏈接

妙趣剪紙下載

軟件效果截圖

這裡寫圖片描述
這裡寫圖片描述

如何實現上面的圖片處理效果呢

1.初始化高斯矩陣

ProcessFactory.IniGauss_2(ProcessFactory.gauss_radius);  //初始化高斯矩陣

2.轉化為灰度圖

Bitmap bmpGrayscale=ProcessFactory.toGray2(activity.imageBmp);  //轉化為灰度圖

3.反色

Bitmap bmpGauss=ProcessFactory.toInverse(bmpGrayscale); //反色

4.高斯模糊

bmpGauss=ProcessFactory.toGauss(bmpGauss); //高斯模糊

5.處理顏色減淡生成素描圖

toColorDodge()函數

/**
     * 處理顏色減淡
     * @param bmpGauss 高斯模糊完畢的圖像
     * @param bmpGrayscale 灰度圖像
     * @return
     */
     // 在原先的灰度圖上做顏色減淡,使用反色高斯圖輔助```
    bmpPapercut=ProcessFactory.toColorDodge(bmpGauss,bmpGrayscale);
    // TODO bmpColorDodge 圖即為素描圖

6.papercut處理

bmpPapercut=ProcessFactory.toPapercut(bmpPapercut);

7.膨脹處理

bmpPapercut = ProcessFactory.toPengzhang(bmpPapercut);for(int i = 0; i < 2; i++)
        {
            bmpPapercut = ProcessFactory.toPengzhang(bmpPapercut);
        }

8.腐蝕處理

for(int i = 0; i < 2; i++)
        {
            bmpPapercut = ProcessFactory.toFushi(bmpPapercut);
        }

9.frame處理

Bitmap min_img = ProcessFactory.toFramed(bmpPapercut);

最終陽刻算法結束

下面介紹印刻的處理算法

1.初始化高斯矩陣

ProcessFactory.IniGauss_2(ProcessFactory.gauss_radius);  //初始化高斯矩陣

2.轉化為灰度圖

Bitmap bmpGrayscale=ProcessFactory.toGray2(activity.imageBmp);  //轉化為灰度圖

3.反色

Bitmap bmpGauss=ProcessFactory.toInverse(bmpGrayscale); //反色

4.高斯模糊

bmpGauss=ProcessFactory.toGauss(bmpGauss); //高斯模糊

5.處理顏色減淡生成素描圖

toColorDodge()函數

/**
     * 處理顏色減淡
     * @param bmpGauss 高斯模糊完畢的圖像
     * @param bmpGrayscale 灰度圖像
     * @return
     */
     // 在原先的灰度圖上做顏色減淡,使用反色高斯圖輔助```
    bmpPapercut=ProcessFactory.toColorDodge(bmpGauss,bmpGrayscale);
    // TODO bmpColorDodge 圖即為素描圖

6.印刻處理

bmpPapercut=ProcessFactory.toYinkePapercut(bmpPapercut);

7.腐蝕處理

for(int i = 0; i < 2; i++)
            bmpPapercut = ProcessFactory.toFushi(bmpPapercut);

印刻結束,可以看出來,印刻和陽刻的前五步基本一樣

工具類是ProcessFactory,上面用到的所有函數的定義都在裡面可以找到

部分關鍵代碼貼出,如果進一步交流,請加我下面的微信

/**
     * 初始化高斯矩陣
     * @param fi
     */
    public static void IniGauss_2(int fi)
    {
        toOne = 0;           //一定要對此變量進行初始化操作!
        GAUSS = new double[(fi*2+1)*(fi*2+1)];
        int index = 0;

        for (int x=-fi; x<=fi; x++){
            for (int y=-fi; y<=fi; y++){
                double sqrtFi = sigma*sigma;
                double ex = Math.pow(Math.E, (-(double)(x*x + y*y)/(2*(double)sqrtFi)));
                double result = ex/(double)(2 * Math.PI * sqrtFi);
                GAUSS[index] = result;
                toOne += result;
                index++;
                //MessageBox.Show(result.ToString());
                }
            }
        for (int i = 0; i < index; i++){
            GAUSS[i] = GAUSS[i] / toOne;
            //System.out.println("GAUSS["+i+"] = " + GAUSS[i]);
        }

        double sum = 0;
        for( double i : GAUSS) {
            sum += i;
        }
        //System.out.println("sum is"+sum);

    }

    /**
     * 取灰度圖像函數1
     * @param bmpOriginal
     * @return
     */
    public static Bitmap toGray1(Bitmap bmpOriginal){ 
        int width = bmpOriginal.getWidth(); //獲取位圖的寬 
        int height = bmpOriginal.getHeight(); //獲取位圖的高 

        int[] pixels = new int[width*height]; //通過位圖的大小創建像素點數組 

        bmpOriginal.getPixels(pixels, 0, width, 0, 0, width, height); 
        int alpha = (pixels[0] & 0xFF000000)>>24; 
        //int alpha = (byte)0xFF; 
        for(int i = 0; i < height; i++){ 
            for(int j = 0; j < width; j++){ 
                int pixel_src = pixels[width * i + j]; 
                int red = (pixel_src & 0x00FF0000 ) >> 16; 
                int green = (pixel_src & 0x0000FF00) >> 8; 
                int blue = pixel_src & 0x000000FF; 
                //注意需要先轉換成float類型
                int pixel_gray = (int)(((float)red) * 0.299 + ((float)green) * 0.587 + ((float)blue) * 0.114);
                int pixel_output = ((alpha <<24) & 0xFF000000) | ((pixel_gray << 16) & 0x00FF0000) | 
                        ((pixel_gray << 8) & 0x0000FF00) | (pixel_gray & 0x000000FF); 
                pixels[width * i + j] = pixel_output; 
                } 
            } 
        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Config.ARGB_8888); 
        bmpGrayscale.setPixels(pixels, 0, width, 0, 0, width, height); 
        return bmpGrayscale; 

        //bmpOriginal.setPixels(pixels, 0, width, 0, 0, width, height); 
        //return bmpOriginal;   
    }

//  public static Bitmap toGray5(Bitmap bmpOriginal){
//      int row;
//      int pixel;
//      int R, G, B, A = 255;
//      
//      int width = bmpOriginal.getWidth(); //獲取位圖的寬 
//      int height = bmpOriginal.getHeight(); //獲取位圖的高 
//      int[] pixels = new int[width*height]; //通過位圖的大小創建像素點數組 
//      bmpOriginal.getPixels(pixels, 0, width, 0, 0, width, height); 
//      
//      for(int i = 0; i < height; i++)
//      { 
//          row = width * i;
//          for(int j = 0; j < width; j++)
//          { 
//              int pixel_src = pixels[row + j]; 
//              
//              R = (pixel_src & 0x00FF0000 ) >> 16; 
//              G = (pixel_src & 0x0000FF00) >> 8; 
//              B = pixel_src & 0x000000FF; 
//              
//              pixel = (int)(R * 0.299 + G * 0.587 + B * 0.114);
//              R = G = B = pixel;
//              
//              pixel = (A << 24) | (R << 16) | (G << 8) | B; 
//              pixels[row + j] = pixel; 
//          } 
//      } 
//      Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Config.ARGB_8888); 
//      bmpGrayscale.setPixels(pixels, 0, width, 0, 0, width, height); 
//      return bmpGrayscale; 
//  }

    /**
     * 取灰度圖像函數2
     * @param bmpOriginal
     * @return
     */
     public static Bitmap toGray2(Bitmap bmpOriginal) {
        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();    

        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
        }

     /**
      * 取反色
      * @param bmpOriginal
      * @return
      */
     public static Bitmap toInverse(Bitmap bmpOriginal){
        int width = bmpOriginal.getWidth(); //獲取位圖的寬 
        int height = bmpOriginal.getHeight(); //獲取位圖的高 

        int[] pixels = new int[width*height]; //通過位圖的大小創建像素點數組 

        bmpOriginal.getPixels(pixels, 0, width, 0, 0, width, height); 
        int alpha = (byte)((pixels[0] & 0xFF000000)>>24); 
        for(int i = 0; i < height; i++){ 
            for(int j = 0; j < width; j++){ 
                int pixel_src = pixels[width * i + j]; 
                int red = ((pixel_src & 0x00FF0000 ) >> 16); 
                int green = ((pixel_src & 0x0000FF00) >> 8); 
                int blue = (pixel_src & 0x000000FF); 

                red = 255 - red;
                green = 255 - green;
                blue = 255 - blue;

                pixel_src = (alpha<<24) | (red << 16) | (green << 8) | blue; 
                pixels[width * i + j] = pixel_src; 
                } 
            } 
        Bitmap bmpInverse = Bitmap.createBitmap(width, height, Config.ARGB_8888); 
        bmpInverse.setPixels(pixels, 0, width, 0, 0, width, height); 
        return bmpInverse; 

//      bmpOriginal.setPixels(pixels, 0, width, 0, 0, width, height); 
//      return bmpOriginal; 
        }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved