Java - Saving byte[] into an image file - java

This post is a follow up to :
ImageIO.read can't read ByteArrayInputStream (image processing)
Similar to the OP, I am getting a null pointer whenever I try to read from my ByteArrayInputStream (as it should, as explained by the top answer). Noticing this, I have implemented the code from the #haraldK 's answer from the post above in order to correct this issue, but I have run into another problem. I have the following code:
byte[] imageInByteArr = ...
// convert byte array back to BufferedImage
int width = 1085;
int height = 696;
BufferedImage convertedGrayScale = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
convertedGrayScale.getRaster().setDataElements(0, 0, width, height, imageInByteArr );
try {
ImageIO.write(convertedGrayScale, "jpg", new File("C:\\test.jpg"));
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
Upon execution, I run into a java.lang.ArrayIndexOutOfBoundsException: null error on the line right before the try/catch block. My first thought was that this null pointer was arising for not having a file in my C drive called test.jpg. I adjusted to fix that worry, yet I am still getting the same null pointer issue at convertedGrayScale.getRaster().setDataElements(0, 0, width, height, imageInByteArr );. Why is this happening?
On another note, aside from writing the file uining ImageIO, is there ANY other way for me to convert the byte[] into a visual representation of an image? I have tried to just print the array onto a file and saving it as a '.jpg', but the file will not open. Any suggestions will help. To summarize, I am looking to convert a byte[] into an image and save it OR render it onto a browser. Whichever is easier/doable.

it appears that your imageInByteArr is too short. I was able to get the same error you get from this
public static void main(String[] args) {
int width = 1085;
int height = 696;
byte[] imageInByteArr = new byte[width ];
BufferedImage convertedGrayScale = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
convertedGrayScale.getRaster().setDataElements(0, 0, width, height, imageInByteArr);
}
when using width*height for size of imageInByteArr or anything bigger i get no error, but when it's smaller than the data you are trying to update it throws the exception.

Related

GLFW_PLATFORM_ERROR when using glfwSetWindowIcon()

I currently coding a Project using LWJGL 3. Since GLFW 3.2 its possible to add a window icon. I used it like you can see below, but I keep getting the error, and I have no clue why. What am I doing wrong?
What I have tried already:
I have made sure that the file is present in "res/img/icon.png".
The PNGLoader (Found here) works, as I am using textures and it loads them without any problems.
Code:
PNGDecoder dec = null;
try {
dec = new PNGDecoder(new FileInputStream("res/img/icon.png"));
int width = dec.getWidth();
int height = dec.getHeight();
ByteBuffer buf = BufferUtils.createByteBuffer(width * height * 4);
dec.decode(buf, width * 4, PNGDecoder.Format.RGBA);
buf.flip();
Buffer images = new Buffer(buf);
GLFW.glfwSetWindowIcon(window, images);
} catch (IOException e) {
e.printStackTrace();
}
Here's the error that I get:
[LWJGL] GLFW_PLATFORM_ERROR error
Description : Win32: Failed to create RGBA bitmap
Stacktrace :
org.lwjgl.glfw.GLFW.nglfwSetWindowIcon(GLFW.java:1802)
org.lwjgl.glfw.GLFW.glfwSetWindowIcon(GLFW.java:1829)
ch.norelect.emulator.designer.Designer.initialize(Designer.java:81)
ch.norelect.emulator.designer.Designer.start(Designer.java:48)
ch.norelect.emulator.Main.main(Main.java:27)
You are passing the raw pixel data as a GLFWImage.Buffer, but GLFW needs the width and height as well. GLFWImage.Buffer is just a convenience class for putting several GLFWImages next to each other, which each hold properties about an image.
GLFWImage image = GLFWImage.malloc();
image.set(width, height, buf);
GLFWImage.Buffer images = GLFWImage.malloc(1);
images.put(0, image);
glfwSetWindowIcon(window, images);
images.free();
image.free();

Convert and display image from byte array

I'm making a program, which gets data about an image in byte array from a server. I'm converting this data into 24bit BMP format (whether its jpeg, png, bmp or 8-24-32bpp). First, I'm saving it to my HD, and then I'm loading it into a JLabel's Icon. Works perfectly, though there are some cases in which I get the following exception:
java.io.EOFException at
javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:353) at
com.sun.imageio.plugins.bmp.BMPImageReader.read24Bit(BMPImageReader.java:1188) at
com.sun.imageio.plugins.bmp.BMPImageReader.read(BMPImageReader.java:843) at
javax.imageio.ImageIO.read(ImageIO.java:1448) at
javax.imageio.ImageIO.read(ImageIO.java:1308)
For this line (the second)
File imgFile = new File("d:/image.bmp");
BufferedImage image = ImageIO.read(imgFile);
In these cases:
the image does not load into the JLabel, but it can be found on my HD
the conversion is not proper, because something "slips"
the picture is like when you use italics in a word document
First, i thought maybe the bpp is the problem, then i thought that maybe the pictures are too large, but i have cases it works and cases it doesn't for both suggestions. I'm a little stuck here, and would be glad for ideas.
the picture is like .. when You use italics in a word document
Think I finally got what this bullet item meant now.. ;-)
Speculative answer, but here goes:
If the image you write looks "skewed", it's probably due to missing padding for each column as the BMP format specifies (or incorrect width field in the BMP header). I assume then, that the images you get EOF exceptions for, is where the width is not a multiple of 4.
Try to write the BMPs using ImageIO to see if that helps:
private static BufferedImage createRGBImage(byte[] bytes, int width, int height) {
DataBufferByte buffer = new DataBufferByte(bytes, bytes.length);
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8, 8, 8}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, Raster.createInterleavedRaster(buffer, width, height, width * 3, 3, new int[]{0, 1, 2}, null), false, null);
}
...
byte[] bytes = ...; // Your image bytes
OutputStream stream = ...; // Your output
BufferedImage image = createRGBImage(bytes, width, height);
try {
ImageIO.write(image, "BMP", stream);
}
finally {
stream.close();
}
Call it by class name, liek ClassName.byteArrayToImage(byte):
public static BufferedImage byteArrayToImage(byte[] bytes){
BufferedImage bufferedImage=null;
try {
InputStream inputStream = new ByteArrayInputStream(bytes);
bufferedImage = ImageIO.read(inputStream);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
return bufferedImage;
}
You can use this code to convert the output image to a byte Array
Blob b = rs.getBlob(2);
byte barr[] = new byte[(int)b.length()]; //create empty array
barr = b.getBytes(1,(int)b.length());
FileOutputStream fout = new FileOutputStream("D:\\sonoo.jpg");
fout.write(barr);

Canvas bitmap to byte array and reading it back trouble

I have a Canvas I draw on, I'm trying to take the bitmap out, convert it to a byte array and save it serialized into a file. then later open, deserialize, and apply the bitmap back to the canvas. In the code below everything seems to work well except that when applying the bitmap to canvas nothing appears. can someone please show me where I'm going wrong.
public byte[] getCanvasData(){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
mBitmap.compress(CompressFormat.PNG, 0, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
public void setCanvasData(byte[] canvasData, int w, int h){
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mBitmap.eraseColor(0x00000000);
mCanvas = new Canvas(mBitmap);
mCanvas.drawBitmap(BitmapFactory.decodeByteArray(canvasData , 0, canvasData.length).copy(Bitmap.Config.ARGB_8888, true), 0, 0, null);
}
ADDED SOME EXTRA CODE TO POSSIBLY HELP A LITTLE
public void readInSerialisable() throws IOException
{
FileInputStream fileIn = new FileInputStream("/sdcard/theBKup.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
try
{
BookData book = (BookData) in.readObject();
pages.clear();
canvasContainer.removeAllViews();
for (int i = 0; i < book.getBook().size(); i++){
Log.d("CREATION", "LOADING PAGE " + i);
pages.add(new Canvas2(context, book.getPageAt(i), canvasContainer.getWidth(), canvasContainer.getHeight()));
}
canvasContainer.addView(pages.get(page), new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT, AbsoluteLayout.LayoutParams.FILL_PARENT, 0, 0));
updatePagination();
Log.d("CREATION", "Updated Pagination");
}
catch (Exception exc)
{
System.out.println("didnt work");
exc.printStackTrace();
}
}
BookData - Serializable class containing all my data, simple gets/sets in there
onDraw Method
#Override
protected void onDraw(Canvas canvas) {
Log.d("DRAWING", "WE ARE DRAWING");
canvas.drawColor(0x00AAAAAA); //MAKE CANVAS TRANSPARENT
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
I would do the following 2 tests.
Log some of the byte stream to make sure that it was loaded correctly. Something like Log.v(canvasData[0]+canvasData[1]);, or put a break point there, or something just to make sure the data is correct.
Draw a bitmap that you know is valid, using the same code, and see if it appears correctly.
I'm not sure exactly what's going on, but I strongly suspect one of the following.
The byte stream is not being read in correctly.
The bitmap is not being updated to the screen, or is using a trivially small size.
In the event that your byte stream data has something, then you will want to take a look at the Canvas documentation. Specifically, look at the following bit.
In order to see a Canvas, it has to be put on to a view. Once it is on a view, the onDraw() command must be called for it to be visible. I would make sure that you are in fact doing an onDraw(), and that the Canvas is associated with the View correctly. If you are using an onDraw() already, please post the bits of code associated with it.

ImageIO not able to write a JPEG file

I have a BufferedImage I'm trying to write to a jpeg file, but my Java program throws an exception. I'm able to successfully save the same buffer to a gif and png. I've tried looking around on Google for solutions, but to no avail.
Code:
File outputfile = new File("tiles/" + row + ":" + col + ".jpg");
try {
ImageIO.write(mapBufferTiles[row][col], "jpg", outputfile);
} catch (IOException e) {
outputfile.delete();
throw new RuntimeException(e);
}
Exception:
Exception in thread "main" java.lang.RuntimeException: javax.imageio.IIOException: Invalid argument to native writeImage
at MapServer.initMapBuffer(MapServer.java:90)
at MapServer.<init>(MapServer.java:24)
at MapServer.main(MapServer.java:118)
Caused by: javax.imageio.IIOException: Invalid argument to native writeImage
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1055)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:357)
at javax.imageio.ImageWriter.write(ImageWriter.java:615)
at javax.imageio.ImageIO.doWrite(ImageIO.java:1602)
at javax.imageio.ImageIO.write(ImageIO.java:1526)
at MapServer.initMapBuffer(MapServer.java:87)
... 2 more
OpenJDK does not have a native JPEG encoder, try using Sun's JDK, or using a library (such as JAI
AFAIK, regarding the "pinkish tint", Java saves the JPEG as ARGB (still with transparency information). Most viewers, when opening, assume the four channels must correspond to a CMYK (not ARGB) and thus the red tint.
If you import the image back to Java, the transparency is still there, though.
I had the same issue in OpenJDK 7 and I managed to get around this exception by using an imageType of TYPE_3BYTE_BGR instead of TYPE_4BYTE_ABGR using the same OpenJDK.
2019 answer: Make sure your BufferedImage does not have alpha transparency. JPEG does not support alpha, so if your image has alpha then ImageIO cannot write it to JPEG.
Use the following code to ensure your image does not have alpha transparancy:
static BufferedImage ensureOpaque(BufferedImage bi) {
if (bi.getTransparency() == BufferedImage.OPAQUE)
return bi;
int w = bi.getWidth();
int h = bi.getHeight();
int[] pixels = new int[w * h];
bi.getRGB(0, 0, w, h, pixels, 0, w);
BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
bi2.setRGB(0, 0, w, h, pixels, 0, w);
return bi2;
}
Here is some code to illustrate #Thunder idea to change the image type to TYPE_3BYTE_BGR
try {
BufferedImage input = ImageIO.read(new File("input.png"));
System.out.println("input image type=" + input.getType());
int width = input.getWidth();
int height = input.getHeight();
BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
int px[] = new int[width * height];
input.getRGB(0, 0, width, height, px, 0, width);
output.setRGB(0, 0, width, height, px, 0, width);
ImageIO.write(output, "jpg", new File("output.jpg"));
} catch (Exception e) {
e.printStackTrace();
}
You get the same error
Caused by: javax.imageio.IIOException: Invalid argument to native writeImage
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1055)
if you are using a not supported Color Space (in my case CYMK). See How to convert from CMYK to RGB in Java correctly? how to solve this.

Java 1.5.0_16 corrupted colours when saving jpg image

i have a loaded image from disk (stored as a BufferedImage), which i display correctly on a JPanel but when i try to re-save this image using the command below, the image is saved in a reddish hue.
ImageIO.write(image, "jpg", fileName);
Note! image is a BufferedImage and fileName is a File object pointing to the filename that will be saved which end in ".jpg".
I have read that there were problems with ImageIO methods in earlier JDKs but i'm not on one of those versions as far as i could find. What i am looking for is a way to fix this issue without updating the JDK, however having said that i would still like to know in what JDK this issue was fixed in (if it indeed is still a bug with the JDK i'm using).
Thanks.
Ok, solved my problem, it seems that i need to convert the image to BufferedImage.TYPE_INT_RGB for some reason. I think the alpha channels might not be handled correctly at some layer.
I would first start by investigating if it is the BifferedImage color model that is the problem or the jpeg encoding. You could try changing the image type (3rd argument in the constructor), to see if that produces a difference, and also use the JPEGCodec directly to save the jpeg.
E.g.
BufferedImage bufferedImage = ...; // your image
out = new FileOutputStream ( filename );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bufferedImage );
encoder.setJPEGEncodeParam ( param );
encoder.encode ( bufferedImage );
out.close();
EDIT: changed text, it's the image type you want to change. See the link to the constructor.
Another approach is to render the image in a TYPE_INT_ARGB buffer, has a DirectColorModel with alpha, as outlined below and suggested here.
private BufferedImage process(BufferedImage old) {
int w = old.getWidth();
int h = old.getHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, null);
g2d.dispose();
return img;
}

Categories