Android 谷歌移动视觉文本API-阅读特定的文本,在一个纠结
我正在使用Google mobile vision文本API,使用提供的API阅读文本 我的用例: 最初,我在视图表面绘制一个可重新调整大小的矩形。 限制google API只能识别矩形中的文本Android 谷歌移动视觉文本API-阅读特定的文本,在一个纠结,android,Android,我正在使用Google mobile vision文本API,使用提供的API阅读文本 我的用例: 最初,我在视图表面绘制一个可重新调整大小的矩形。 限制google API只能识别矩形中的文本 public class GraphicOverlay<T extends GraphicOverlay.Graphic> extends View { protected void onDraw(Canvas canvas) { su
public class GraphicOverlay<T extends GraphicOverlay.Graphic> extends View
{
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
synchronized (mLock)
{
if ((mPreviewWidth != 0) && (mPreviewHeight != 0))
{
mWidthScaleFactor = (float) canvas.getWidth() / (float) mPreviewWidth;
mHeightScaleFactor = (float) canvas.getHeight() / (float) mPreviewHeight;
}
for (Graphic graphic : mGraphics)
{
graphic.draw(canvas);
}
paint.setColor(Color.parseColor("#55000000"));
paint.setStyle(Paint.Style.FILL);
paint.setStrokeJoin(Paint.Join.ROUND);
// mPaint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(5);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#55FFFFFF"));
if (groupId == 1) {
mCurrentRect= new Rect(point2.x+colorballs.get(1).getWidthOfBall() / 2,
point4.y +colorballs.get(1).getWidthOfBall() / 2,
point4.x+colorballs.get(1).getWidthOfBall() / 2,
point2.y+colorballs.get(1).getWidthOfBall() / 2);
canvas.drawRect(mCurrentRect, paint);
} else {
mCurrentRect= new Rect(point2.x + colorballs.get(1).getWidthOfBall() / 2,
point4.y + colorballs.get(3).getWidthOfBall() / 2, point4.x
+ colorballs.get(3).getWidthOfBall() / 2, point2.y
+ colorballs.get(1).getWidthOfBall() / 2);
canvas.drawRect(mCurrentRect, paint);
}
BitmapDrawable mBitmap;
mBitmap = new BitmapDrawable();
// draw the balls on the canvas`enter code here`
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(),
new Paint());
}
}
}
}
class CustomTextRecognizer extends Detector<TextBlock> {
private Detector<TextBlock> mDelegate;
private GraphicOverlay<OcrGraphic> mOcrGraphicOverlay;
CustomTextRecognizer(Detector<TextBlock> delegate, GraphicOverlay<OcrGraphic> ocrGraphicOverlay) {
mDelegate = delegate;
mOcrGraphicOverlay= ocrGraphicOverlay;
}
@Override
public void receiveFrame(Frame frame) {
Bitmap bt= frame.getBitmap();
super.receiveFrame(frame);
}
public SparseArray<TextBlock> detect(Frame frame) {
//How to compare the items that is inside my rectangle.
}
}
公共类GraphicOverlay扩展视图
{
受保护的void onDraw(画布)
{
super.onDraw(帆布);
已同步(mLock)
{
如果((mPreviewWidth!=0)和&(mPreviewWidth!=0))
{
mWidthScaleFactor=(float)canvas.getWidth()/(float)mPreviewWidth;
mHeightScaleFactor=(float)canvas.getHeight()/(float)mPreviewHeight;
}
用于(图形:mGraphics)
{
图形绘制(画布);
}
paint.setColor(Color.parseColor(#55000000”);
绘制.设置样式(绘制.样式.填充);
绘制.设置行程连接(绘制.连接.圆形);
//mPaint.setStrokeCap(油漆盖圆形);
油漆。设置行程宽度(5);
帆布.拉丝漆(油漆);
paint.setColor(Color.parseColor(#55FFFFFF”);
if(groupId==1){
mCurrentRect=new Rect(point2.x+colorbolls.get(1).getWidthOfBall()/2,
point4.y+colorbolls.get(1.getWidthOfBall()/2,
point4.x+colorbolls.get(1.getWidthOfBall()/2,
点2.y+colorbolls.get(1.getWidthOfBall()/2);
画布.drawRect(MCurrect,油漆);
}否则{
mCurrentRect=new Rect(point2.x+colorbolls.get(1).getWidthOfBall()/2,
point4.y+colorbolls.get(3.getWidthOfBall()/2,point4.x
+colorbolls.get(3.getWidthOfBall()/2,point2.y
+get(1.getWidthOfBall()/2);
画布.drawRect(MCurrect,油漆);
}
位图可绘制mBitmap;
mBitmap=新的BitmapDrawable();
//在画布上绘制球`在此处输入代码`
用于(彩色球:彩色球){
canvas.drawBitmap(ball.getBitmap(),ball.getX(),ball.getY(),
新油漆());
}
}
}
}
类CustomTextRecognitor扩展检测器{
专用探测器;
私有图形覆盖;
CustomTextRecognitor(检测器代理、GraphicOverlay和GraphicOverlay){
mDelegate=代表;
mOcrGraphicOverlay=ocrGraphicOverlay;
}
@凌驾
公共无效接收框架(框架){
位图bt=frame.getBitmap();
超级接收帧(帧);
}
公共Sparsarray检测(帧){
//如何比较矩形内的项目。
}
}
/*
*此方法将在每次块检测之前调用,并仅在以下情况下使用有效检测进行解析:
*在提供的矩形中扫描。
*/
公共Sparsarray检测(帧){
SparseArray validDetections=新SparseArray();
Rect overlayRectangle=mOcrGraphicOverlay.getCurrentRectangle();
SparseArray detectedItems=mDelegate.detect(帧);
对于(int index=0;index(boundingBox.top+boundingBox.height()){
validDetections.put(索引,detectedBlock);
}
}
返回有效检测;
}
找到一个解决方案。请建议如何使用矩形覆盖限制检测
/*
* This method will call before each block detection and resolves only with valid detection that
* scanned with in the rectangle provided.
*/
public SparseArray<TextBlock> detect(Frame frame) {
SparseArray<TextBlock> validDetections = new SparseArray<TextBlock>();
Rect overlayRectangle = mOcrGraphicOverlay.getCurrentRectangle();
SparseArray<TextBlock> detectedItems = mDelegate.detect(frame);
for (int index = 0; index < detectedItems.size(); ++index) {
TextBlock detectedBlock = detectedItems.valueAt(index);
Rect boundingBox = detectedBlock.getBoundingBox();
// mOcrGraphicOverlay.getColorball(4).setX(overlayRectangle.left);
// mOcrGraphicOverlay.getColorball(4).setY(overlayRectangle.top);
/* if(((overlayRectangle.top <boundingBox.top) &&
(overlayRectangle.top+overlayRectangle.height())> (boundingBox.top +boundingBox.height())) &&
((overlayRectangle.left <boundingBox.left) &&
(overlayRectangle.left+overlayRectangle.width())> (boundingBox.left +boundingBox.width())))*/
mOcrGraphicOverlay.getColorball(4).setX(boundingBox.left);
mOcrGraphicOverlay.getColorball(4).setY(boundingBox.top);
if ((overlayRectangle.top < boundingBox.top) &&
(overlayRectangle.top + overlayRectangle.height()) > (boundingBox.top + boundingBox.height())) {
validDetections.put(index, detectedBlock);
}
}
return validDetections;
}