代码之家  ›  专栏  ›  技术社区  ›  Dulanga

谷歌视觉API条码扫描器删除相机视图

  •  -2
  • Dulanga  · 技术社区  · 8 年前

    目前,我正在开发一个条码阅读器android应用程序与谷歌视觉API。我需要启动相机预览时,按钮被点击,直到按钮被点击屏幕应该是空的白色屏幕。当我尝试做这个相机预览的同时,屏幕上也出现了如何解决这个问题请帮助我。

    Screen before Start scanner

    Screen After start Scanner

    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() {
    
    }
    

    我的activity\u main.xml文件

       <?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>
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Ümañg ßürmån    8 年前

    我已经更改了xml文件的某些部分:请尝试此操作。

    将整个XML替换为:

    <?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" />
    

    更改后:

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

    barcodeCapture.setShowDrawRect(false);
    

    试试看。。如果可以的话告诉我

        2
  •  1
  •   Dulanga    8 年前

    几天后,我终于找到了解决办法:)

    将这两行代码添加到BarcodeCapture.java的onCreate方法中

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

    startCameraSource(); 排队 onResume() BarcodeCapture.java方法

    refresh BarcodeCapture.java类中的方法

       mPreview.setVisibility(View.VISIBLE);
       mGraphicOverlay.setVisibility(View.VISIBLE);
       startCameraSource();