-2

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.

Screen before Start scanner

Screen After start Scanner

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>
Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
Dulanga
  • 921
  • 11
  • 23

2 Answers2

1

I've changed some parts of the xml file: Please try this.

Replace your entire XML with:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">

<fragment
    android:id="@+id/barcode"
    android:name="com.google.android.gms.samples.vision.barcodereader.BarcodeCapture"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="10dp"
    app:gvb_auto_focus="true"
    app:gvb_code_format="code_39|aztec"
    app:gvb_flash="false"
    app:gvb_rect_colors="@array/rect_color"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<RelativeLayout
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#FFFFFF"
    app:layout_constraintBottom_toBottomOf="@+id/barcode"
    app:layout_constraintTop_toTopOf="@+id/barcode" >

    <TextView
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="Press Start Button To Activate The Scanner"
        android:layout_centerInParent="true"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="18sp"
        android:gravity="center"/>

</RelativeLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="40dp"
    android:layout_marginStart="40dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

After changing:

findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        RelativeLayout rel = findViewById(R.id.relative);
        rel.setVisibility(View.GONE);

        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);
    }
});

Before the above change, write this:

barcodeCapture.setShowDrawRect(false);

Try it.. and let me know if it works fine

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
1

Finally after few days I found a solution for this :)

To do this you have to edit BarcodeCapture.java class which provide by google vision sample. Please edit it like bellow.

add these Two line into onCreate method of BarcodeCapture.java

   mPreview.setVisibility(View.GONE);
   mGraphicOverlay.setVisibility(View.GONE);

and Comment startCameraSource(); line in the onResume() method of BarcodeCapture.java

finally add these into refresh method in BarcodeCapture.java class

   mPreview.setVisibility(View.VISIBLE);
   mGraphicOverlay.setVisibility(View.VISIBLE);
   startCameraSource();
Dulanga
  • 921
  • 11
  • 23