Reputation: 321
I am trying to display a bitmap image pixel by pixel (which explicitly means with some delay).
For that I am using two "for-loops", but it prints only a single row of pixels...
My code:
Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getBaseContext(), "Printing...", Toast.LENGTH_SHORT).show();
iImageArray = new int[bMap.getWidth()* bMap.getHeight()]; //initializing the array for the image size
bMap.getPixels(iImageArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight()); //copy pixel data from the Bitmap into the 'intArray' array
//Canvas canvas = new Canvas (bMap);
//replace the red pixels with yellow ones
for (int i=0; i < bMap.getHeight(); i++) {
for(int j=0; j<bMap.getWidth(); j++) {
iImageArray[j] = 0xFFFFFFFF;
}
}
bMap = Bitmap.createBitmap(iImageArray, bMap.getWidth(), bMap.getHeight(), Bitmap.Config.ARGB_8888);//Initialize the bitmap, with the replaced color
image.setImageBitmap(bMap);
//canvas.drawPoints(iImageArray, 0, bMap.getHeight()*bMap.getWidth(), paint);
}
});
And I want to print the bitmap in grayscale and for that I found this code...
public Bitmap toGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
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;
}
It is iImageArray[i] = 0xFFFFFFFF;
that I have to test, not the actual grayscale value...
Upvotes: 6
Views: 4836
Reputation: 8715
Your code looks basically correct except for your inner loop. Your comment says that you're setting them to yellow, but you're actually storing white. The loop will only affect the first row of pixels because your array index has a range of 0 to width-1. You can either calculate the index by multiplying (i*width+j) or keep a counter that increments.
Original Version:
//replace the red pixels with yellow ones
for (int i=0; i < bMap.getHeight(); i++)
{
for(int j=0; j<bMap.getWidth(); j++)
{
iImageArray[j] = 0xFFFFFFFF;
}
}
Fixed Version:
//replace the red pixels with yellow ones
int iWidth = bMap.getWidth();
for (int i=0; i < bMap.getHeight(); i++)
{
for(int j=0; j<bMap.getWidth(); j++)
{
iImageArray[(i*iWidth)+j] = 0xFF00FFFF; // actual value of yellow
}
}
Upvotes: 3
Reputation: 3666
I also made a program that reads the RGB colors of every pixels from an image. Here is my code to read:
int orgWidth = bmp.getWidth();
int orgHeight = bmp.getHeight();
//define the array size
int[] pixels = new int[orgWidth * orgHeight];
bmp.getPixels(pixels, 0, orgWidth, 0, 0, orgWidth, orgHeight);
for (int y = 0; y < orgHeight; y++){
for (int x = 0; x < orgWidth; x++){
}
}
This is how my array works. I made a question for ranged pixel check last week, however I also fixed it by my self :) Optimize Color range check with nested for loop
EDIT:
Now I see, sorry.
at: iImageArray[j] = 0xFFFFFFFF;
also add iImageArray[i] = 0xFFFFFFFF;
then it should work
Upvotes: 1