android 在图片(bitmap)上画矩形框,并显示

在原有图片上画矩形框:









实现方式:


private void drawRectangles(Bitmap imageBitmap, int[] keywordRects,
int[] valueRects) {
int left, top, right, bottom;
Bitmap mutableBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
//Canvas canvas = new Canvas(imageBitmap);
Paint paint = new Paint();
for (int i = 0; i < 8; i++) {
left = valueRects[i * 4];
top = valueRects[i * 4 + 1];
right = valueRects[i * 4 + 2];
bottom = valueRects[i * 4 + 3];
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);//不填充
paint.setStrokeWidth(10);  //线的宽度
canvas.drawRect(left, top, right, bottom, paint);
}
for (int i = 0; i < 6; i++) {
left = keywordRects[i * 4];
top = keywordRects[i * 4 + 1];
right = keywordRects[i * 4 + 2];
bottom = keywordRects[i * 4 + 3];
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
canvas.drawRect(left, top, right, bottom, paint);
}
img.setImageBitmap(mutableBitmap);//img: 定义在xml布局中的ImagView控件
//img.setImageBitmap(imageBitmap);
}


注意:

(1)keywordRects:姓名,性别,。。。。的坐标值。

valueRects:袁政政,女,。。。。得坐标值。

(2)画矩形框思路:左上角和左下角的坐标值,知道这两个坐标值就可以画出矩形框了。

(3)传给Canvas之前做不做copy(Bitmap.Config.ARGB_8888, true)处理?      

Bitmap mutableBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888, true);

之前直接传给Canvas时报错:Immutable bitmap passed to Canvas constructor。

 网友的回答是:



但是后来发现直接传给Canvas也行。不知道为什么




































没有更多推荐了,返回首页