Currently I am developing a bar code reader android app with Google vision API. I need to start camera preview when button is clicked and until the button is clicked screen should be empty white color screen. When I Try to do this camera preview starts at the same time screen also appears how to solve this problem please help me.
My MainActivity Class
public class MainActivity extends AppCompatActivity implements BarcodeRetriever {
// use a compound button so either checkbox or switch widgets work.
private static final String TAG = "BarcodeMain";
BarcodeCapture barcodeCapture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barcodeCapture = (BarcodeCapture) getSupportFragmentManager().findFragmentById(barcode);
barcodeCapture.setRetrieval(this);
findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
barcodeCapture.setShowDrawRect(true)
.setSupportMultipleScan(false)
.setTouchAsCallback(true)
.shouldAutoFocus(true)
.setShowFlash(true)
.setBarcodeFormat(Barcode.ALL_FORMATS)
.setCameraFacing(false ? CameraSource.CAMERA_FACING_FRONT : CameraSource.CAMERA_FACING_BACK)
.setShouldShowText(true);
barcodeCapture.resume();
barcodeCapture.refresh(true);
}
});
}
@Override
public void onRetrieved(final Barcode barcode) {
Log.d(TAG, "Barcode read: " + barcode.displayValue);
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("code retrieved")
.setMessage(barcode.displayValue);
builder.show();
}
});
barcodeCapture.stopScanning();
}
@Override
public void onRetrievedMultiple(final Barcode closetToClick, final List<BarcodeGraphic> barcodeGraphics) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String message = "Code selected : " + closetToClick.displayValue + "\n\nother " +
"codes in frame include : \n";
for (int index = 0; index < barcodeGraphics.size(); index++) {
Barcode barcode = barcodeGraphics.get(index).getBarcode();
message += (index + 1) + ". " + barcode.displayValue + "\n";
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("code retrieved")
.setMessage(message);
builder.show();
}
});
}
@Override
public void onBitmapScanned(SparseArray<Barcode> sparseArray) {
for (int i = 0; i < sparseArray.size(); i++) {
Barcode barcode = sparseArray.valueAt(i);
Log.e("value", barcode.displayValue);
}
}
@Override
public void onRetrievedFailed(String reason) {
}
@Override
public void onPermissionRequestDenied() {
}
}
My activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/actions"
android:layout_weight="1"
android:gravity="center">
<fragment
android:id="@+id/barcode"
android:name="com.google.android.gms.samples.vision.barcodereader.BarcodeCapture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:gvb_auto_focus="true"
app:gvb_code_format="code_39|aztec"
app:gvb_flash="false"
app:gvb_rect_colors="@array/rect_color" />
</RelativeLayout>
<LinearLayout
android:id="@+id/actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:paddingLeft="16dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:padding="20dp"
android:paddingRight="16dp">
<RelativeLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:background="@drawable/circle_layout_for_start_scan">
</RelativeLayout>
</LinearLayout>
</RelativeLayout>