I first parse image, and index it in database as byte[] which means the byte[] variable has that image in byte format
I am going to use images on search list.
Is it possible to put byte[] variable in File()?
ImageIO.read(new File(byteImage+".png"));
Your image is stored as a png? If so, you can ready it directly from the byte array with:
BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));
use new FileOutputStream(fileVariable).write(bytes);
Related
Displaying an image in base64 is rather simple:
<img src="data:image/png;base64,hexadecimal-code-for-image-here">
However, what I'm trying to do is to convert the hexadecimal value received and the save it in a png file so I get the desired picture from the code.
For instance: let's say I have the following code:
iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABmJLR0QAAAAAAAD5
Q7t/AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3wEVDTYmJtINnAAAAcRJ
REFUOMuVlL9rU1EUx7/n3psmL6VoWqoJpNgI0Vg6uXSw0E0QHBzsUJDSoSKCuLro
KIL0D3Dr2N0gOHUpdWgrLW0EW5osakNT+4Oo7+X13XuPi4L2SZ73jGf43O/hfM4l
nKnC9TtZmStfIwKFn5bXW9vvDBxKnG2YdP7BzOzDtanp+6uBGLwFx1LxF7RtHvmI
tIaUsK7AWEJW6UgbC2MYxtjQFUijky/Hy5WROQIgBbgT6nyuUBo2hvGtVf8oVPrE
GCbDwM6Hjce71WcrXUfuv1C8lC9eHiMwlCCcGgs/1CBiFEuVShhZBKcGHQ309H0e
AtAd+PXwkFa36shkPCglIaXEcOE8jABqjQMQgEhrdAIfZCNKHFmlsr3C6yuzjaCD
tr56++m9GzfvPgEDr+efPzquVZco5QkhFcJ2cxuMoGtCHfk/EPkbvxsD+dJejyIA
jMzFkYZ+v7CJ8Pt/exrbMhFoaCCNYn8G2gpy9TTm4cF+U7xZ+QIAyHKbXD2NARtv
X7w6rk8sghmtrWrN1dPErbl6qpKArp4mAl09TRzZ1dPEhK6eKuff5JenzPinp87A
JE/JFZjyznm58sSVPzz96/R+AmVHLPIJpOvnAAAAAElFTkSuQmCC
Is there any way I can convert this code to image and then store in a png file in JAVA?
Transfer the base64 into byte array and than write it to a png file.
byte[] img = Base64.getDecoder().decode(imgBase64);
Files.write(Paths.get("my.png"), img); //As suggested by Joop Eggen
Decode base64 image to byte[] then using ImageIO to write to file
byte[] imgInBytes = Base64.getDecoder().decode(base64Img);
InputStream in = new ByteArrayInputStream(imgInBytes);
BufferedImage bufferedImage = ImageIO.read(in);
File png = new File("ImageAsPNG.png");
ImageIO.write(bufferedImage, "PNG", png);
I decoded an image (JPEG) with Android BitmapFactory class and it decoded fine with color format ARGB_8888.
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().toString()+"/camel.jpg",
new BitmapFactory.Options());
Log.d(TAG,"Color format of the bitmap : "+bitmap.getConfig());
I dumped the raw buffer from the Bitmap class
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(buffer);
Log.d(TAG,"Color format of the bitmap : "+bitmap.getConfig());
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/buffer.raw");
outputStream.write(buffer.array());
outputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
and examined the dump in a Hex editor , it looks like the ordering of the color components is not in the order A,R,G and B.
Instead it looks like ordering is R,G,B and A.
As seen above the alpha (FF) is in the end of the set of four bytes for a pixel.Also to corroborate this I opened the image in 7yuv and image is displayed properly with color format RGBA and little-endian encoding.
My confusion is why is Android reports the format of the Bitmap as ARGB_8888 and actual byte ordering is R,G,B and A.
I was wondering this might be due to some endian mismatch , but in that case the whole ordering needs to be simply reversed (B,G,R and A).
Also I might be doing something wrong while dumping the bitmap to a raw buffer (and I am not very good at Java) , but I am not sure.
Thanks in advance!
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.
I have a problem with showing a picture from the database, the database saves the picture in blob, when i pick up the data the blob passes to Byte[], so after that i do that to show the image, why didnt work?
Select_1 xp = new Select_1();
byte[] img=xp.Select_1(username);
InputStream in = new ByteArrayInputStream(img);
BufferedImage image = ImageIO.read(in);
BufferedImage resizedImage=resize(image,204,204);
ImageIcon icon=new ImageIcon(resizedImage);
lblavatar.setIcon(icon);
Edit according to the comment:
Originally, the image was written using the following methods:
blob = (Blob) connect.createBlob();
ImageIcon ii = new ImageIcon(ficheiro);
ObjectOutputStream oos;
oos = new ObjectOutputStream(blob.setBinaryStream(1));
oos.writeObject(ii);
oos.close();
psInsert.setBlob(4, blob);
You are not serialzing a BufferedImage, but an ImageIcon.
In order to create an image from the blob data, you have to do "the opposite" of what you have been doing to create the blob. In this case, you'll have to do something along the lines of
byte[] img=xp.Select_1(username);
InputStream in = new ByteArrayInputStream(img);
ObjectInputStream ois = new ObjectInputStream(in);
ImageIcon imageIcon = (ImageIcon)ois.readObject();
Now, you have an ImageIcon, from which you can obtain the Image. For many cases, this image can be used directly. If you really need a BufferedImage, then you can do
BufferedImage bi = new BufferedImage(204,204,BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(imageIcon.getImage(), 0, 0, 204, 204, null);
g.dispose();
Then the buffered image will contain your image (already scaled to the desired target size of 204,204 in this case).
In any case, you should consider to not store the image as a serialized ImageIcon. Instead, you should write your image into a byte array as a PNG or JPG file, and store the resulting byte array as the blob in the database, as, for example, shown in Java: BufferedImage to byte array and back
I need to know how to get an array of bytes from a loaded image, in java. BufferedImage seems not to supply any methods that produce an array of bytes, so what do I use?
BufferedImage bufferedImage; //assumed that you have created it already
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"jpg", byteStream);
byte[] byteArray = byteStream.toByteArray();