android 在圖片(bitmap)上畫矩形框,並顯示
阿新 • • 發佈:2019-02-16
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);
}
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);
}