I'm trying to convert an image into hexadecimal data for a http POST. On the iOS version I achieved this in 2 minutes with one line of code using UIImageJPEGRepresentation.
I have spent 2 days trying to achieve the same in Java with a Bitmap image but I just can't work it out.
I've converted the bitmap into a Byte Array using:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
I don't know what to do next. I've tried writing my own hexadecimal converter based on other SO answers but have been unable to achieve the extremely simple task that it was in objective-c.
Hope you can help, thanks.
Related
I am trying to write a byte array that I know the size of to the array however I am not able to parse the resultant data.
I am using the following code:
okAppender.writeBytes(b -> b.write(byteData));
and
byte[] byteData = new byte[500];
okTailer.readBytes(b -> b.read(byteData));
I expect there is padding but I do not know how much. Any insight would be appreciated.
I suggest you use the chronicle Bytes class ( which can wrap a bytebufffer ) as reading into a byte[] creates and object each time.
as the title say i need to find some java jpeg encoder (it's good both source code or external library) that given an array that represent a raw pixel image or a BufferedImage can encode it without writing anything on file and return the encoded image possibly trough an array of some kind, with at least possibility to choose image quality and possibly with good efficiency.
NB: the array/image type input required (byte, int, argb, rgb, bgr, yuv...) doesn't matter for me, i can make approppriate conversions
As already mentioned in the comments: You can use the ImageIO class, and use it to write to a ByteArrayOutputStream. The code could really be as simple as this:
private static byte[] getJpgData(BufferedImage image)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
return baos.toByteArray();
}
This will NOT write the image to a disc or so. It will only write the image into a memory block, which you can then process or manipulate further.
If you just need a stream of bytes, then use Java's built in methods.
http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#write(java.awt.image.RenderedImage,%20java.lang.String,%20java.io.OutputStream)
ImageIO.write(myImage, "format", myOutputStream);
I've spent several hours trying to figure out how to do this. I've read post after post here on stackoverflow and the documentation.
I have a android.graphics.Bitmap object and I need to get it's md5 sum. At the point that I want to verify the sum it has not been saved to the file system. I've seen several ways of doing this for java.io.File objects. I just need a function that receives a Bitmap object and returns the hex md5 sum as a String.
This might have been addressed somewhere but if it has been I have been unable to understand it or deduce how to do it from it.
The less resource heavy the method is the better it is of course.
Get bitmap's bytes to calculate md5.
Bitmap bm = ... // your bitmap
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] bitmapBytes = baos.toByteArray();
So you have byte array now. You can find how to get md5 hash of byte array in android here.
I'm not Android developer, but I see in the API reference (http://developer.android.com/reference/android/graphics/Bitmap.html) that there are methods for:
getting size of the bitmap: getWidth, getHeight
getting the pixels as an array of integers: getPixels
So you could just create an array of needed size, then read all the pixels, and convert the array to byte[].
Then it should be not a problem to calculate md5 sum from it.
Hi I have a model testing question for a object recognition project i am working on. I want to be able to take .jpeg files I have in my eclipse project folder and reduce them to very sparse byte arrays in Java. For example if I had a picture of a ball I would like to be able to convert it to the following byte 2-D array:
00000000000000000
00000001110000000
00001100001110000
00010000000001000
00010000000001000
00001000000010000
00000011111000000
00000000000000000
If someone could be so kind as to explain how I can do this most efficiently I would greatly appreciate it. I am fairly new to programming and do not understand much more than oop so if you could describe the process in simple programming terms without any jargon I would really appreciate it.
First to get byte array of image you need to convert image to BufferedImage. See ths link to convert image to BuffredImage. http://www.dzone.com/snippets/converting-images
After you get BufferedImage convert t into bytearray using bufferedImageToByteArray function.
BufferedImage buf_image; // this is BufferedImage reference you got after converting it from Image
byte[] imageByteArray = bufferedImageToByteArray(buf_image,"jpg");
public static byte[] bufferedImageToByteArray(BufferedImage image, String format) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, format, baos);
return baos.toByteArray();
}
Good friends of the forum.
I've been searching a lot, and I could not find how to serialize an image and pass it to a String in android.
as might be not so, If someone knows and wants to inform me I would appreciate very much!.
Although the java.awt.Image class is not (does not implement) java.io.Serializable, javax.swing.ImageIcon is. Because of this, you can serialize it as follows:
ImageIcon myImage; // declare somewhere
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(myImage);
byte[] theBytes = baos.toByteArray();
StringBuffer buf = new StringBuffer();
for (byte b : theBytes) {
buf.append((char) b); // cast to char, then append
}
String theString = b.toString();
I'll let you figure out how to reverse it, but here's a hint: instead of OutputStream classes, use InputStream classes.
You could read the bytes of the image to a byte[] and then encode the byte[] using Base64, Here is how.
Base64 will be the most efficient way to reliably transfer binary data (such as an image) in a string.
However, since you are requesting something smaller, you may consider base64 encoding your image, then compressing the resultant string, then base64 encoding that...
It saves you a few bytes, but not many. And in some cases it may even make the result larger.
If you have control of both the server and the client, you should consider creating another interface that would allow you to send bytes.