ImageView getting zoomed in when drawing Rectangle Over it

160 views Asked by At

I want to draw rectangle shape over an Image that captured from the camera. First I created a Bitmap of the Image, then created a class named DrawView which extends ImageView and overrides onDraw(Canvas canvas). It drawn rectangle but not maintaining proper coordination. That's why changed LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(1920, 2560); setLayoutParams(param);

After doing the changed Image getting too much zoomed in. I don't understand what is happening here.

Captured Image:enter image description here When drawing Rectangle over the Image enter image description here

package com.example.rabel.cameratestdi;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class DrawRectAngleActivity extends AppCompatActivity
{
    DrawView drawView;
    private String filePath = null;
    ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_rect_angle);
        img = (ImageView) findViewById(R.id.imgPreview);
        Intent i = getIntent();

        filePath = i.getStringExtra("filePath");
        Bitmap bitmap = BitmapFactory.decodeFile(filePath);

        drawView = new DrawView(this);
        drawView.setVisibility(View.VISIBLE);

        drawView.setImageBitmap(bitmap);
        drawView.setScaleType(ImageView.ScaleType.FIT_XY);
        setContentView(drawView);
    }
}




package com.example.rabel.cameratestdi;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import static android.content.ContentValues.TAG;

/**
 * Created by rabel on 11/14/2017.
 */

public class DrawView extends ImageView
{
    private String result ;

    //public Canvas canvastest;
    public DrawView(Context context) {
        super(context);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(1920, 2560);
        setLayoutParams(param);
    }
    public DrawView(Context context, AttributeSet attrs) {

        super(context, attrs);
    }
    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        Utility u = new Utility();
        ArrayList<Pair<String,ArrayList<DetectedObjectInfo>>> ans = u.ParseJson(result);

        for(int i=0;i<ans.size();i++)
        {
            String _imageName = ans.get(i).first;
            ArrayList<DetectedObjectInfo> _objectInfo = ans.get(i).second;
            for(int j=0;j <_objectInfo.size();j++)
            {
                DetectedObjectInfo _object = _objectInfo.get(j);

                String mClassName = _object.GetClassName();
                float mScore = _object.GetScore();
                if(mScore >= 50)
               {
                    Pair<Float,Float> lowerLeft = _object.GetLowerLeft();
                    Pair<Float, Float> upperRight = _object.GetUpperRight();

                    float lx = lowerLeft.first;
                    float ty = getHeight() - upperRight.second;
                    float rx = upperRight.first;
                    float by = getHeight() - lowerLeft.second;

                    drawRect(canvas, lx, ty, rx, by);
              }
            }
        }
    }

    private void drawRect(Canvas canvas, float lx, float ty, float rx, float by)
    {
        Paint paint = new Paint();
        paint.setColor(Color.TRANSPARENT);
        paint.setStyle(Paint.Style.FILL);

        canvas.drawRect(lx, ty, rx, by, paint);
        paint.setStrokeWidth(10);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(lx, ty, rx, by, paint);
    }
}
0

There are 0 answers