大家好,我正在努力使用位图和画布。我要做的是用相机拍照,然后让用户创建两个矩形(通过滑动手指)并在图像中标记它们(矩形应该被标记,直到按下按钮,照片没有保存,它始终在内存中)。因此,基于相机示例,我使用SurfaceView进行布局以包含相机预览,然后我添加了代码以在onPictureTaken方法中绘制矩形。我搜索了一些关于如何实现它的例子但当然不起作用。到目前为止,我有这个代码(在onPictureTaken中):
final Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
arg0.length);
surfaceView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
if (source coordinates of rect1 are not set) {
setSourceCoordinatesForRect1FromArg1();
} else {
setSourceCoordinatesForRect2FromArg1();
}
break;
case MotionEvent.ACTION_UP:
if (end coordinates of rect1 are not set) {
setEndCoordinatesForRect1FromArg1();
} else {
setEndCoordinatesForRect2FromArg1();
}
break;
default:
break;
}
if (coordinates for rect1 are set) {
Paint paint = new Paint();
Bitmap bmOverlay = Bitmap.createBitmap(bitmapPicture.getWidth(), bitmapPicture.getHeight(), bitmapPicture.getConfig());
Canvas canvas = new Canvas(bmOverlay);
paint.setColor(Color.GREEN);
paint.setStrokeWidth(3);
canvas.drawRect(/*all of my source coordinates*/, paint);
} else {
if (coordinates for rect2 are set) {
Paint paint = new Paint();
Bitmap bmOverlay = Bitmap.createBitmap(bitmapPicture.getWidth(), bitmapPicture.getHeight(), bitmapPicture.getConfig());
Canvas canvas = new Canvas(bmOverlay);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(3);
canvas.drawRect(/*all of my end coordinates*/, paint);
}
}
return true;
}
});
我没有任何异常,但是没有绘制矩形,所以如果有人能告诉我我做错了什么,我真的很感激。另外,对于我的特定场景,是否适合使用GestureDetector而不是创建自定义的OnTouchListener? 提前谢谢。
答案 0 :(得分:0)
您可以创建画布和位图,但不要将它们连接到您的视图:
Bitmap bmOverlay = Bitmap.createBitmap(bitmapPicture.getWidth(), bitmapPicture.getHeight(), bitmapPicture.getConfig());
Canvas canvas = new Canvas(bmOverlay);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(3);
canvas.drawRect(/*all of my end coordinates*/, paint);
你需要创建一个位图(你做到了这一点),然后在那里绘制两个rects,然后将该位图连接到你的视图:
if (coordinates for rect1 are set) {
Paint paint = new Paint();
Canvas canvas = new Canvas(bitmapPicture);
paint.setColor(Color.GREEN);
paint.setStrokeWidth(3);
canvas.drawRect(/*all of my source coordinates*/, paint);
} else {
if (coordinates for rect2 are set) {
Paint paint = new Paint();
Canvas canvas = new Canvas(bitmapPicture);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(3);
canvas.drawRect(/*all of my end coordinates*/, paint);
}
}
}
yourImageView.setImageDrawable(new BitmapDrawable(getResources(), bitmapPicture));
答案 1 :(得分:0)
好吧,经过很长一段时间我可以画出一个位图。碰巧我有几个错误:首先,我试图在SurfaceView中绘制,这是不可能的,然后我添加了一个ImageView来包含位图和画布,所以,现在我的代码看起来像这样:
Bitmap tempBitmap = Bitmap.createScaledBitmap(bt, bt.getWidth(), bt.getHeight(), true);
Canvas canvas = new Canvas(tempBitmap);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
canvas.drawLine(x1, y1, x2, y1, paint);//up
canvas.drawLine(x1, y1, x1, y2, paint);//left
canvas.drawLine(x1, y2, x2, y2, paint);//down
canvas.drawLine(x2, y1, x2, y2, paint);
ImageView iView = (ImageView)findViewById(R.id.imageViewPreview);
iView.setImageBitmap(tempBitmap);
iView.draw(canvas);
此代码位于单独的活动中,我在其中实现了OnTouchListener
以读取绘制矩形的坐标。读完坐标后,执行代码
作为参考,布局是一个包含ImageView的FrameLayout(看看this答案)。
这段代码终于绘制了一个矩形,但幸运的是图像越来越大了:(但这是另一个问题,所以如果你想跟随它here is the link。