Convert/display byte array to bmp/jpeg image - java

First of all, im asking this specific question, because i've already read many examples around this topic, but none of them really helped.
My problem is that i'd like to convert and display a jpg image. I don't have the original image, it is on a server. The data comes through a stream (socket) and it is given in a byte array. In this byte array, every 4 bytes represents information about a pixel. I managed to get the RGBs and convert/save them into a bmp image, however i couldn't find a working solution to make a jpg.
On the other hand, i have a BMP :) how can i display it in a JLabel or JPanel? I also read abou imageicon ImageIO etc. but it doesn't work for me. I use ScheduleExecutorSystem btw. Maybe that is the problem?
Here's the encoder code (Copyright (C) 2013 Philipp C. Heckel ):
public static void encodeToBitmap(byte[] srcBytes, OutputStream destStream)
throws IOException {
int imageWidth = 1024;
int imageHeight = 1080;
int imageBytes = imageWidth * imageHeight * 3;
int filesizeBytes = imageBytes + BMP_SIZE_HEADER;
byte[] header = BMP_HEADER.clone(); // Clone bitmap header template, and
// overwrite with fields
header = writeIntLE(header, BMP_OFFSET_FILESIZE_BYTES, filesizeBytes);
header = writeIntLE(header, BMP_OFFSET_IMAGE_WIDTH, imageWidth);
header = writeIntLE(header, BMP_OFFSET_IMAGE_HEIGHT, imageHeight);
header = writeIntLE(header, BMP_OFFSET_IMAGE_DATA_BYTES, 0);
header = writeIntLE(header, BMP_OFFSET_PAYLOAD_LENGTH,
(int) srcBytes.length);
// WRITE TO STREAM
// Add payload
destStream.write(header, 0, header.length);
for (int offset = imageBytes - imageWidth * 3; offset >= 0; offset -= imageWidth * 3) {
for (int i = 0; i < (imageWidth) * 3; i++) {
destStream.write(srcBytes[offset + i]);
}
}
destStream.close();
}
And the code, how i use it:
BitmapEncoder.encodeToBitmap(RGBvalues, new FileOutputStream("path to file"));
RGBvalues - bytes of 3, with the RGB values (i don't say!! :P )
image width and Height is fix for debug purpose
I have a JPanel and a JLabel within, and i added a default ImageIcon to the JLabel (a jpg image), and it works fine.
After i saved the image, i try :
ImageIcon icon = new ImageIcon("path to file");
pictureLabel.setIcon(icon);
It only makes the default disappear. Am i forgetting to set something?
Is this the fastest way, to do it?

The ImageIcon class only directly supports reading PNG, GIF and JPEG types. You need to use ImageIO to read your bitmap into a generic Image object (which is supported by ImageIcon) and then pass that to ImageIcon. Here's how I'd modify your code to work:
File imgFile = new File("path to file");
Image image = javax.imageio.ImageIO.read(imgFile);
ImageIcon icon = new ImageIcon(image);
pictureLabel.setIcon(icon);
Note that you can also use ImageIO to convert your bitmap to JPEG format.

Related

ImageIO.write(img,"jpg",pathtosave) JAVA not saving Image file to selected filepath

I'm using JVM 14.0.2 in VSCode IDE.
The purpose of the code is to change the original input image to grayscale image and save the new gray image to the desired location.
The code runs with no exceptions and i tried to print some progress lines(System.out.println("Saving completed...");), those lines printed throughout the program where i plugged in. However, when i go to the selected filepath to search for the saved GrayScale image, i do not see the new image in the directory.
I then tried the BlueJ IDE, and the gray image was saved. Can you check if it's VSCode developing environment issue or my code issue? or I need a different class/method to edit images in VSCode? Thanks for your help.Let me know if you need more details.
public class GrayImage {
public static void main(String args[]) throws IOException {
BufferedImage img = null;
// read image
try {
File f = new File("C:\\original.jpg");
img = ImageIO.read(f);
// get image width and height
int width = img.getWidth();
int height = img.getHeight();
BufferedImage grayimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// convert to grayscale
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Color color = new Color(img.getRGB(x, y));
int r = (int) color.getRed();
int g = (int) color.getBlue();
int b = (int) color.getGreen();
// calculate average
int avg = (r + g + b) / 3;
// replace RGB value with avg
Color newColor = new Color(avg, avg, avg, color.getAlpha());
grayimg.setRGB(x, y, newColor.getRGB());
}
}
// write image
System.out.println("Trying to write the new image...");
File newf = new File("H:\\gray.jpg");
ImageIO.write(grayimg, "jpg", newf);
System.out.println("Finished writing the new image...");
} catch (IOException e) {
System.out.println(e);
}
}// main() ends here
}
If I understand this problem correctly, the important lesson here is that ImageIO.write(...) returns a boolean, indicating whether it succeeded or not. You should handle situations where the value is false, even if there is no exception. For reference, see the API doc.
Something like:
if (!ImageIO.write(grayimg, "JPEG", newf)) {
System.err.println("Could not store image as JPEG: " + grayimg);
}
Now, for the reason your code does indeed work in one JRE and not in another, is probably related to the image being of type TYPE_INT_ARGB (ie. contains alpha channel). This used to work in Oracle JDK/JREs but support was removed:
Previously, the Oracle JDK used proprietary extensions to the widely used IJG JPEG library in providing optional color space support.
This was used to support PhotoYCC and images with an alpha component on both reading and writing. This optional support has been removed in Oracle JDK 11.
The fix is easy; as your source is a JPEG file, it probably does not contain an alpha component anyway, so you could change to a different type with no alpha. As you want a gray image, I believe the best match would be:
BufferedImage grayimg = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
But TYPE_INT_RGB or TYPE_3BYTE_BGR should work too, should you later run into the same problem with color images.

Convert byte[] to Image EmguCV without know width and height

I need convert byte array (byte[]) (from java BufferedImage) to EmguCV Image without to know the width and height.
byte[] bytes = [data];
Image<Gray, byte> image = new Image<Gray, byte>();
image.Bytes = bytes;
Can you help me?
I found this
[ http://www.emgu.com/forum/viewtopic.php?t=1057 ]
public Image<Bgr, Byte> byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Bitmap returnImage = Image.FromStream(ms);
return new Image<Bgr, byte>(returnImage);
// you probably need to clean up stuff like the bitmap and the stream...
}
On the emgu forum and retyped it to remove a variable-name typo. Assuming your byte array is a standard image , looks like you can load it into a standard image variable that will automatically figure out sizing. From there you could pass that image to the constructor of your emgu image.

How do I save a BufferedImage to a pgm file?

I looked through the internet but I could not find an answer. I have a pgm file which I use as a BufferedImage to do a convolution (I use JAI for that) but I am having trouble in saving it back to a pgm file.
So far I used following code to save:
JAI.create("filestore", newImage, outputFileName);
With that I get a pgm file but when I open the image IfranView tells me that it is a TIF file with incorrect extension. What do I need to change?
I appreciate any help! Please provide code examples if possible. Thanks everyone.
Kind regards,
Staniel
BufferedImage bufferedImage = ImageIO.read(new File("input file directory...image.png"));
ImageIO.write(bufferedImage, "pgm", new File("output file directory.....image.pgm"));
This should take a buffered image (jpeg, png...etc) and convert it properly to a pgm.
EDIT: The JAI Plugin which allows for .pgm files to be used with ImageIO can be found at http://www.oracle.com/technetwork/java/index.html
Here's an example I found. Not tested.
// Create the OutputStream.
OutputStream out = new FileOutputStream(fileToWriteTo);
// Create the ParameterBlock.
PNMEncodeParam param = new PNMEncodeParam();
param.setRaw(true.equals("raw"));
//Create the PNM image encoder.
ImageEncoder encoder = ImageCodec.createImageEncoder("PNM", out, param);
See Writing PNM Files.
I found the answer. I already added the external JAI imageio to my library.
ImageIO.write(bufferedImage, "pnm", new File("output file directory.....image.pgm"));
Instead of "pgm" it should say "pnm". The new file will automatically have the pgm extension.
Maybe late, but I just wrote one. A simple PGM writer taking a 2d-double array with values in range [0.0,1.0].
public static void WritePGM(string fileName, double[,] bitmap)
{
var width = bitmap.GetLength(0);
var height = bitmap.GetLength(1);
var header = "P5\n" + width + " " + height + "\n255\n";
var writer = new BinaryWriter(new FileStream(fileName, FileMode.Create));
writer.Write(System.Text.Encoding.ASCII.GetBytes(header));
for (var j = 0; j < height; j++)
{
for (var i = width-1; i >= 0; i--)
{
var c = (byte)(System.Math.Max(System.Math.Min(1.0, bitmap[i, j]), 0.0) * 255.0);
writer.Write(c);
}
}
writer.Close();
}

About java ByteArrayOutputStream class

BufferedImage bufferedImage = ImageIO.read(new File("/...icon.jpg"));
// this writes the bufferedImage into a byte array called resultingBytes
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", byteArrayOut);
byte[] resultingBytes = byteArrayOut.toByteArray();
I use the above code to get a JEPG image as a byte array. I want to know what exactly is in this byte array. Does this array contain any file header information or just pixel values? And for example, if I want to reverse this image's color, what is a good way to do so?
Thanks so much!
It's a complete JPEG file, in memory.
EDIT: If you want to manipulate pixel data as an array, you may find Raster more helpful:
E.g.:
Raster raster = bufferedImage.getData();
You can then call one of the Raster.getPixels methods.
The ByteArrayOutputStream contains whatever you wrote to it. Nothing more, nothing less. So your question is really about ImageIO.write(). Which writes out an encoding of an image according to the encoding type you supply. Which was JPEG.
Here is how you read real pixel values. The JPEG information is much harder to do anything with!
public static void main(String... args) throws IOException {
String u = "http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png";
BufferedImage old = ImageIO.read(new URL(u));
BufferedImage inverted = new BufferedImage(old.getWidth(),
old.getHeight(),
BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < old.getHeight(); y++) {
for (int x = 0; x < old.getWidth(); x++) {
Color oldColor = new Color(old.getRGB(x, y));
// reverse all but the alpha channel
Color invertedColor = new Color(255 - oldColor.getRed(),
255 - oldColor.getGreen(),
255 - oldColor.getBlue());
inverted.setRGB(x, y, invertedColor.getRGB());
}
}
ImageIO.write(inverted, "png", new File("test.png"));
}

Write to 16 bit BufferedImage TYPE_USHORT_GRAY

I'm trying to write 16 bit grayscale imagedata to a png using BufferedImage.TYPE_USHORT_GRAY. Normally I write to an image like so:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
then:
image.setRGB(x,y,Color.getRGB);
to set the pixels, and finally:
ImageIO.write(image, "png", new File(path + ".png"));
to write to a png image.
But now I have this as image:
BufferedImage imageGray = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
How do I go about saving pixels to that format? Using setRGB() with a 16 bit integer doesn't seem to work, when I open the saved png file I see a lot of banding happening.
I tried saving a simple gradient from 0 to 65535 and then using the setRGB() on the grayscale image, and checked the results in Photoshop. I can see the image consists of smaller gradients every 256 rows. I'm guessing either setRGB() or imageIO doesn't work as I would like it to.
Are there workarounds for this? Does imageIO even support the BufferedImage.TYPE_USHORT_GRAY format? Or can it only save 8 bit data? And if it can save 16bit data, how would I go about saving pixel data, preferably in a way like setRGB() works (so for a certain x,y coordinate)?
As pst already commented below my question:
Try using the Raster directly?
Accessing the Raster directly solved the problem :
BufferedImage bi = BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY)
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Short s = shortData[x][y]
bi.getRaster().setDataElements(x, y, Short[] {s})
}
}
From BufferedImage you can read
public static final int TYPE_USHORT_GRAY
Represents an unsigned short grayscale image, non-indexed). This image has a ComponentColorModel with a CS_GRAY ColorSpace.
So try instantiating your own ColorSpace with the CS_GRAY type (ColorSpace.getInstance(ColorSpace.CS_GRAY) should do it I suppose). This object has a method called fromRGB which you should be able to use...
You probably need to widen the signed 16bit shorts to ints and remove the sign:
int ushort = (int)(shortData[x][y]) & 0xFFFF;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
short[] dataArray = ((DataBufferUShort)image.getRaster().getDataBuffer()).getData();
dataArray[y*width+x] = color;
ImageIO.write(image, "png", new File(path + ".png"));

Categories