i have image captured camera, in raw bgra format (byte array). how can save disk, jpg/png file?
i've tried imageio.write java api, got error illegalargumentexception (image = null)
code:
try { inputstream input = new bytearrayinputstream(img); bufferedimage bimagefromconvert = imageio.read(input); string path = "d:/image.jpg"; imageio.write(bimagefromconvert, "jpg", new file(path)); } catch(exception ex) { system.out.println(ex.tostring()); } note "img" raw byte array, , not null.
the problem imageio.read not support raw rgb (or bgra in case) pixels. expects file format, bmp, png or jpeg, etc.
in code above, causes bimagefromconvert become null, , reason error see.
if have byte array in bgra format, try this:
// need know width/height of image int width = ...; int height = ...; int samplesperpixel = 4; int[] bandoffsets = {2, 1, 0, 3}; // bgra order byte[] bgrapixeldata = new byte[width * height * samplesperpixel]; databuffer buffer = new databufferbyte(bgrapixeldata, bgrapixeldata.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 = 0 ... imageio.write(image, "png", new file(path)); note jpeg not format storing images alpha channel. while possible, software not display properly. suggest using png instead.
alternatively, remove alpha channel, , use jpeg.
Comments
Post a Comment