Fastest Way to Convert Image to Byte Array

Fastest way to convert Image to Byte array

So is there any other method to achieve this goal?

No. In order to convert an image to a byte array you have to specify an image format - just as you have to specify an encoding when you convert text to a byte array.

If you're worried about compression artefacts, pick a lossless format. If you're worried about CPU resources, pick a format which doesn't bother compressing - just raw ARGB pixels, for example. But of course that will lead to a larger byte array.

Note that if you pick a format which does include compression, there's no point in then compressing the byte array afterwards - it's almost certain to have no beneficial effect.

How to convert image to byte array

Sample code to change an image into a byte array

public byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms,imageIn.RawFormat);
return ms.ToArray();
}
}

C# Image to Byte Array and Byte Array to Image Converter Class

Convert image to byte array quickly

In getImageBytes() you are compressing the image to PNG which can take time. The fastest ImageIO.write call you can achieve is

ImageIO.write(copyImage(image), "bmp", baos);

where there is no compression. Still, ImageIO contains reference implementations of image formats which are not designed for speed.

Convert selected image into byte array and into string

To convert image to string, use following short of code.

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
yourbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//this will convert image to byte[]
byte[] byteArrayImage = baos.toByteArray();
// this will convert byte[] to string
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

Now, you have encodedImage string of image.

Your code of "saveItem()" looks like following.

private void saveItem() {

// Client-Server - Start //////////////////////////////////////
String name = inputname.getText().toString();
String description = inputnote.getText().toString();
// Encode the image file to String !! by using Base64
//String encodedImage = Base64.encodeToString(blob, Base64.DEFAULT);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yourbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//this will convert image to byte[]
byte[] byteArrayImage = baos.toByteArray();
// this will convert byte[] to string
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("name", name));
params1.add(new BasicNameValuePair("description", description));
params1.add(new BasicNameValuePair("photo",encodedImage));

Log.v("log_tag", System.currentTimeMillis()+".jpg");

// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params1);

// check log cat fro response
Log.d("Create Response", json.toString());

// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);

Log.v("log_tag", "In the try Loop" );

if (success == 1) {
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}

Converting Image to Bytearray with Python

Create io.BytesIO buffer and write to it using PIL.Image.save. Set appropriate quality and other parameters as per requirement.

import io
from PIL import Image

def convert_pil_image_to_byte_array(img):
img_byte_array = io.BytesIO()
img.save(img_byte_array, format='JPEG', subsampling=0, quality=100)
img_byte_array = img_byte_array.getvalue()
return img_byte_array

References:

Why is the quality of JPEG images produced by PIL so poor?

convert image and save as byte array

I see what you are trying to do. A similar approach is used in terrain rendering with an heightMap. The idea is to map an image as grey scale into a text file, which in turn is used to generate the height of a terrain.

In your case you can just map it as 0s and 1s(where 1s is the colour of your obstacles).

You can then upload the mapped text file into a 2D array which will be your map bird-eye view.

As the bot moves across the scene you can detect whether it can safely move or not by checking the current position on the 2D array map.

Hope it helps.

What is the fastest way to convert JPEG byte data to raw greyscale byte information?

Theoretically, the fastest way would be to just decode the Y component and ignore Cb and Cr in the stream.

how to convert image to byte array in java?

BufferedImage consists of two main classes: Raster & ColorModel. Raster itself consists of two classes, DataBufferByte for image content while the other for pixel color.

if you want the data from DataBufferByte, use:

public byte[] extractBytes (String ImageName) throws IOException {
// open image
File imgPath = new File(ImageName);
BufferedImage bufferedImage = ImageIO.read(imgPath);

// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();

return ( data.getData() );
}

now you can process these bytes by hiding text in lsb for example, or process it the way you want.



Related Topics



Leave a reply



Submit