Skip to content

Instantly share code, notes, and snippets.

@doomSDey
Last active July 24, 2020 18:04
Show Gist options
  • Save doomSDey/7a4d79774efe9e7ff03e9305e3e043f6 to your computer and use it in GitHub Desktop.
Save doomSDey/7a4d79774efe9e7ff03e9305e3e043f6 to your computer and use it in GitHub Desktop.
PreviewView mCameraView;
SurfaceHolder holder;
SurfaceView surfaceView;
Canvas canvas;
Paint paint;
int cameraHeight, cameraWidth, xOffset, yOffset, boxWidth, boxHeight;
private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;
private ExecutorService executor = Executors.newSingleThreadExecutor();
/**
* Starting Camera
*/
void startCamera(){
mCameraView = findViewById(R.id.previewView);
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(new Runnable() {
@Override
public void run() {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
MainActivity.this.bindPreview(cameraProvider);
} catch (ExecutionException | InterruptedException e) {
// No errors need to be handled for this Future.
// This should never be reached.
}
}
}, ContextCompat.getMainExecutor(this));
}
/**
*
* Binding to camera
*/
private void bindPreview(ProcessCameraProvider cameraProvider) {
Preview preview = new Preview.Builder()
.build();
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
preview.setSurfaceProvider(mCameraView.createSurfaceProvider());
Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector,preview);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Start Camera
startCamera();
//Create the bounding box
surfaceView = findViewById(R.id.overlay);
surfaceView.setZOrderOnTop(true);
holder = surfaceView.getHolder();
holder.setFormat(PixelFormat.TRANSPARENT);
holder.addCallback(this);
}
/**
*
* For drawing the rectangular box
*/
private void DrawFocusRect(int color) {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = mCameraView.getHeight();
int width = mCameraView.getWidth();
cameraHeight = height;
cameraWidth = width;
int left, right, top, bottom, diameter;
diameter = width;
if (height < width) {
diameter = height;
}
int offset = (int) (0.05 * diameter);
diameter -= offset;
canvas = holder.lockCanvas();
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
//border's properties
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(color);
paint.setStrokeWidth(5);
left = width / 2 - diameter / 3;
top = height / 2 - diameter / 3;
right = width / 2 + diameter / 3;
bottom = height / 2 + diameter / 3;
xOffset = left;
yOffset = top;
boxHeight = bottom - top;
boxWidth = right - left;
//Changing the value of x in diameter/x will change the size of the box ; inversely proportionate to x
canvas.drawRect(left, top, right, bottom, paint);
holder.unlockCanvasAndPost(canvas);
}
/**
* Callback functions for the surface Holder
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
DrawFocusRect(Color.parseColor("#b3dabb"));
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment