3

I'm starting with Android and I work on Image processing. I would like to create a grid with random black and white pixels. I tried with bitmap but I don't really get how it works.

I tried with the following code but it says: The type of the expression must be an array type but it resolved to Matrix

Even without this error, I'm not sure that this is the right way to proceed. So if you have any ideas ...

Thank you !

import java.util.Random;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {       

    private ImageView img;           

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView)findViewById(R.id.imageView1);                     
    }

    public void RandomImage (View view)
    {
        Random rand = new Random(); 

        Matrix mat = new Matrix();
        for (int i=0; i<100; i++)
        {
            for (int j=0;  j<100 ; j++)
            {                       
                mat[i][j]=rand.nextInt(1);
                img.setImageMatrix(mat);                                     
            }                                               
        }                                   
    }                   
}
Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
user3855955
  • 87
  • 10
  • 2
    i dont think `img.setImageMatrix(mat);` should be inside your `for` loops. But thats a seperate issue – IAmGroot Nov 21 '14 at 14:16
  • Yeah you're right.It wasn't on purpose. I'll change it. – user3855955 Nov 21 '14 at 14:20
  • Another point is that `nextInt(1)` will return just 0 I think (you should check this). `nextInt(2)` would return 0 or 1. Consider drawing squares (or pixels depending on what scale you want) onto a custom canvas. to achieve a grid like drawing. Or onto a bitmap using a canvas, to return a grid image.. See http://stackoverflow.com/questions/5663671/creating-an-empty-bitmap-and-drawing-though-canvas-in-android for more – IAmGroot Nov 21 '14 at 14:26

2 Answers2

1

You can use bitmap to achieve this, first create a bitmap with prefered size (100*100 in this example) :

 int width = 100;
 int height = 100;

 Bitmap randomGridBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565);

Color this bitmap with black or white pixels (go through each pixel and assigne a "random color") :

Random rand = new Random();

for (int i=0; i<width; i++){
    for (int j=0;  j<height ; j++){
        // default color : Black
        int colorToPut = Color.BLACK;

        // If lucky get a white pixel ;)
        if(rand.nextInt(2)==0){
            colorToPut = Color.WHITE;
        }

        // Set color to (i,j) pixel
        randomGridBitmap.setPixel(i,j,colorToPut);
    }
}

Finally put this bitmap in your imageview :

imv.setImageBitmap(randomGridBitmap);

Globally your Activity should look like this :

public class MainActivity extends ActionBarActivity {       

    private ImageView img;           

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView)findViewById(R.id.imageView1); 
        createAndSetRandomImage();                    
    }

    public void createAndSetRandomImage(){
        Random rand = new Random();

        for (int i=0; i<width; i++){
            for (int j=0;  j<height ; j++){
            // default color : Black
            int colorToPut = Color.BLACK;

            // If lucky get a white pixel ;)
            if(rand.nextInt(2)==0){
                colorToPut = Color.WHITE;
            }

            // Set color to (i,j) pixel
            randomGridBitmap.setPixel(i,j,colorToPut);
            }
        }
        imv.setImageBitmap(randomGridBitmap);                        
    }                   
}

You try to use setImageMatrix() method (which is strangely not documented) but it is use to transform image of the imageview (for example rotate or custom scale image) and not set an image.

Gaëtan Maisse
  • 12,208
  • 9
  • 44
  • 47
  • This is exactly what i suggested, but with out the example :) +1. I think there is also scope for drawing small squares, rather than just pixels. Depending on requirements. – IAmGroot Nov 21 '14 at 14:38
  • So I tried what you suggested. I really like the idea ! There are no errors but when I run the app nothing happens ... – user3855955 Nov 21 '14 at 16:04
  • How your method RandomImage is called ? There is nothing related to it in onCreate – Gaëtan Maisse Nov 22 '14 at 10:29
-1

I personally suggest you to go for GridView and customize this as per your need GridView

Hope this helps.

Ashish Tamrakar
  • 810
  • 10
  • 24