public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
static AtomicBoolean surfaceExists = new AtomicBoolean(false);
private static final int CAMERA_REQUEST_ID = 2122;
public static final String mTextBlock = "String";
private SurfaceHolder mSurfaceHolder;
Camera mCamera;
Context mContext;
SurfaceView mCameraView;
TextView mTextView;
CameraSource mCameraSource;
CameraInfo mCameraInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mTextView = (TextView)findViewById(R.id.text_view);
mCameraView = (SurfaceView)findViewById(R.id.surfaceView);
startCameraSource();
}
private void startCameraSource() {
TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();
if (textRecognizer.isOperational()) {
mCameraSource = new CameraSource.Builder(this, textRecognizer)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedPreviewSize(1280, 1024)
.setRequestedFps(2.0f)
.setAutoFocusEnabled(true)
.build();
mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA},CAMERA_REQUEST_ID);
return;
}
mCameraSource.start(mCameraView.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
mCameraSource.stop();
}
});
textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
final SparseArray<TextBlock> items = detections.getDetectedItems();
mTextView.post(new Runnable() {
@Override
public void run() {
if (items.size() == 0) {
mTextView.setText("");
mTextView.setVisibility(View.INVISIBLE);
} else {
StringBuilder stringBuilder = new StringBuilder();
TextBlock text = null;
for (int i = 0; i < items.size(); i++) {
if ((i < items.size()) && (i < 10)) {
String str = ((TextBlock)items.valueAt(i)).getValue().replaceAll("[^0-9]", "");
Log.d("tag", "Value: " + str);
if ((str.length() == 16) || (str.length() == 19)) { //read card No.
BA.Log("Value: " + str);
Intent mIntent = new Intent();
mIntent.putExtra(mTextBlock, str);
MainActivity.this.setResult(0, mIntent);
items.clear();
finish(); // close activaty
}
}
else
{
return;
}
}
mTextView.setText(stringBuilder.toString());
mTextView.setVisibility(View.VISIBLE);
}
}
});
}
});
} else {
Log.d(getClass().getSimpleName(), "Text Recognizer isn't ready yet");
}
}
}