Image Target bitmap
Hello :)
I am trying to extract some data added to recognizable image target - is it possible to get bitmap from camera preview limited to image target bounds for processing purposes?. Trackable object doesn't contain any data I want to use.
Hello
To summarize the question, do you mean you want to get the image data used for tracking?
You can get the current camera frame. The instruction is as follows.
TrackingState state = TrackerManager.GetInstance().UpdateTrackingState();
TrackedImage image = state.GetImage();
byte[] imageData = image.GetData();
Thank you.
- MAXST support team
Hello,
Yes, it seems to be what I expected. Unfortunately, I am trying to use this data this way:
BitmapFactory.Options options = new BitmapFactory.Options(); options.inMutable = true; Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
but "bitmap" is always null. How can I use and process it?
To get the image, you must call it after the following code: You probably have not called getTrackingResult.
So to get a bitmap in your code, you need to modify it like this:
TrackingResult trackingResult = state.getTrackingResult();
byte[] data = state.getImage().getData();
int dataLength = data.length;
options.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
Thank you.
- MAXST support team
I am working on a class for you sample - ImageTrackerRenderer - where both lines are definitely called:
TrackingState state = TrackerManager.getInstance().updateTrackingState(); TrackingResult trackingResult = state.getTrackingResult();
Then I am trying to process it after one of trackable object is recognized:
for (int i = 0; i < trackingResult.getCount(); i++) { Trackable trackable = trackingResult.getTrackable(i); if (trackable.getName().equals("Lego")) { useImage(state.getImage().getData()) ...
private void useImage(byte[] data){ BitmapFactory.Options options = new BitmapFactory.Options(); options.inMutable = true; Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); }
Unfortunately, it's always null.
Thank you for your help.
Android's bitmap supports 2bytes (YUV) / 4bytes (RGBA) format by default and does not support 3bytes format. So the image output from trackingState is 3bytes (RGB), so if you want to use it in a bitmap object, convert it to 4bytes.
2. create bitmap (config.ARGB_8888)
3. set converted image data(4 bytes) to bitmap
To convert, see the following code:
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int oldIdx = (y * width + x) * 3;
int newIdx = (y * width + x) * 4;
newdata [newIdx + 0] = data[oldIdx + 0];
newdata [newIdx + 1] = data[oldIdx + 1];
newdata [newIdx + 2] = data[oldIdx + 2];
newdata [newIdx + 3] = 255;
}
}
Bitmap bitmap = Bitmap.createBitmap(newdata, width, height, Bitmap.Config.ARGB_8888);
Thank you.
- MAXST support team
Works like a charm.
Thank you :)