0

大家好,我在位图和画布上苦苦挣扎。我想要做的是用相机拍照,然后允许用户创建两个矩形(通过滑动手指)并在图像中标记它们(矩形应该被标记,直到按下按钮,照片没有保存,它总是在内存中)。因此,基于相机示例,我使用 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?提前致谢。

4

2 回答 2

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);

您需要创建一个位图(您在顶部做了),然后在上面绘制两个矩形,然后将该位图连接到您的视图:

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));
于 2013-05-01T04:02:11.313 回答
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 (看看这个答案)。
这段代码最终绘制了一个矩形,但幸运的是我的图像越来越大:(,但这是另一个问题,所以如果你想关注它,这里是链接

于 2013-06-20T21:17:39.973 回答