List
Image Target bitmap
Posted Date: 2018-01-12 13:22     Edited Date: 2018-01-15 9:24     Writer: inactive

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. 

Posted Date: 2018-01-15 1:36     Edited Date: 2018-01-15 1:36     Writer: inactive

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

Posted Date: 2018-01-15 6:43     Edited Date: 2018-01-15 6:43     Writer: inactive

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?

Posted Date: 2018-01-15 7:48     Edited Date: 2018-01-15 8:15     Writer: inactive

To get the image, you must call it after the following code: You probably have not called getTrackingResult.

 

TrackingResult trackingResult = state.getTrackingResult();

 

So to get a bitmap in your code, you need to modify it like this:

 

TrackingState state = TrackerManager.getInstance().updateTrackingState();
TrackingResult trackingResult = state.getTrackingResult();
byte[] data = state.getImage().getData();
int dataLength = data.length;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);

 

Thank you.

- MAXST support team

Posted Date: 2018-01-15 8:15     Edited Date: 2018-01-15 8:15     Writer: inactive

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. 

Posted Date: 2018-01-15 9:22     Edited Date: 2018-01-15 9:22     Writer: inactive

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.

 

1. 3 bytes image data -> 4 bytes image data
2. create bitmap (config.ARGB_8888)
3. set converted image data(4 bytes) to bitmap

 

To convert, see the following code:

 

int[] newdata = new int[width * height * 4];
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

Posted Date: 2018-01-15 9:24     Edited Date: 2018-01-15 9:24     Writer: inactive

Works like a charm.

Thank you :)