How can I create a black and white image using array of BLACK and WHITE pixels ?
int pixels[]= {-1, -16777216,-16777216,-16777216....}
Color w = new Color(Color.WHITE.getRGB());
int wi = w.getRGB();
Color b = new Color(Color.BLACK.getRGB());
int bi = b.getRGB();
I was trying to use this code but the result was only few black pixels.
public static BufferedImage getImageFromArray(int[] pixels, int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = (WritableRaster) image.getData();
raster.setDataElements(0, 0, width, height, pixels);
return image;
}
Not sure what exactly you are tying to create, but here are some nice examples:
http://www.lac.inpe.br/JIPCookbook/1100-create-bw.jsp
Related
I have a byte array with type TYPE_4BYTE_ABGR, and I know its width and height, I want to change it to BufferedImage, any ideas?
The fastest way to create a BufferedImage from a byte array in TYPE_4BYTE_ABGR form, is to wrap the array in a DataBufferByte and create an interleaved WritableRaster from that. This will make sure there are no additional byte array allocations. Then create the BufferedImage from the raster, and a matching color model:
public static void main(String[] args) {
int width = 300;
int height = 200;
int samplesPerPixel = 4; // This is the *4BYTE* in TYPE_4BYTE_ABGR
int[] bandOffsets = {3, 2, 1, 0}; // This is the order (ABGR) part in TYPE_4BYTE_ABGR
byte[] abgrPixelData = new byte[width * height * samplesPerPixel];
DataBuffer buffer = new DataBufferByte(abgrPixelData, abgrPixelData.length);
WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, samplesPerPixel * width, samplesPerPixel, bandOffsets, null);
ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
System.out.println("image: " + image); // Should print: image: BufferedImage#<hash>: type = 6 ...
}
Note however, that this image will be "unmanaged" (some HW accelerations will be disabled), because you have direct access to the pixel array.
To avoid this, create the WritableRaster without the pixels, and copy the pixels into it. This will use twice as much memory, but will keep the image "managed" and thus possible better display performance:
// Skip creating the data buffer
WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, samplesPerPixel * width, samplesPerPixel, bandOffsets, null);
raster.setDataElements(0, 0, width, height, abgrPixelData);
// ...rest of code as above.
You could even do this (which might be more familiar):
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
WritableRaster raster = image.getRaster();
raster.setDataElements(0, 0, width, height, abgrPixelData);
Might not be very efficient, but a BufferedImage can be converted to another type this way:
public static BufferedImage convertToType(BufferedImage image, int type) {
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), type);
Graphics2D graphics = newImage.createGraphics();
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
return newImage;
}
About the method you want to be implemented, you would have to know the width or height of the image to convert a byte[] to a BufferedImage.
Edit:
One way is converting the byte[] to int[] (data type TYPE_INT_ARGB) and using setRGB:
int[] dst = new int[width * height];
for (int i = 0, j = 0; i < dst.length; i++) {
int a = src[j++] & 0xff;
int b = src[j++] & 0xff;
int g = src[j++] & 0xff;
int r = src[j++] & 0xff;
dst[i] = (a << 24) | (r << 16) | (g << 8) | b;
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, width, height, dst, 0, width);
I am working in javafx BufferedImage.
I have two BufferedImages 1)Grayscaleimage and 2)rgbimage.
i want to merged this two images in that rgb image want to transperanet with black color.
How can i achirve this?
my code snippet is:
if (depth == 24)
{
bufferedImgCFM = getPixelArrayToBmpByteArray(
fbData.getBuf(), width, height, depth);
}
else
{
bufferedImgBMode = getPixelArrayToBmpByteArray(
fbData.getBuf(), width, height, depth);
}
finalimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) finalimage.getGraphics();
if (bufferedImgBMode != null) {
g.drawImage(bufferedImgBMode, 0, 0, width, height, null);
}
if (bufferedImgCFM != null) {
g.drawImage(bufferedImgCFM, 0, 0, width, height, null);
}
Image image = SwingFXUtils.toFXImage(finalimage, null);
getPixelArrayToBmpByteArray method is:
private BufferedImage getPixelArrayToBmpByteArray(byte[] pixelData, int width,
int height, int depth) throws Exception {
int[] pixels = byteToInt(pixelData);
BufferedImage image = null;
if (depth == 8) {
image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
} else if (depth == 24) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//int[][] result = new int[height][width];
}
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0, 0, width, height, pixels);
image.setData(raster);
return image;
}
When i merged this two images second image that is rgb image will hide first image. i want to transperant rgb image with black.
Ok so I'm designing a maze related game and I am now handling the GUI.
Considering it will be a NxN dimension, I have to resize the images (content of the labyrinth) according to the screen size, so it will remain untouched regardless of the maze size.
I used this piece of code:
public static BufferedImage resizeImage(Image image, int width, int height) {
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
return bufferedImage;
}
There is an issue though. The new resized image is now black (completely black) even though it is correctly resized (dimensions wise).
What am I doing wrong? Thanks in advance.
Here's how you can add in the code to draw a scaled version of your image on the new bitmap that you created:
public static BufferedImage resizeImage(Image image, int width, int height) {
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
// Increase quality if needed at the expense of speed
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
AffineTransform scaleTransform = AffineTransform.getScaleInstance(
width / (double) image.getWidth(null), height / (double) image.getHeight(null));
g.drawImage(image, scaleTransform, null);
// Release resources
g.dispose();
return bufferedImage;
}
I am trying to use Tesseract OCR for scanning text in an image. However the background is dark and Tesseract is not able to scan thru for the text.
I need help with java code to clear the background gray color from the image.
I have tried below code, where i set the last parameter, Color, as "COLOR.WHITE" (suggested by someone on this forum but it did not help).
static public BufferedImage scaleImage(BufferedImage img, int width, int height,
Color background) {
int imgWidth = img.getWidth();
int imgHeight = img.getHeight();
if (imgWidth*height < imgHeight*width) {
width = imgWidth*height/imgHeight;
} else {
height = imgHeight*width/imgWidth;
}
BufferedImage newImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = newImage.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setBackground(background);
g.clearRect(0, 0, width, height);
g.drawImage(img, 0, 0, width, height, null);
} finally {
g.dispose();
}
return newImage;
}
Thanks.
I have this problem. I am using this code to rotate an image but the rotated image has black padding in its corners due to rotation.
How could I remove it?
public static BufferedImage rotate(BufferedImage img, int angle) {
rotate_checked = false;
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg =new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = dimg.createGraphics();
g.rotate(Math.toRadians(angle), w/2, h/2);
g.drawImage(img, null, 0, 0);
return dimg;
}
You need to create a transparent image:
BufferedImage buffer = gc.createCompatibleImage(height, width, Transparency.TRANSLUCENT);
where 'gc' is a Graphics2D object. You can also create one directly with new BufferedImage() of course, but this will give you the most efficient-to-use image for your particular graphics context.