2016-07-04 8 views
12

私はスケッチや描画によってビットマップの一部をコピーするのに役立つアンドロイドアプリを開発しています。私は、ユーザーがキャンバスにビットマップを背景として持ついくつかの図形を描画し、ビットマップ(ビットマトリックス/ 2Dビット配列)として色付けされた点を持っていることを意味します。ここまで、すべてのヒット音が良いと思う。ビットマップイメージからビットマップイメージをアンドロイドプログラミングでコピーする

ここで問題は、対応する真のビットを持つ画像の一部をマトリックスにコピーすることができますか? 必要に応じて追加情報を求めます。

ADDING説明:

1)メイン画像:

Image be fore any process

2)キャンバスの背景として画像:

Image as canvas background

3)キャンバス上のいくつかの絵画:

User paints on canvas

4)塗装面積のビット行列表現:

Bit matrix

5)期待される出力:

Where is painted is outputted

事前に感謝。

+0

ビットがUNsetの場合、どのようなデフォルト値を意味しますか? –

+0

@PaulStelian、彼らは透明になります。本当のビットがイメージから切り取られると仮定してください! – ConductedClever

+0

その透明性をどのように表現しますか? どのような出力がありますか? PNG? –

答えて

3

このようにすることができます。この例では、ソースとしてBitmapを使用し、フィルター・マトリックスとしてもう1つのBitmapを使用します。フィルタビットマップは透明な背景を有するので、濾過ビットマップがあります。例えば

public Bitmap doFilter(Bitmap source, Bitmap filter) { 
    Bitmap filtered = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig()); 

    Paint paint = new Paint(); 
    Canvas canvas = new Canvas(filtered); 

    canvas.drawBitmap(source, 0, 0, paint); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
    canvas.drawBitmap(filter, 0, 0, paint); 

    return filtered; 
} 

、このソースを有する:

enter image description here

そして、このフィルタ:

enter image description here

をこのフィルタリングされた画像は次のとおりです。

私は、次のサンプル・プロジェクトとコードは以下のとおりである作られ、あなたのした説明に基づいて

enter image description here

+0

私はあなたとDebuの両方に恩恵を与えることができたらいいですよ!両方の答えが素晴らしいです。私はそれらをテストするつもりですが、私はDebuの答えはより詳細ですが、antonioの方が良いと思います。 – ConductedClever

+0

私はあなたのコードをテストし、それはちょうど最高のように働いていた。ありがとう。 TG。 – ConductedClever

0

3つのビットマップがあります。最初はあなたが背景として示すものです。 2番目はユーザーがマスクを描く場所で、3番目は結果です。

背景イメージを表示し、マスクの上にビットマップを描画します。ユーザーがマスクを描画できるようにします。一度マスクに満足すれば、ピクセルを反復するだけです。空でないピクセルが見つかると、そのピクセルはコピーする必要があります。あなたはあなたの反復からのピクセル位置を持つので、バックグラウンドのビットマップからその位置を読み取ってその値を結果のビットマップに書き込むことができます。

+0

あなたは私の説明を自分に戻しています。非正方形の画像を出力する作業コードが必要です。 – ConductedClever

2

-

public class ActivityImage extends AppCompatActivity{ 
Button grab; 
CustomImageView iv_custom; 
ImageView iv_later; 
Bitmap bitmapBG,bitmapOrg; 
int iMin,jMin,iMax,jMax; 
@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.so_particular_img_section); 
    iv_custom = (CustomImageView)findViewById(R.id.iv_custom); 
    iv_custom.setDrawingCacheEnabled(true); 
    iv_later = (ImageView)findViewById(R.id.iv_later); 
    grab = (Button)findViewById(R.id.btn_grab); 
    grab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      iv_custom.buildDrawingCache(); 
      bitmapBG = iv_custom.getDrawingCache(); 
      getSelectedRegionOnly(); 
     } 
    }); 
} 

/** 
* this method will return the whole image But only the selected region is highlighted and the rest section is simply white 
*/ 
private void getSelectedRegionWithBG(){ 
    int[] mainImageArray = iv_custom.getImagePixels(); 
    int[] bgImageArray = new int[bitmapBG.getWidth() * bitmapBG.getHeight()]; 
    int[] finalImageArray = new int[bitmapBG.getWidth() * bitmapBG.getHeight()]; 
    bitmapBG.getPixels(bgImageArray,0,bitmapBG.getWidth(), 0, 0, bitmapBG.getWidth(), bitmapBG.getHeight()); 

    if(mainImageArray.length == bgImageArray.length){ 
     for(int i = 0; i < (bitmapBG.getWidth() * bitmapBG.getHeight());i++){ 
      if(mainImageArray[i] == bgImageArray[i]){ 
       finalImageArray[i] = Color.WHITE; 
      }else{ 
       finalImageArray[i] = mainImageArray[i]; 
      } 
     } 

     Bitmap finalBitmap = Bitmap.createBitmap(bitmapBG.getWidth(), bitmapBG.getHeight(), Bitmap.Config.ARGB_8888); 
     // Set the pixels 
     finalBitmap.setPixels(finalImageArray, 0, finalBitmap.getWidth(), 0, 0, finalBitmap.getWidth(), finalBitmap.getHeight()); 
     iv_later.setImageBitmap(finalBitmap); 
    }else{ 
     Toast.makeText(ActivityImage.this,"Array length are not same",Toast.LENGTH_SHORT).show(); 
    } 

} 

/** 
* This method will select only the selected region from the main image and create the bitmap 
*/ 
private void getSelectedRegionOnly(){ 
    generateBounds(); 
    int count = 0; 
    int[] finalImageArray = new int[(1+iMax - iMin) * (1+jMax - jMin)]; 
    Bitmap finalBitmap = Bitmap.createBitmap((1+jMax - jMin), (1+iMax - iMin), Bitmap.Config.ARGB_8888); 
     for(int i = iMin; i <= iMax; i++){ 
      for(int j = jMin; j <= jMax; j++){ 
       if(bitmapBG.getPixel(j,i) != bitmapOrg.getPixel(j,i)) { 
        finalImageArray[count] = bitmapOrg.getPixel(j, i); 
       }else { 
        finalImageArray[count] = Color.WHITE; 
       } 
       count++; 
      } 
     } 
     // Set the pixels 
     finalBitmap.setPixels(finalImageArray, 0, finalBitmap.getWidth(), 0, 0, finalBitmap.getWidth(), finalBitmap.getHeight()); 
     iv_later.setImageBitmap(finalBitmap); 

} 

/** 
* generates the bound of the coloured region 
*/ 
private void generateBounds(){ 
    bitmapOrg = iv_custom.getMainBitmap(); 
    iMax = jMax = 0; 
    iMin = jMin = bitmapBG.getWidth() * bitmapBG.getHeight(); 
    for(int i = 0; i < bitmapBG.getHeight(); i++){ 
     for(int j = 0; j < bitmapBG.getWidth(); j++){ 
      if(bitmapBG.getPixel(j,i) != bitmapOrg.getPixel(j,i)){ 
       if(iMin > i){ 
        iMin = i; 
       } 
       if(jMin > j){ 
        jMin = j; 
       } 
       if(iMax < i){ 
        iMax = i; 
       } 
       if(jMax < j){ 
        jMax = j; 
       } 
      } 
     } 
    } 
} 


} 

レイアウトファイル -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="GRAB IMAGE" 
    android:id="@+id/btn_grab" 
    android:layout_alignParentBottom="true"/> 
    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    android:id="@+id/demo" 
    android:orientation="vertical" 
    android:layout_above="@id/btn_grab" 
    > 
    <com.wandertails.stackovrflw.CustomImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/iv_custom" 
     android:layout_weight="1" /> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/iv_later" 
     android:layout_weight="1"/> 
</LinearLayout> 
</RelativeLayout> 

最後にカスタム画像を表示 -

public class CustomImageView extends ImageView { 
int width,height; 
Bitmap sample; 

Context con; 
Paint paint=new Paint(); 
public CustomImageView(Context context) { 
    super(context); 
    con = context; 
} 

public CustomImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    con = context; 
} 

public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    con = context; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    sample = createBackground(width,height,con); 
    canvas.drawBitmap(sample,0,0,paint); 
    canvas.drawBitmap(createForeground(width,height,con),0,0,paint); 
    paint.setColor(Color.RED); 
    //canvas.drawRect(400,400,600,600,paint); 
    //canvas.drawCircle(400,400,200,paint); 
} 

public int[] getImagePixels(){ 
    int[] pixels = new int[sample.getWidth() * sample.getHeight()]; 
    sample.getPixels(pixels, 0, sample.getWidth(), 0, 0, sample.getWidth(), sample.getHeight()); 
    return pixels; 
} 

public Bitmap getMainBitmap(){ 
    return sample; 
} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(w, h, oldw, oldh); 
    width = w; 
    height = h; 
} 

private Bitmap createBackground(int width, int height, Context con) { 
    Bitmap logo = null; 
    logo = BitmapFactory.decodeResource(con.getResources(), R.drawable.sample); 
    return Bitmap.createScaledBitmap(logo,width,height,true).copy(Bitmap.Config.ARGB_8888,true); 
} 

private Bitmap createForeground(int width, int height, Context con) { 
    Bitmap logo = null; 
    logo = BitmapFactory.decodeResource(con.getResources(), R.drawable.sample1); 
    return Bitmap.createScaledBitmap(logo,width,height,true).copy(Bitmap.Config.ARGB_8888,true); 
} 

} 

ここにいくつかあります私はacheiveスクリーンショットの - Here the green image is the pattern(like the yellow region of image 3 of yours) and the Horse image is the actual main image

Here the red colored shapes are patterns

コードが最適化されたことではないかもしれないが、私はそれがあなたの目的を果たすだろうと思います。さらに、任意の形状やデザインをマスク(選択が必要な領域)として使用することができます。説明が必要な場合は私に尋ねてください。

+0

私はあなたとantonioの両方に恩恵を与えることができたらよかった!両方の答えが素晴らしいです。私はそれらをテストするつもりですが、私はDebuの答えはより詳細ですが、antonioの方が良いと思います。 – ConductedClever

+0

マスキングだけが必要な場合のアントニオの答えは明らかですが、この例では次のような利点があります。 1.フィルタービットマップは不要です。 2.ユーザーが選択できるようなカスタムシナリオではユーザーが指で触れて選択することができます)任意のセクション画像、このコードは動作します – Debu

+0

3。最後に、200×200のイメージがあり、ユーザーが右上のセクションから40×40を選択した後、getSelectedRegionOnly()を使用して、選択したセクションの40×40ビットマップのみを持つことができます。 私のコードがあなたの問題を解決するならば、賞金は必要ありません。私の最大の報酬は – Debu

関連する問題