My doubt is bitmap.getPixels(allPixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
is processing 1D array. But a bitmap is always 2D picture representation. But why there is single dimentional array?
And how the packing of bytes in 1D array?
I know this is anoob question, but I can't understand it.
Thanks
But a bitmap is always 2D picture representation. But why there is
single dimentional array?
Bitmap stored in memory as 1-dimensional array of bytes (not only bitmap, but most binary data). All pixels of the bitmap are placed in memory row by row and each row with width of bitmap. I think, method Bitmap.getPixels() do nothing but copy bytes from memory into int[] array. You are free to create your own method that will convert 1D array to 2D array, but in most cases this is not required (see below).
And how the packing of bytes in 1D array?
Method Bitmap.getPixels() accepts and fills int[] array with length of bitmap width multiply by bitmap height. The part of result array corresponding to the rectangle, specified in parameters of the method, will be filled with colors of pixels, and rest of array will filled with zeros.
It's very easy to get the color of the desired pixel from this array. Index of pixel is x + y * bitmapWidth:
...
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] allPixels = new int[width * height];
bitmap.getPixels(allPixels, 0, width, 0, 0, width, height);
int x = 64;
int y = 128;
int pixelColor = allPixels[x + y * width];
...
Related
I have an application that currently takes black and white 176 x 144 images via camera2 in jpeg format and saves it to storage. In addition to this, I also need an int/float array where each point corresponds to the intensity of one pixel from the jpeg image. As the image is black and white this array of numbers should be sufficient to reconstruct my image by simply plotting it as a heatmap along the appropriate dimensions, as only one value per pixel is needed in black and white space.
The way I have found to do this is to convert the jpeg into a byte array into a bitmap into an int array of sRGB values into an int array of R values. This does work (code below), but seems like a really longwinded and inefficient way of doing this. Is anyone able to suggest a more direct way? Such as getting pixel values directly from the original jpeg Image?
// Convert photo (176 x 144) to byte array (1x25344)
Image mImage = someImage // jpeg capture from camera
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
//Save photo as jpeg
savePhoto(bytes);
//Save pixel values by converting to Bitmap first
Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
int x = image.getWidth();
int y = image.getHeight();
int[] intArray = new int[x * y];
image.getPixels(intArray, 0, x, 0, 0, x, y);
for(int i = 0; i < intArray.length; i++) {
intArray[i] = Color.red(intArray[i]); //Any colour will do
}
//Save pixel values
saveIntArray(intArray);
in my application i have a database where i store image .i want to retrieve images.images are easly retrieve in BitMap but the problem is how can i convert that BitMap image into integer format.
this is code:
DataBaseClass objOfDataBaseClass=new DataBaseClass(context);
Cursor mCursor=objOfDataBaseClass.showData();//here is all data taken from dataBase
if(mCursor.moveToNext()) {
byte[] mg=null;
mg = mCursor.getBlob(mCursor.getColumnIndex("image"));
Bitmap bitmap = BitmapFactory.decodeByteArray(mg, 0, mg.length);
int[] store=?//how can i bitMap store in this array.
}
You can use the Bitmap.getPixels method (documentation)
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] store = new int[width * height];
bitmap.getPixels(store, 0, width, 0, 0, width, height);
You shouldn't store the pixels of the bitmap itself, as a bitmap contains the image in uncompressed form and that would result in a huge amount of data (e.g. 1024 x 768 x 32 bit is 3.1 MB).
Better compress the bitmap to e.g. an PNG and store that in the database. You can use Bitmap.compress() for that purpose and use a ByteArrayOutputStream to write the data to. Then, store the bytes of that outputstream to your database.
I have a jpg image. First i want to convert it to bitmap so that i can get all pixels from my picture.
Then i want to keep the pixels into a two dimensional array as row and column of a matrix.
Then i want to search either row or column or both to match with a color.That means i want to find if a row(or a column whatever i want) of that 2D matrix contains RED color(for example). I tried something like this:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.laser);
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
int[] pixels = new int[bmp.getHeight()*bmp.getWidth()];
bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
for(int i =0; i<pixels.length;i++){
if(pixels[i]==0xffff0000)
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", Toast.LENGTH_LONG).show();
}
Here i took pixels into an array(1D) and check if a red color is exist or not in that array.
But it didn't find although red color exists in my pic.
Here may be the array cant get pixels.
But this is not what i want. What i want mentioned above. How can i reach my goal???
if you have a Bitmap object, you can get pixels using Bitmap::getPixels method.
for example, here i get first column of an image:
// byte[] data2 contains image binary date from the network
Bitmap bitmap = BitmapFactory.decodeByteArray(data2, 0, data2.length);
int[] pixels = new int[bitmap.getHeight()];
int offset = 0;
int stride = 1;
int x = 0;
int y = 0;
int width = 1;
int height = bitmap.getHeight();
bitmap.getPixels(pixels, offset, stride, x, y, width, height);
each pixels is just an Color object so you can check it's RGB values.
I need some info on the possible methods for dividing a bitmap in smaller pieces.
More importantly I would need some options to judge. I have checked many posts and I am still not entirely convinced about what to do:
cut the portion of bitmap
How do I cut out the middle area of the bitmap?
These two posts are some good options I found, but I cant calculate the CPU and RAM cost of each method, or maybe I should not bother with this calculation at all. Nonetheless if I am about to do something, why not do it the best way from the start.
I would be grateful to get some tips and links on bitmap compression so maybe I get better performance combining the two methods.
This function allows you to split a bitmap into and number of rows and columns.
Example Bitmap[][] bitmaps = splitBitmap(bmp, 2, 1);
Would create a vertically split bitmap stored in a two dimensional array.
2 columns 1 row
Example Bitmap[][] bitmaps = splitBitmap(bmp, 2, 2);
Would split a bitmap into four bitmaps stored in a two dimensional array.
2 columns 2 rows
public Bitmap[][] splitBitmap(Bitmap bitmap, int xCount, int yCount) {
// Allocate a two dimensional array to hold the individual images.
Bitmap[][] bitmaps = new Bitmap[xCount][yCount];
int width, height;
// Divide the original bitmap width by the desired vertical column count
width = bitmap.getWidth() / xCount;
// Divide the original bitmap height by the desired horizontal row count
height = bitmap.getHeight() / yCount;
// Loop the array and create bitmaps for each coordinate
for(int x = 0; x < xCount; ++x) {
for(int y = 0; y < yCount; ++y) {
// Create the sliced bitmap
bitmaps[x][y] = Bitmap.createBitmap(bitmap, x * width, y * height, width, height);
}
}
// Return the array
return bitmaps;
}
You want to divide a bitmap into parts. I assume you want to cut equal parts from bitmap. Say for example you need four equal parts from a bitmap.
This is a method which divides a bitmap onto four equal parts and has it in an array of bitmaps.
public Bitmap[] splitBitmap(Bitmap src) {
Bitmap[] divided = new Bitmap[4];
imgs[0] = Bitmap.createBitmap(
src,
0, 0,
src.getWidth() / 2, src.getHeight() / 2
);
imgs[1] = Bitmap.createBitmap(
src,
src.getWidth() / 2, 0,
src.getWidth() / 2, src.getHeight() / 2
);
imgs[2] = Bitmap.createBitmap(
src,
0, src.getHeight() / 2,
src.getWidth() / 2, src.getHeight() / 2
);
imgs[3] = Bitmap.createBitmap(
src,
src.getWidth() / 2, src.getHeight() / 2,
src.getWidth() / 2, src.getHeight() / 2
);
return divided;
}
I want to print a Bitmap to a mobile Bluetooth Printer (Bixolon SPP-R200) - the SDK doesn't offer direkt methods to print an in-memory image. So I thought about converting a Bitmap like this:
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
To a Monochrome Bitmap. I am drawing black text on above given Bitmap using a Canvas, which works well. However, when I convert the above Bitmap to a ByteArray, the printer seems to be unable to handle those bytes. I suspect I need an Array with one Bit per Pixel (a Pixel would be either white = 1 or black = 0).
As there seems to be no convenient, out of the box way to do that, one idea I had was to use:
bitmap.getPixels(pixels, offset, stride, x, y, width, height)
to Obtain the pixels. I assume, I'd have to use it as follows:
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int [] pixels = new int [width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
However - I am not sure about a few things:
In getPixels - does it make sense to simply pass the width as the "Stride" argument?
I guess I'd have to evaluate the color information of each pixel and either switch it to black or white (And I'd write this value in a new target byte array which I would ultimately pass to the printer)?
How to best evaluate each pixel color information in order to decide that it should be black or white? (The rendered Bitmap is black pain on a white background)
Does this approach make sense at all? Is there an easier way? It's not enough to just make the bitmap black & white, the main issue is to reduce the color information for each pixel into one bit.
UPDATE
As suggested by Reuben I'll first convert the Bitmap to a monochrome Bitmap. and then I'll iterate over each pixel:
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
// Iterate over height
for (int y = 0; y < height; y++) {
int offset = y * height;
// Iterate over width
for (int x = 0; x < width; x++) {
int pixel = bitmap.getPixel(x, y);
}
}
Now Reuben suggested to "read the lowest byte of each 32-bit pixel" - that would relate to my question about how to evaluate the pixel color. My last question in this regard: Do I get the lowest byte by simply doing this:
// Using the pixel from bitmap.getPixel(x,y)
int lowestByte = pixel & 0xff;
You can convert the image to monochrome 32bpp using a ColorMatrix.
Bitmap bmpMonochrome = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpMonochrome);
ColorMatrix ma = new ColorMatrix();
ma.setSaturation(0);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(ma));
canvas.drawBitmap(bmpSrc, 0, 0, paint);
That simplifies the color->monochrome conversion. Now you can just do a getPixels() and read the lowest byte of each 32-bit pixel. If it's <128 it's a 0, otherwise it's a 1.
Well I think its quite late now to reply to this thread but I was also working on this stuff sometimes back and decided to build my own library that will convert any jpg or png image to 1bpp .bmp. Most printers that require 1bpp images will support this image (tested on one of those :)).
Here you can find library as well as a test project that uses it to make a monochrome single channel image. Feel free to change it..:)
https://github.com/acdevs/1bpp-monochrome-android
Enjoy..!! :)
You should convert each pixel into HSV space and use the value to determine if the Pixel on the target image should be black or white:
Bitmap bwBitmap = Bitmap.createBitmap( bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565 );
float[] hsv = new float[ 3 ];
for( int col = 0; col < bitmap.getWidth(); col++ ) {
for( int row = 0; row < bitmap.getHeight(); row++ ) {
Color.colorToHSV( bitmap.getPixel( col, row ), hsv );
if( hsv[ 2 ] > 0.5f ) {
bwBitmap.setPixel( col, row, 0xffffffff );
} else {
bwBitmap.setPixel( col, row, 0xff000000 );
}
}
}
return bwBitmap;
Converting to monochrome with exact the same size as the original bitmap is not enough to print.
Printers can only print each "pixel" (dot) as monochrome because each spot of ink has only 1 color, so they must use much more dots than enough and adjust their size, density... to emulate the grayscale-like feel. This technique is called halftoning. You can see that printers often have resolution at least 600dpi, normally 1200-4800dpi, while display screen often tops at 200-300ppi.
So your monochrome bitmap should be at least 3 times the original resolution in each side.