目前,我正在使用Google视觉API开发条形码阅读器android应用。单击按钮时,我需要启动相机预览,直到单击按钮,屏幕应该是空白的空白屏幕。当我尝试执行此摄像头预览的同时开始时,屏幕上也出现了如何解决此问题的信息,请帮助我。
我的MainActivity类
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_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>
答案 0 :(得分:2)
最后几天后,我找到了解决方案:)
为此,您必须编辑由Google视觉示例提供的BarcodeCapture.java类。请像下面这样进行编辑。
将这两行添加到BarcodeCapture.java的onCreate方法中
mPreview.setVisibility(View.GONE);
mGraphicOverlay.setVisibility(View.GONE);
在BarcodeCapture.java的startCameraSource();
方法中的和注释onResume()
行
最后将它们添加到BarcodeCapture.java类的refresh
方法中
mPreview.setVisibility(View.VISIBLE);
mGraphicOverlay.setVisibility(View.VISIBLE);
startCameraSource();
答案 1 :(得分:1)
我已经更改了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);
尝试一下..让我知道它是否正常