我想在从相机捕获的图像上绘制矩形形状。首先,我创建了一个Image的Bitmap,然后创建了一个名为DrawView的类,它扩展了ImageView并覆盖了onDraw(Canvas canvas)。它绘制了矩形但没有保持适当的协调。这就是改变的原因 LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(1920,2560); setLayoutParams(PARAM);
在更改后的图像变得过于放大之后。我不明白这里发生了什么。
捕获的图片:enter image description here 在图像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);
}
}