Last active
April 8, 2020 10:59
-
-
Save prakashpun/af618c1dafc62ef2d2943285ba4d909d to your computer and use it in GitHub Desktop.
Initialize TextRecognizer and CameraSource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void startCameraSource() { | |
//Create the TextRecognizer | |
final TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build(); | |
if (!textRecognizer.isOperational()) { | |
Log.w(TAG, "Detector dependencies not loaded yet"); | |
} else { | |
//Initialize camerasource to use high resolution and set Autofocus on. | |
mCameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer) | |
.setFacing(CameraSource.CAMERA_FACING_BACK) | |
.setRequestedPreviewSize(1280, 1024) | |
.setAutoFocusEnabled(true) | |
.setRequestedFps(2.0f) | |
.build(); | |
/** | |
* Add call back to SurfaceView and check if camera permission is granted. | |
* If permission is granted we can start our cameraSource and pass it to surfaceView | |
*/ | |
mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() { | |
@Override | |
public void surfaceCreated(SurfaceHolder holder) { | |
try { | |
if (ActivityCompat.checkSelfPermission(getApplicationContext(), | |
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(MainActivity.this, | |
new String[]{Manifest.permission.CAMERA}, | |
requestPermissionID); | |
return; | |
} | |
mCameraSource.start(mCameraView.getHolder()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
} | |
/** | |
* Release resources for cameraSource | |
*/ | |
@Override | |
public void surfaceDestroyed(SurfaceHolder holder) { | |
mCameraSource.stop(); | |
} | |
}); | |
//Set the TextRecognizer's Processor. | |
textRecognizer.setProcessor(new Detector.Processor<TextBlock>() { | |
@Override | |
public void release() { | |
} | |
/** | |
* Detect all the text from camera using TextBlock and the values into a stringBuilder | |
* which will then be set to the textView. | |
* */ | |
@Override | |
public void receiveDetections(Detector.Detections<TextBlock> detections) { | |
final SparseArray<TextBlock> items = detections.getDetectedItems(); | |
if (items.size() != 0 ){ | |
mTextView.post(new Runnable() { | |
@Override | |
public void run() { | |
StringBuilder stringBuilder = new StringBuilder(); | |
for(int i=0;i<items.size();i++){ | |
TextBlock item = items.valueAt(i); | |
stringBuilder.append(item.getValue()); | |
stringBuilder.append("\n"); | |
} | |
mTextView.setText(stringBuilder.toString()); | |
} | |
}); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment