我正在通过谷歌开展 ocr-vision 工作。我可以检测 OcrGraphic.java 中没有扩展Activity的文本,我想将提取的值发送到一个活动文件,该活动文件包含特定文本的编辑文本,当它进入时timer run()方法它会在null引用上抛出空指针异常。
我的代码被删除了:
OcrGraphic.java
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;
import android.widget.Toast;
import com.example.amirfirdaus.mrztutorial.Model.SkanIkadValues;
import com.example.amirfirdaus.mrztutorial.ScanIkadResult;
import com.google.android.gms.vision.text.Line;
import com.google.android.gms.vision.text.Text;
import com.google.android.gms.vision.text.TextBlock;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
* Graphic instance for rendering TextBlock position, size, and ID within an
associated graphic
* overlay view.
*/
public class OcrGraphic extends GraphicOverlay.Graphic {
private static Context context;
private int mId;
private static final int TEXT_COLOR = Color.WHITE;
private static Paint sRectPaint;
private static Paint sTextPaint;
private final TextBlock mText;
private boolean isBirthday;
private boolean isExpiry;
private boolean isPassport;
String birthday = "";
String expiry = "";
String passport = "";
OcrGraphic(GraphicOverlay overlay, TextBlock text) {
super(overlay);
mText = text;
if (sRectPaint == null) {
sRectPaint = new Paint();
sRectPaint.setColor(TEXT_COLOR);
sRectPaint.setStyle(Paint.Style.STROKE);
sRectPaint.setStrokeWidth(4.0f);
}
if (sTextPaint == null) {
sTextPaint = new Paint();
sTextPaint.setColor(TEXT_COLOR);
sTextPaint.setTextSize(54.0f);
}
// Redraw the overlay, as this graphic has been added.
postInvalidate();
}
public static Context getContext() {
return context;
}
public int getId() {
return mId;
}
public void setId(int id) {
this.mId = id;
}
public TextBlock getTextBlock() {
return mText;
}
/**
* Checks whether a point is within the bounding box of this graphic.
* The provided point should be relative to this graphic's containing overlay.
*
* @param x An x parameter in the relative context of the canvas.
* @param y A y parameter in the relative context of the canvas.
* @return True if the provided point is contained within this graphic's bounding box.
*/
public boolean contains(float x, float y) {
if (mText == null) {
return false;
}
RectF rect = new RectF(mText.getBoundingBox());
rect.left = translateX(rect.left);
rect.top = translateY(rect.top);
rect.right = translateX(rect.right);
rect.bottom = translateY(rect.bottom);
return (rect.left < x && rect.right > x && rect.top < y && rect.bottom > y);
}
/**
* Draws the text block annotations for position, size, and raw value on the supplied canvas.
*/
@Override
public void draw(final Canvas canvas)
{
if (mText == null) {
return;
}
// Draws the bounding box around the TextBlock.
RectF rect = new RectF(mText.getBoundingBox());
rect.left = translateX(rect.left);
rect.top = translateY(rect.top);
rect.right = translateX(rect.right);
rect.bottom = translateY(rect.bottom);
canvas.drawRect(rect, sRectPaint);
// Break the text into multiple lines and draw each one according to its own bounding box.
List<Line> lineComponents;
lineComponents = (List<Line>) mText.getComponents();
//List<? extends Text> textComponents = mText.getComponents();
for (Text currentText : lineComponents) {
float left = translateX(currentText.getBoundingBox().left);
float bottom = translateY(currentText.getBoundingBox().bottom);
canvas.drawText(currentText.getValue(), left, bottom, sTextPaint);
if (!(currentText.getValue() == null))
{
Log.e("OCrGraphic", "Text detected! " + currentText.getValue());
if (isBirthday == false && !currentText.getValue().contains("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") || currentText.getValue().startsWith("0") && currentText.getValue().contains("+-0123456789/-#")) {
Log.e("currentTextBirthday", currentText.getValue());
birthday = "";
birthday = currentText.getValue();
isBirthday = true;
SkanIkadValues.setBirthday(birthday);
// Log.e("OCrGraphic", "Text detected! " + currentText.getValue());
}
if (isExpiry == false && !currentText.getValue().contains("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") || currentText.getValue().startsWith("0") && currentText.getValue().contains("+-0123456789/-#")) {
Log.e("currentTextExpiry", currentText.getValue());
expiry = "";
expiry = currentText.getValue();
isExpiry = true;
SkanIkadValues.setExpiry(expiry);
// Log.e("OCrGraphic", "Text detected! " + currentText.getValue());
}
if (isPassport == false && currentText.getValue().contains("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") || currentText.getValue().startsWith("0") && currentText.getValue().contains("+-0123456789/-#")) {
Log.e("currentTextPassport", currentText.getValue());
passport = "";
passport = currentText.getValue();
isPassport = true;
SkanIkadValues.setPassport(passport);
// Log.e("OCrGraphic", "Text detected! " + currentText.getValue());
}
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// if(!expiry.equals("") && !birthday.equals("") && !passport.equals(""))
Intent next = new Intent(getContext(),ScanIkadResult.class);
next.putExtra("passport", passport);
next.putExtra("expiry", expiry);
next.putExtra("birthday", birthday);
next.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.putStringArrayListExtra("contactsList",stringList);
context.startActivity(next);
/* Log.e("CBname",skanIkadValues.getName());*/
}
},2000,2000);
}
}
}
/* private static class ApplicationController {
public static Context getContext() {
return context;
}*/
}
答案 0 :(得分:2)
因为您从未为class New(View):
template_name = 'webapp/new.html'
form_class = SofaProjectForm
def get(self,request):
form = self.form_class(None)
return render(request,'webapp/new.html',{'form':form})
def post(self,request):
if request.method == 'POST':
form = self.form_class(request.POST)
code = request.POST['code']
run = runcode.RunPyCode(code)
rescompil, resrun = run.run_py_code()
resrun=resrun
rescomp=rescompil
if not resrun:
resrun = 'No result!'
else:
code = default_py_code
resrun = 'No result!'
rescompil = "No compilation for Python"
return render (request,'webapp/new.html', {'code':code,'resrun':resrun,'rescomp':rescomp})
分配值。您可以将构造函数修改为context
作为参数,而不是使用static
上下文。
Context
由于 public class OcrGraphic extends GraphicOverlay.Graphic {
private Context context;
OcrGraphic(GraphicOverlay overlay, TextBlock text, Context context) {
super(overlay);
this.context = context;
}
}
不是GraphicOverlay.Graphic
,您无法直接使用View
。