Search code examples
javaandroidcanvasfirebase-mlkit

How to draw multiple points on imageview in android java


I'm setting up an eye detection android project and I want to draw probabilities using Canvas, I'm using Firebase ML kit for custom models I have successfully drawn only one point. I would like to draw points ( probabilities from tflite model that I have ).

I tried using those functions :

private void useInferenceResult(float[] probabilities) throws IOException {

    // [START mlkit_use_inference_result]
    String[] result=new String[80];
    float x=0;
    float y=0;
    ArrayList<Point> listpoint= new ArrayList<Point>();

    for (int i = 0; i < probabilities.length; i++) {

        Log.i("MLKit", String.format("%1.4f", probabilities[i]));
        x=probabilities[i];
        y=probabilities[i+1];
        Point p=new Point(x,y);
        i=i+1;

        p.setX(x);
        p.setY(y);

        Log.i("Information1 ","valeur 1 "+p.getX());

        listpoint.add(p);
            Log.i("Information2 ","valeur 2 "+p.getX());
    }

    for(int j=0;j<listpoint.size();j++){

        Log.e("Information","work");
        Log.e("Resultat","point_"+j+"("+listpoint.get(j).getX()+", "+listpoint.get(j).getY()+")");
        float xx=listpoint.get(j).getX()*100;
        float yy=listpoint.get(j).getY()*100;
        drawpoint(image2,0.20958422f * 100,0.6274962f * 100,1);
        drawpoint(image2, 0.20460524f * 100,0.6708223f * 100,1);

    }
}

//drawpoint function
private void drawpoint(ImageView imageView,float x,float y, int raduis){

    BitmapFactory.Options myOptions = new BitmapFactory.Options();
    myOptions.inDither = true;
    myOptions.inScaled = false;
    myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
    myOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.imgg,myOptions);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.WHITE);

    Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
    Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);

    Canvas canvas = new Canvas(mutableBitmap);
    canvas.drawCircle(x,y, raduis, paint);

    imageView = (ImageView)findViewById(R.id.imageView);
    imageView.setAdjustViewBounds(true);
    imageView.setImageBitmap(mutableBitmap);
}

but I didn't get any result just one point drawn. How can I draw multiples points on imageView?


Solution

  • I solved my problem by changing some lines of code, I declare those variables on the top and I create objects after setContentView:

    BitmapFactory.Options myOptions;
    Canvas canvas;
    Bitmap mutableBitmap;
    Bitmap workingBitmap;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_open= findViewById(R.id.btn_open);
        image2= findViewById(R.id.imageView);
        myOptions = new BitmapFactory.Options();
        bitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.image000880,myOptions);
        paint= new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
    
        workingBitmap = Bitmap.createBitmap(bitmap);
        mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
        canvas = new Canvas(mutableBitmap);
    
       private void drawpoint(ImageView imageView,float x,float y, int raduis){
        myOptions.inDither = true;
        myOptions.inScaled = false;
        myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
        myOptions.inPurgeable = true;
    //  ArrayList<Point> list= new ArrayList<>();
        canvas.drawCircle(x,y, raduis, paint);
        imageView = (ImageView)findViewById(R.id.imageView);
        imageView.setAdjustViewBounds(true);
        imageView.setImageBitmap(mutableBitmap);
    }
    

    Hope this solution will help others who have the same situation