java how to convert a int[][] to an BufferedImage - java

i have an 2D integer array, which i get from the BufferedImage method "getRGB()".
when i try to convert the 2D integer array back to BufferdImage, i get only a black picture.
This method
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
int pixel=matrix[i][j];
System.out.println("The pixel in Matrix: "+pixel);
bufferedImage.setRGB(i, j, pixel);
System.out.println("The pixel in BufferedImage: "+bufferedImage.getRGB(i, j));
}
}
give this output:
The pixel in Matrix: 0
The pixel in BufferedImage: -16777216
The pixel in Matrix: 721420288
The pixel in BufferedImage: -16777216
The pixel in Matrix: 738197504
The pixel in BufferedImage: -16777216
The pixel in Matrix: 520093696
The pixel in BufferedImage: -16777216
The pixel in Matrix: 503316480
The pixel in BufferedImage: -16777216
why is every Pixel "-16777216"?
Thanks!
UPDATE
the method which returns the integer Matrix
public int[][] getMatrixOfImage(BufferedImage bufferedImage) {
int width = bufferedImage.getWidth(null);
int height = bufferedImage.getHeight(null);
int[][] pixels = new int[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
pixels[i][j] = bufferedImage.getRGB(i, j);
}
}
return pixels;
}

All your pixels seem to be black with different alpha values. You have to use TYPE_INT_ARGB to not lose the alpha channel.

If you are using TYPE_INT_RGB, you can do it this way:
BufferedImage.getRaster().setPixels(xCord, YCord, Width, Height, IntArray);

Related

flat picture (1;16) divide into equal parts (4;4)

I have a Bufferedimage that is 1px high and eg wide. I need to create another Bufferedimage that will be in 1:1 resolution. I know what can be done in this case by calculating the square root of the width, but what if the result is not an integer? I have a method for creating a matrix (a list of each pixel in turn) from the width and height, so there will be no problems. At the end, if there are empty pixels, I can make them transparent, so not necessarily a completely filled square. I just need to get the dimensions of the square, having the width of the original photo, I will fill in the pixels myself.
my code:
public static BufferedImage encode(String str){
byte[] bytes = str.getBytes();
BufferedImage img = new BufferedImage(bytes.length, 1, BufferedImage.TYPE_INT_ARGB);
for (int i = 0; i < bytes.length; i++) {
img.setRGB(i, 0,encode(bytes[i]).getRGB() );
}
return img;
}
public static Color encode(byte byt){
return new Color(byt+128,byt+128,byt+128);
}
public static BufferedImage encode(String str){
byte[] bytes = str.getBytes();
int w = bytes.length;
int width = (int) Math.ceil(Math.sqrt(w));
BufferedImage img = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
for (int index = 0; index < bytes.length; index++) {
img.setRGB(index%width, (int)Math.floor(index/(float)width),encode(bytes[index]).getRGB() );
}
return img;
}
code update. I just calculate the square root, and if the number is not an integer, then I round up to a larger one and as a result I get the height of the image, because the image has a 1:1 format, we immediately got the width. Then I just fill in the formula:
x = byteIndex % imageWidth
y = floor(byteIndex / imageHeight)

Getting the color of a pixel [duplicate]

Given an image file, say of PNG format, how to I get an array of int [r,g,b,a] representing the pixel located at row i, column j?
So far I am starting here:
private static int[][][] getPixels(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
int[][][] result = new int[height][width][4];
// SOLUTION GOES HERE....
}
Thanks in advance!
You need to get the packed pixel value as an int, you can then use Color(int, boolean) to build a color object from which you can extract the RGBA values, for example...
private static int[][][] getPixels(BufferedImage image) {
int[][][] result = new int[height][width][4];
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
Color c = new Color(image.getRGB(i, j), true);
result[y][x][0] = c.getRed();
result[y][x][1] = c.getGreen();
result[y][x][2] = c.getBlue();
result[y][x][3] = c.getAlpha();
}
}
}
It's not the most efficient method, but it is one of the simplest
BufferedImages have a method called getRGB(int x, int y) which returns an int where each byte is the components of the pixel (alpha, red, green and blue). If you dont want to do the bitwise operators yourself you can use Colors.getRed/Green/Blue methods by creating a new instance of Java.awt.Color with the int from getRGB.
You can do this in a loop to fill the three-dimensional array.
This is my code for this problem:
File f = new File(filePath);//image path with image name like "lena.jpg"
img = ImageIO.read(f);
if (img==null) //if img null return
return;
//3d array [x][y][a,r,g,b]
int [][][]pixel3DArray= new int[img.getWidth()][img.getHeight()][4];
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
int px = img.getRGB(x,y); //get pixel on x,y location
//get alpha;
pixel3DArray[x][y][0] =(px >> 24)& 0xff; //shift number and mask
//get red
pixel3DArray[x][y][1] =(px >> 16)& 0xff;
//get green
pixel3DArray[x][y][2] =(px >> 8)& 0xff;
//get blue
pixel3DArray[x][y][3] =(px >> 0)& 0xff;
}
}

Reading a grayscale image in java

As we read a RGB image , do the shifting operations to get the R, G and B matrices separately ...Is it possible to read a gray scale image(JPEG) and directly manipulate its pixel values.And then rewrite the image ?
Ultimately I have to do the DCT operation on the gray scale image.
The code below will read the grayscale image to simple two dimensional array:
File file = new File("path/to/file");
BufferedImage img = ImageIO.read(file);
int width = img.getWidth();
int height = img.getHeight();
int[][] imgArr = new int[width][height];
Raster raster = img.getData();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
imgArr[i][j] = raster.getSample(i, j, 0);
}
}
Note: The raster.getSample(...) method takes 3 arguments: x - the X coordinate of the pixel location, y - the Y coordinate of the pixel location, b - the band to return. In case of the grayscale images we should/may get only the 0 band.

How to create valid image from RGB pixel values using java

I have a 2D array which contains the RGB values. I need to create a valid image from these pixel values and save it. I have given the 2D array below. I wanted to implement this part in my project, so please help me with this. Thank you.
int[] pixels = new int[imageSize * 3];
int k = 0;
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
if(k<imageSize*3)
{
pixels[k] = r[i][j];
pixels[k+1] = g[i][j];
pixels[k+2] = b[i][j];
}
k = k+3;
}
}
You can build a BufferedImage of type BufferedImage.TYPE_INT_RGB. This type represents a color as an int where:
3rd byte (16-23) is red,
2nd byte (8-15) is green and
1st byte (7-0) is blue.
You can get the pixel RGB value as follows:
int rgb = red;
rgb = (rgb << 8) + green;
rgb = (rgb << 8) + blue;
Example (Ideone full example code):
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = r[y][x];
rgb = (rgb << 8) + g[y][x];
rgb = (rgb << 8) + b[y][x];
image.setRGB(x, y, rgb);
}
}
File outputFile = new File("/output.bmp");
ImageIO.write(image, "bmp", outputFile);

BufferedImage - Gray Scale

Given an image that is in grayscale, how would I get the pixel values of the grayscale at that location?
This outputs temp as -16777216 (black) all the time.
public void testMethod()
{
int width = imgMazeImage.getWidth();
int height = imgMazeImage.getHeight();
//Assign class variable as a new image with RGB formatting
imgMazeImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for(int i=0; i < width; i ++){
for(int j=0; j < height; j++)
{
//Grab and set the colors one-by-one
inttemp = imgMazeImage.getRGB(j, i);
System.out.println(temp);
}
}
}
you are creating a new blank image and assigning it to your class variable:
imgMazeImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
the pixels are created with a default value and its logical that they all have the same color (black) at the stage when you are printing them since you did not manipulate the color in any pixel yet.
also, your code might fail if the width is not equal to the height. according to your for loops, i runs along width and j runs along height. therefore, you should change
int temp = imgMazeImage.getRGB(j, i);
to
int temp = imgMazeImage.getRGB(i, j);

Categories

Resources