MhdFeras / cordova-plugin-image-recognition

image recognition plugin for Cordova apps
9 stars 0 forks source link

fragment of the recognized image #4

Closed armanth14 closed 5 years ago

armanth14 commented 5 years ago

Hello Is it possible to obtain the fragment of the recognized image?

MhdFeras commented 5 years ago

Hi @armanth14

sorry that's not possible in this plugin but you can add it to java code if you want that feature

in BoxRenderer.java the function render here I display blue box over the recognized image you can use it's vertices to crop the fragment of the recognized image and you can get the full image from OpenGl : GLES20.glReadPixels

armanth14 commented 5 years ago

Thank you very much, I will try and tell you.

armanth14 commented 5 years ago

Hello, excuse me the question

As I do to obtain the position x y, and the dimensions w and h from the vertices, the truth is that I do not have much knowledge in gles2

thank you

MhdFeras commented 5 years ago

sorry I'm not that good in OpenGl but after googling it i got this code form here https://stackoverflow.com/questions/24421290/how-do-you-convert-opengl-texture-back-to-bitmap-in-android and here https://stackoverflow.com/questions/17674634/saving-and-reading-bitmaps-images-from-internal-memory-in-android

int w=400;
int h=400;

IntBuffer ib = IntBuffer.allocate(w*h*4);
GLES20.glReadPixels(0, 0, w, h, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);

for(int i=0, k=0; i<h; i++, k++)
{
//remember, that OpenGL bitmap is incompatible with Android bitmap
//and so, some correction need.
    for(int j=0; j<w; j++)
    {
        int pix=b[i*w+j];
        int pb=(pix>>16)&0xff;
        int pr=(pix<<16)&0x00ff0000;
        int pix1=(pix&0xff00ff00) | pr | pb;
        bt[(h-k-1)*w+j]=pix1;
    }
}
Bitmap sb=Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
saveToInternalStorage(sb);

// this function to save bitmap image to internal storage 
private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"image.jpg");

        FileOutputStream fos = null;
        try {           
            fos = new FileOutputStream(mypath);
       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
            try {
              fos.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
        } 
        return directory.getAbsolutePath();
    }

give it a try add this code to BoxRenderer.java in the function render