我正在试验zxing barcodefraglibv2。我将它作为一个模块导入,经过多次失败后,我开始了一个新项目并编译了提供的示例活动。我最终得到的结果与我在样本周围建立的样本相同 - 应用程序启动但是应该有准备扫描的相机屏幕,我得到一个黑屏。在我的logcat中,我收到此错误:连接到摄像头时出错:0,以及通常的数据转储。如果有人需要,我很乐意提供整个转储。我试过在Nexus 5和Nexus 7上运行该设备。再次,这是随包提供的代码,所以我不知道发生了什么。
感谢您的帮助。
MainActivity:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.abhi.barcode.frag.libv2.BarcodeFragment;
import com.abhi.barcode.frag.libv2.IScanResultHandler;
import com.abhi.barcode.frag.libv2.ScanResult;
public class MainActivity extends AppCompatActivity implements IScanResultHandler {
BarcodeFragment brf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
brf = new BarcodeFragment();
trx.add(R.id.testFragmentHolder, brf, "HOLDER");
trx.commit();
//brf = (BarcodeFragment) getSupportFragmentManager().findFragmentById(R.id.barcodeFragment);
brf.setDecodeFor(MODE.ONE_D_MODE);
brf.setScanResultHandler(this);
}
@Override
public void scanResult(ScanResult result) {
Toast.makeText(this, result.getRawResult().getText(), Toast.LENGTH_LONG).show();
try{
brf.restart();
}catch (Exception e) {
e.printStackTrace();
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/testFragmentHolder"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.valdes.fuckingbarcode.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="198dp"
android:text="Scan" />
<fragment
android:id="@+id/barcodeFragment"
android:name="com.abhi.barcode.frag.libv2.BarcodeFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp" />
</RelativeLayout>
清单:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.valdes.barcode">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Don't require camera, as this requires a rear camera. This allows it to work on the Nexus 7 -->
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<!-- TODO replace above two with next line after Android 4.2 -->
<!-- <uses-feature android:name="android.hardware.camera.any"/> -->
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.flash"
android:required="false" />
<uses-feature android:name="android.hardware.screen.landscape" />
<uses-feature
android:name="android.hardware.wifi"
android:required="false" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
想出来。 API 23需要在运行时请求权限。两个修复:
1)在运行时请求延迟。你可以在这里阅读:https://developer.android.com/training/permissions/requesting.html
2)在API 22编译。
仍有一些问题,但这是对这一特定问题的解决方法。