以不規則圖片爲邊框,切割另外的圖片

最近工作上遇到了一個將一個圖片按照相框進行裁剪的問題,花了一個下午研究了下,在此整理一下,以便後用。


          +            =     

   (相片)                                              (相框)                                             (結果圖)

一個按照邊框裁剪的類,因爲相框通體爲藍色,只是不同區域的透明度不同,所以,選用了根據透明度來篩選邊界上的點的方法。

另外,這個類也提供了判斷一個點是否是在相框內部的方法:
和當前像素點同一水平線上的各點,假如左右兩側均有相框藍色邊界上的點的話,並且當前點不在邊框上則認爲當前點在相框的內部
反之,則認爲當前點在相框的外部。
  1. package com.example.test_filter;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Color;  
  5. import android.graphics.Matrix;  
  6. import android.graphics.Bitmap.Config;  
  7.   
  8. public class AlphaFilter implements Filter{  
  9.   
  10.     public static final int INSIDE_MODE = 0;  
  11.     public static final int EDGE_MODE = 1;  
  12.     public static final int OUTSIDE_MODE = 2;  
  13.       
  14.     public int findNonOpaque(int x, int y, int width, int height, int[] srcPixels) {  
  15.         if(width < height) {  
  16.             return findNonOpaqueByX(x, y, width, height, srcPixels);  
  17.         } else {  
  18.             return findNonOpaqueByY(x, y, width, height, srcPixels);  
  19.         }  
  20.     }  
  21.       
  22.     /* 
  23.      * 橫向檢測左右兩側邊界 
  24.      */  
  25.     public int findNonOpaqueByX(int x, int y, int width, int height, int[] srcPixels) {  
  26.         int mode = OUTSIDE_MODE;  
  27.         //當前點左右兩側均有邊界點出現,則認爲當前點在內部或者邊框中  
  28.         if(findNonOpaqueByXLeft(x, y, width, height, srcPixels) && findNonOpaqueByXRight(x, y, width, height, srcPixels)) {  
  29.             mode = INSIDE_MODE;  
  30.         }  
  31.         int pos = y*width+x;  
  32.         if(isMatch(pos, srcPixels)) {  
  33.             mode = EDGE_MODE;  
  34.         }  
  35.         return mode;  
  36.     }  
  37.       
  38.     /* 
  39.      * 檢測與當前點y座標相同的左側各點是否有邊界存在 
  40.      */  
  41.     public boolean findNonOpaqueByXLeft(int x, int y, int width, int height, int[] srcPixels) {  
  42.         for(int i=0; i < x; i++) {  
  43.             int pos = y*width + i;  
  44.             if(isMatch(pos, srcPixels)) {  
  45.                 return true;  
  46.             }  
  47.         }  
  48.         return false;  
  49.     }  
  50.       
  51.     /* 
  52.      * 檢測與當前點y座標相同的右側各點是否有邊界存在 
  53.      */  
  54.     public boolean findNonOpaqueByXRight(int x, int y, int width, int height, int[] srcPixels) {  
  55.         for(int i= x+1; i < width; i++) {  
  56.             int pos = y*width + i;  
  57.             if(isMatch(pos, srcPixels)) {  
  58.                 return true;  
  59.             }  
  60.         }  
  61.         return false;  
  62.     }  
  63.       
  64.     /* 
  65.      * 縱向檢測上下兩側的邊界 
  66.      */  
  67.     public int findNonOpaqueByY(int x, int y, int width, int height, int[] srcPixels) {  
  68.         int mode = OUTSIDE_MODE;  
  69.         //當前點上下兩側均有邊界點出現,則認爲當前點在內部或者邊框中  
  70.         if(findNonOpaqueByYTop(x, y, width, height, srcPixels) && findNonOpaqueByYBottom(x, y, width, height, srcPixels)) {  
  71.             mode = INSIDE_MODE;  
  72.         }  
  73.         int pos = y*width+x;  
  74.         if(isMatch(pos, srcPixels)) {  
  75.             mode = EDGE_MODE;  
  76.         }  
  77.         return mode;  
  78.     }  
  79.       
  80.     /* 
  81.      * 檢測與當前點x座標相同的上方各點是否有邊界存在 
  82.      */  
  83.     public boolean findNonOpaqueByYTop(int x, int y, int width, int height, int[] srcPixels) {  
  84.         for(int i=0; i < y; i++) {  
  85.             int pos = i*width + x;  
  86.             if(isMatch(pos, srcPixels)) {  
  87.                 return true;  
  88.             }  
  89.         }  
  90.         return false;  
  91.     }  
  92.       
  93.     /* 
  94.      * 檢測與當前點x座標相同的下方各點是否有邊界存在 
  95.      */  
  96.     public boolean findNonOpaqueByYBottom(int x, int y, int width, int height, int[] srcPixels) {  
  97.         for(int i=y+1; i < height; i++) {  
  98.             int pos = i*width + x;  
  99.             if(isMatch(pos, srcPixels)) {  
  100.                 return true;  
  101.             }  
  102.         }  
  103.         return false;  
  104.     }  
  105.       
  106.     public boolean isMatch(int pos, int[]srcPixels) {  
  107.         int color = srcPixels[pos];  
  108.         int alpha = Color.alpha(color);  
  109.         //檢測是否是邊界,針對背景圖片選用透明度進行過濾  
  110.         if(alpha >= 94 && alpha < 255) {  
  111.             return true;  
  112.         }  
  113.         return false;  
  114.     }  
  115.       
  116.     /** 
  117.      * 圖片效果疊加 
  118.      * @param bmp 要裁剪的圖片 
  119.      * @param filter 邊框 
  120.      * @return 
  121.      */  
  122.     public Bitmap overlay(Bitmap bmp, Bitmap filter)  
  123.     {  
  124.         int width = bmp.getWidth();  
  125.         int height = bmp.getHeight();  
  126.         Bitmap overlay = filter;  
  127.         Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_4444);  
  128.         bitmap.setHasAlpha(true);   
  129.           
  130.         // 對邊框圖片進行縮放  
  131.         int w = overlay.getWidth();  
  132.         int h = overlay.getHeight();  
  133.         float scaleX = width * 1F / w;  
  134.         float scaleY = height * 1F / h;  
  135.         Matrix matrix = new Matrix();  
  136.         matrix.postScale(scaleX, scaleY);  
  137.           
  138.         Bitmap overlayCopy = Bitmap.createBitmap(overlay, 00, w, h, matrix, true);  
  139.           
  140.         int[] srcPixels = new int[width * height];  
  141.         int[] layPixels = new int[width * height];  
  142.         bmp.getPixels(srcPixels, 0, width, 00, width, height);  
  143.         overlayCopy.getPixels(layPixels, 0, width, 00, width, height);  
  144.           
  145.         int pos = 0;  
  146.         for (int i = 0; i < height; i++)  
  147.         {  
  148.             for (int k = 0; k < width; k++)  
  149.             {  
  150.                 pos = i * width + k;  
  151.                   
  152.                 int mode = findNonOpaque(k, i, width, height, layPixels);  
  153.                 if(mode == INSIDE_MODE) {  
  154.                     srcPixels[pos] = srcPixels[pos];  
  155.                     continue;  
  156.                 } else if(mode == EDGE_MODE){  
  157.                     srcPixels[pos] = layPixels[pos];  
  158.                 } else{  
  159.                     srcPixels[pos] = 0;  
  160.                     continue;  
  161.                 }  
  162.                   
  163.             }  
  164.         }  
  165.         bitmap.setPixels(srcPixels, 0, width, 00, width, height);  
  166.         return bitmap;  
  167.     }  
  168. }  


  1. package com.example.test_filter;  
  2.   
  3. import android.os.AsyncTask;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.widget.ImageView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     ImageView filter;  
  13.     AlphaFilter alphaFilter;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.         filter = (ImageView) findViewById(R.id.filter);  
  19.         alphaFilter = new AlphaFilter();  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void onResume() {  
  24.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.album_photo_0);  
  25.         Bitmap frame = BitmapFactory.decodeResource(getResources(), R.drawable.first_album_frame);  
  26.         new BitmapFilter().execute(bitmap, frame);  
  27.         super.onResume();  
  28.     }  
  29.   
  30.     class BitmapFilter extends AsyncTask<Bitmap, Bitmap, Bitmap> {  
  31.   
  32.         @Override  
  33.         protected Bitmap doInBackground(Bitmap... params) {  
  34.             Bitmap bitmap = alphaFilter.overlay(params[0], params[1]);  
  35.             return bitmap;  
  36.         }  
  37.   
  38.         @Override  
  39.         protected void onPostExecute(Bitmap result) {  
  40.             filter.setImageBitmap(result);  
  41.             super.onPostExecute(result);  
  42.         }  
  43.           
  44.           
  45.     }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章