从ANDROID编程中的位图图像复制位矩阵

时间:2016-07-04 09:29:04

标签: android matrix bitmap

我正在开发一个Android应用程序,帮助用户通过草绘或绘图来复制位图的一部分。我的意思是用户将在Canvas上绘制一些具有位图作为背景的形状,然后我将这些点着色为位图(位矩阵/ 2D位阵列)。直到这里,每一个声音听起来都不错。

现在的问题是如何复制矩阵中具有相应真位的图像部分?

添加说明

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

3 个答案:

答案 0 :(得分:3)

你可以这样做。该示例使用Bitmap作为源,另一个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

答案 1 :(得分: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);
}

}

以下是我实现的一些屏幕截图 - 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

代码可能没有那么优化,但我认为它将满足您的目的。此外,你可以使用任何形状或设计作为面具(需要选择的区域),问我是否需要任何解释..

答案 2 :(得分:0)

我会有三个位图。第一个是你展示的背景。第二个是用户绘制掩码的地方,第三个是结果。

显示背景图像并在其上绘制蒙版位图。允许用户在蒙版上绘图。一旦他们对掩码感到满意,只需迭代像素,当你发现一个不是空的时,你知道这是一个应该被复制的像素。您将获得迭代中的像素位置,这样您就可以从背景位图中读取该位置,并将该值写入结果位图。