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.
Related
I am trying to pass an image from java to flutter. The image is in bitmap in java so I first try to convert it to bytes[] using:
on java side:
int byteCount = transferredImage.getAllocationByteCount(); // transferredImage is the bitmap
ByteBuffer buffer = ByteBuffer.allocate(byteCount); //Create a new buffer
transferredImage.copyPixelsToBuffer(buffer);
byte[] myByte = buffer.array();
Then on flutter side I read the image as Uint8List and create an img.Image object from it:
img.Image styleImage = img.Image.fromBytes(
imgWidth,
imgHeight,
transferredBytes,
);
This works, without causing any errors but the image displayed using Image.memory(img.encodeJpg(styleImage)) always has a blue tint. The bitmap on java side is fine but when converted to bytes[] and displayed in flutter it has a blue tint..
So I assumed some important information may have been dropped during conversion to bytes using my above method, I then convert the image to an image format and then transfer the bytes:
at java side:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transferredImage.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte bytes []= baos.toByteArray();
on flutter side:
img.Image styleImage = img.Image.fromBytes(
imgWidth,
imgHeight,
transferredBytes,
);
But this does not work and creates an error:
RangeError (index): Index out of range: index should be less than
321512: 322560
If possible, I would prefer to have my first attempt work as it is really fast, so is there any way I can get this to work ?
Thanks
I'm in the draw loop of an android view:
Bitmap bitmap = Bitmap.createBitmap(this.getWidth(),
this.getHeight(), Bitmap.Config.ARGB_4444);
Canvas newCanvas = new Canvas(bitmap);
super.draw(newCanvas);
Log.d("AndroidUnity","Canvas Drawn!");
mImageView.setImageBitmap(bitmap);
And the above code shows me the correct drawing on the attached Image Viewer.
When I convert the bitmap to a byte array:
ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(byteBuffer);
byte[] bytes = byteBuffer.array();
importing the bytes into Unity does not work (shows a black image on my rawimage):
imageTexture2D = new Texture2D(width, height, TextureFormat.ARGB4444, false);
imageTexture2D.LoadRawTextureData(bytes);
imageTexture2D.Apply();
RawImage.texture = imageTexture2D;
Any ideas on how to get the Java bytes[] to display as a texture/image in Unity? I've tested that the bytes are sending correctly, i.e. when I push a byte array of {1,2,3,4} from android, I get {1,2,3,4} on the unity side.
this isn't mentioning that Unity throws an error when trying to transfer the bytes as a byte[], so instead I have to follow this advice, on the C# side:
void ReceieveAndroidBytes(AndroidJavaObject jo){
AndroidJavaObject bufferObject = jo.Get<AndroidJavaObject>("Buffer");
byte[] bytes = AndroidJNIHelper.ConvertFromJNIArray<byte[]>(bufferObject.GetRawObject()); }
and a trivial byte[] container class "Buffer" on the java side
I was trying to do the exact same thing and my initial attempts also had a black texture. I do the array conversion with AndroidJNIHelper.ConvertFromJNIArray like you do except I used sbyte[] instead of byte[]. To set the actual image data I ended up using
imageTexture2D.SetPixelData(bytes, 0);
If I'm not mistaken LoadRawTextureData is even rawer than an array of pixel data, it might be how graphics cards store textures with compression. If that is true then raw pixel data isn't in the right format and it can't be decoded.
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 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);
First of all, im asking this specific question, because i've already read many examples around this topic, but none of them really helped.
My problem is that i'd like to convert and display a jpg image. I don't have the original image, it is on a server. The data comes through a stream (socket) and it is given in a byte array. In this byte array, every 4 bytes represents information about a pixel. I managed to get the RGBs and convert/save them into a bmp image, however i couldn't find a working solution to make a jpg.
On the other hand, i have a BMP :) how can i display it in a JLabel or JPanel? I also read abou imageicon ImageIO etc. but it doesn't work for me. I use ScheduleExecutorSystem btw. Maybe that is the problem?
Here's the encoder code (Copyright (C) 2013 Philipp C. Heckel ):
public static void encodeToBitmap(byte[] srcBytes, OutputStream destStream)
throws IOException {
int imageWidth = 1024;
int imageHeight = 1080;
int imageBytes = imageWidth * imageHeight * 3;
int filesizeBytes = imageBytes + BMP_SIZE_HEADER;
byte[] header = BMP_HEADER.clone(); // Clone bitmap header template, and
// overwrite with fields
header = writeIntLE(header, BMP_OFFSET_FILESIZE_BYTES, filesizeBytes);
header = writeIntLE(header, BMP_OFFSET_IMAGE_WIDTH, imageWidth);
header = writeIntLE(header, BMP_OFFSET_IMAGE_HEIGHT, imageHeight);
header = writeIntLE(header, BMP_OFFSET_IMAGE_DATA_BYTES, 0);
header = writeIntLE(header, BMP_OFFSET_PAYLOAD_LENGTH,
(int) srcBytes.length);
// WRITE TO STREAM
// Add payload
destStream.write(header, 0, header.length);
for (int offset = imageBytes - imageWidth * 3; offset >= 0; offset -= imageWidth * 3) {
for (int i = 0; i < (imageWidth) * 3; i++) {
destStream.write(srcBytes[offset + i]);
}
}
destStream.close();
}
And the code, how i use it:
BitmapEncoder.encodeToBitmap(RGBvalues, new FileOutputStream("path to file"));
RGBvalues - bytes of 3, with the RGB values (i don't say!! :P )
image width and Height is fix for debug purpose
I have a JPanel and a JLabel within, and i added a default ImageIcon to the JLabel (a jpg image), and it works fine.
After i saved the image, i try :
ImageIcon icon = new ImageIcon("path to file");
pictureLabel.setIcon(icon);
It only makes the default disappear. Am i forgetting to set something?
Is this the fastest way, to do it?
The ImageIcon class only directly supports reading PNG, GIF and JPEG types. You need to use ImageIO to read your bitmap into a generic Image object (which is supported by ImageIcon) and then pass that to ImageIcon. Here's how I'd modify your code to work:
File imgFile = new File("path to file");
Image image = javax.imageio.ImageIO.read(imgFile);
ImageIcon icon = new ImageIcon(image);
pictureLabel.setIcon(icon);
Note that you can also use ImageIO to convert your bitmap to JPEG format.