Converting array of int to Bitmap on Android - java

I have an MxN array of ints representing colors (say RGBA format, but that is easily changeable). I would like to convert them to an MxN Bitmap or something else (such as an OpenGL texture) that I can render to the screen. Is there a fast way to do this? Looping through the array and drawing them to the canvas is far too slow.

Try this, it will give you the bitmap:
// You are using RGBA that's why Config is ARGB.8888
bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
// vector is your int[] of ARGB
bitmap.copyPixelsFromBuffer(IntBuffer.wrap(vector));
Or you can generate IntBuffer from the following native method:
private IntBuffer makeBuffer(int[] src, int n) {
IntBuffer dst = IntBuffer.allocate(n*n);
for (int i = 0; i < n; i++) {
dst.put(src[i]);
}
dst.rewind();
return dst;
}

Why not use Bitmap.setPixel? It's even API level 1:
int[] array = your array of pixels here...
int width = width of "array"...
int height = height of "array"...
// Create bitmap
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// Set the pixels
bitmap.setPixels(array, 0, width, 0, 0, width, height);
You can play with offset/stride/x/y as needed.
No loops. No additional allocations.

Yeah, sounds like you have all the info you need. If M is the width and N is the height, you can create a new bitmap with Bitmap.createBitmap, and you can fill in the ARGB values with the setPixels method which takes an int array.
Bitmap.createBitmap
Bitmap.setPixels

Related

Convert 8bit Grayscale image byte array to a BufferedImage

I have a byte array containing data of the raw grayscale 8bit image, which I need to convert to a BufferedImage. I've tried doing:
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
However, the resulting image object is null which means I'm doing something wrong here.
What's the correct way of making such a conversion?
There are two good ways to do this, depending on your use case.
Either create a new, gray image, and copy the data into it. This will keep the image "managed", which may lead to better rendering performance (ie. on screen). But it will need twice as much memory, and copy the data from your input to the image.
The other, is to create the gray image directly "around" your existing pixel data. This will be faster, and use almost no extra heap, as it avoids copying the pixel data. But the image will not be managed (as the backing array is exposed and mutable).
Both options are demonstrated below:
int w = 640;
int h = 480;
byte[] imageBytes = new byte[w * h];
// 1 Keeps the image "managed" at the expense of twice the memory + a large array copy
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setDataElements(0, 0, w, h, imageBytes);
System.out.println("image: " + image);
// 2 Faster, and uses less memory, but will make the image "unmanaged"
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
WritableRaster raster = Raster.createInterleavedRaster(new DataBufferByte(imageBytes, imageBytes.length), w, h, w, 1, new int[]{0}, null);
BufferedImage image2 = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
System.out.println("image2: " + image2);
If the image data isn't in linear gray color space, one could use an IndexColorModel to map the input into whatever range you want:
// Alternate, using IndexColorModel, if your input isn't in linear gray color space
int[] cmap = new int[256]; // TODO: Add ARGB packed colors here...
IndexColorModel icm = new IndexColorModel(8, 256, cmap, 0, false, -1, DataBuffer.TYPE_BYTE);
// As 1
BufferedImage image3 = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm);
image3.getRaster().setDataElements(0, 0, w, h, imageBytes);
System.out.println("image3: " + image3);
// As 2
BufferedImage image4 = new BufferedImage(icm, raster, cm.isAlphaPremultiplied(), null);
System.out.println("image4: " + image4);
I've managed to did the conversion for the 640x480 resolution the following way:
BufferedImage image = new BufferedImage(640,480,BufferedImage.TYPE_BYTE_INDEXED);
int i = 0;
for(int y = 0; y < 480; y++)
{
for(int x = 0; x < 640; x++)
{
int g = imageBytes[i++] & 0xFF;
image.setRGB(x,y,new Color(g,g,g).getRGB());
}
}
EDIT: removed useless code (thanks to Marco13)
Java
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
image.getRaster().setDataElements(0, 0, width, height, array));
Kotlin
val image = BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY)
image.raster.setDataElements(0, 0, width, height, byteArray )

Android Bitmap - Crop a circle into semicircle

I'm trying to cut a circle into semicircles using android canvas. The circle is loaded using Bitmap class.
Here's the example:
I've been looking for any solution, especially the ones which enable you to crop a bitmap using coordinates, but to no avail.
Any help is appreciated, thanks before..
I had the same challenge before and I solved it in a simple way, The main idea is simple! Use a Bitmap mask, Fill the pixel you want to save (in this case a pie) with the highest integer value (0xFFFFFFFF), So you can use a bitwiseAND to gain the result color, Other pixels of the mask Bitmap will be a transparent black color (0x00000000), When you're done with the mask, Create the result Bitmap and fill the pixels as the method below does:
public Bitmap applyPieMask(Bitmap src, float startAngle, float sweepAngle) {
int width = src.getWidth();
int height = src.getHeight();
//create bitmap mask with the same dimension of the src bitmap
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mask);
canvas.drawColor(0x00000000);//fill mask bitmap with transparent black!
//init mask paint
Paint maskPaint = new Paint();
maskPaint.setColor(0xFFFFFFFF);//pick highest value for bitwise AND operation
maskPaint.setAntiAlias(true);
//choose entire bitmap as a rect
RectF rect = new RectF(0, 0, width, height);
canvas.drawArc(rect, startAngle, sweepAngle, true, maskPaint);//mask the pie
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
//combine src color and mask to gain the result color
int color = mask.getPixel(i, j) & src.getPixel(i, j);
result.setPixel(i, j, color);
}
}
return result;
}
And here we go ...
public void doIt(View view) {
ImageView imageView = (ImageView) findViewById(R.id.iv);
Bitmap src = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(src);
canvas.drawColor(Color.BLUE);//fill src bitmap with blue color
imageView.setImageBitmap(applyPieMask(src, -90, 60));
}
Hope you find it helpful

Java Convert PPM Byte array to JPG image

Currently working on Image manipulation in Java
I have the byte array(PPM) of size 921600 (640*480*3)
byte[] image; // PPM
BufferedImage image = ImageIO.read(new ByteArrayInputStream(image));
image is null.
Tried with ImageMagic and JAI libraries. But it does not help me.
Is it possible to get the RGB components from byte array and convert it to JPG file.
Any help is appreciated.
Below is the Code (which will convert PPM(byte array to Buffered image and you can save buffered image to the file)
// Method Call
BufferedImage image = ppm(width, height, 255, byte[]);
//Method Definition
static public BufferedImage ppm(int width, int height, int maxcolval, byte[] data){
if(maxcolval<256){
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r,g,b,k=0,pixel;
if(maxcolval==255){ // don't scale
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+3)<data.length);x++){
r=data[k++] & 0xFF;
g=data[k++] & 0xFF;
b=data[k++] & 0xFF;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
}
else{
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+3)<data.length);x++){
r=data[k++] & 0xFF;r=((r*255)+(maxcolval>>1))/maxcolval; // scale to 0..255 range
g=data[k++] & 0xFF;g=((g*255)+(maxcolval>>1))/maxcolval;
b=data[k++] & 0xFF;b=((b*255)+(maxcolval>>1))/maxcolval;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
}
return image;
}
else{
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r,g,b,k=0,pixel;
for(int y=0;y<height;y++){
for(int x=0;(x<width)&&((k+6)<data.length);x++){
r=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);r=((r*255)+(maxcolval>>1))/maxcolval; // scale to 0..255 range
g=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);g=((g*255)+(maxcolval>>1))/maxcolval;
b=(data[k++] & 0xFF)|((data[k++] & 0xFF)<<8);b=((b*255)+(maxcolval>>1))/maxcolval;
pixel=0xFF000000+(r<<16)+(g<<8)+b;
image.setRGB(x,y,pixel);
}
}
return image;
}
}
You can use a WritableRaster to set the pixels in the image for you:
For a grayscale image, you will have a byte array like this:
byte[] arr = new byte[width * height];
To make an image, use:
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
img.getRaster().setDataElements(0, 0, width, height, arr);
For a color image, you will have an array like this:
byte[] arr = new byte[width * height * 3];
So, to make an image, use:
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
img.getRaster().setDataElements(0, 0, width, height, arr);
You may need to mess this the TYPE in the first line. See here to choose which type your image is.

Int Pixel Array To Image

I am trying to take an int array of pixels from a Bitmap on Android and use the int array to make a buffered image. But I'm having some problems. I am able to get the bitmap from android with no problem:
Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
b = Bitmap.createScaledBitmap(b, (int)(b.getWidth() * .3125), (int)(b.getHeight() * .44444444444), false);
int[] arr = new int[b.getHeight() * b.getWidth()];
b.getPixels(arr, 0, b.getWidth(), 0, 0, b.getWidth(), b.getHeight());
I scale the bitmap down and then get the pixels and put them into the int array. Then I send them over a network to the PC where I try to recreate it to a BufferedImage (networking isn't important). Here is the code I use to convert the pixel array to a BufferedImage.
int width = 600;
int height = 479;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bi.setRGB(0, 0, width, height, arr, 0, width);
g2d.drawImage(bi, 10, 50, null);
The variable "arr" is the byte array of pixels. Converting to a BufferedImage gives no error and shows the following image:
Obviously this isn't the picture I am trying to get. When I print out 5 of the pixels in the array (just for testing) I get the following and this is the format of the pixels:
-15528956
-15200766
-13558523
-11718123
-12243954
-13294582

How to use TYPE_BYTE_GRAY to efficiently create a grayscale bufferedimage using AWT

I need to create a grayscale image from data in an nio ShortBuffer. I have a function that maps the data in the ShortBuffer to unsigned byte but is in an int (easily changed). The method I found uses an RGB plus transparency color model and appears to be quite inefficent. i have not been able to see how to apply the TYPE_BYTE_GRAY and modify the code. i'm new to Java. Here's my code:
public void paintComponent(Graphics g) {
final BufferedImage image;
int[] iArray = {0, 0, 0, 255}; // pixel
image = (BufferedImage) createImage(WIDTH, HEIGHT);
WritableRaster raster = image.getRaster();
sBuf.rewind(); // nio ShortBuffer
for (int row = 0; row < HEIGHT; row++) {
for (int col = 0; col < WIDTH; col++) {
int v = stats.mapPix(sBuf.get()); // map short to byte
iArray[0] = v; // RGBT
iArray[1] = v;
iArray[2] = v;
raster.setPixel(col, row, iArray);
}
}
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
TIA
Nate
Insted of using a ColorConvertOp, you could simply create a new gray scale BufferedImage and paint the original colored image onto it:
public static BufferedImage convertToGrayScale(BufferedImage image) {
BufferedImage result = new BufferedImage(
image.getWidth(),
image.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
Graphics g = result.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return result;
}
This should perform significantly faster and give better results than using the filter() method.
A great tuturial (including instruction on how to use a GrayFilter) can be found here: http://www.tutorialized.com/tutorial/Convert-a-Color-Image-to-a-Gray-Scale-Image-in-Java/33347
One approach would be to create the BufferedImage by writing to the raster as you are doing now. Once you have the BufferedImage, you can convert it to TYPE_BYTE_GRAY using the filter() method of ColorConvertOp, as shown in this example.

Categories

Resources