我正在尝试使用Android中的Google视觉api制作QR码扫描仪。我成功地检测到QR码并获取了内容,我正在使用 Detector 类来检测QR。扫描后,我停止相机预览,并想在扫描的QR周围画一个方框。为此,我从探测器类中获取了扫描QR点(X,Y坐标)。但是这些点与相机源预览中的QR图像不匹配。 Here is an screenshot of scanned QR
下面是当检测到QR时将调用的方法,在这种方法中,我要获取QR码,然后停止摄像头并调用带有QR位置的绘制矩形方法来绘制一个代替QR码的框>
@Override
public void receiveDetections(Detector.Detections<Barcode> detections)
{
final SparseArray<Barcode> qrCodes = detections.getDetectedItems();
final Detector.Detections<Barcode> frame = detections;
if (qrCodes.size() != 0 )
{
scannerDetector.release();
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable()
{
@Override
public void run()
{
cameraSource.stop();//Stopping the camera after a QR is detected
for (int i = 0; i < qrCodes.size(); i++)
{
final int key = qrCodes.keyAt(i);
// this gives the rect co-ordinates of the detected QR
Rect rect = qrCodes.get(key).getBoundingBox();
drawRectangle(rect);
break;
}
}
});
}
}
private void drawRectangle(final Rect rect)
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
// detected frame is a constraint layout and view returned from Draw Rectangle class will be added as its subview
detectedFrame.addView(new DrawRectangle(QRCodeScannerActivity.this, rect));
}
});
}
我在这里用我从探测器获得的矩形坐标绘制一个盒子。
public class DrawRectangle extends View
{
Rect rect;
public DrawRectangle(Context context, Rect rect )
{
super(context);
this.rect = rect;
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint();
//Rectangle
paint.setColor(Color.BLUE);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
try
{
canvas.drawRect(rect,paint);
}
catch (Exception e)
{
}
}
}