Search code examples
javaandroidbitmapqcar-sdk

Get x,y coordinated for an area not covered by text


In the textreco sample, when text is detected a rectangular box is drawn around it. I need to select any area from within this box that is not covered by text. I therefore need positive x,y coordinates of any area that is not covered by text but lies within this box.

what has been done: I first get a bitmap that contains the text region and can obtain x,y coordinates of the top left and bottom right corners of the bounds.

Research done: using the getMask() as per API Returns an image representing the bit mask of the letters in the word.Each pixel in the image is represented by a byte (8-bit value). A value of 255 represents an empty area, i.e. a pixel not covered by any letter of the word. If a pixel is covered by a letter, then the pixel value represents the position of that letter in the word, i.e. 0 for the first character, 1 for the second, 2 for the third, and so on.

Can anyone help on how I could iterate over the pixels of the image and based on the position (row and column) of the pixel in the image, be able to compute the coordinates that im looking for?


Solution

  • Use this code in the render-er native function.

    const unsigned char *pixels = (const unsigned char *)mask->getPixels();
    
                int xCoord = 0;//gives the x coordinate of the first pixel
                int yCoord = 0;//y coordinate
    
                for (int row = 0; row < num_rows; ++row)
                {
                    for (int column = 0; column < num_cols; ++column)
                    {
                        unsigned char byte = pixels[row*num_cols + column]; // Get byte from mask
                        if (byte == 255)
                        {
                            xCoordPadding = row;
                            yCoordPadding = column;
    
                            break;
                        }
                    }
                }