我必须在我的项目中使用QR扫描仪:
目前我正在使用QRCodeReaderView:
但是这个库的问题是:
我得到了
java.lang.RuntimeException:无法连接到摄像头服务 例外
在Android 6.0中,如此问题中所述:Android 6.0 RuntimeException: Fail to connect to camera service
正如答案所示,我试图将目标SDK版本降级到22,它工作正常。
但是我无法在Playstore中更新我的应用,因为它拒绝让我更新以前版本的目标SDK版本较低的应用。
我还试图运行相同 QRCodeReaderView 的演示应用程序,它也会在我的Marshmallow设备中崩溃。正如here所述。我也尝试在我自己的github上编译代码,但它仍然崩溃。
我试图寻找其他一些qr代码阅读器,但他们似乎已经放了"棉花糖许可"在待办事项列表中。
那么您可以建议任何能够很好地使用Marshmallow权限的库,或者提供有关使用同一个库的帮助吗?
答案 0 :(得分:1)
好的,我使用过QrCode,它可以很好地适用于所有设备。
<uses-permission android:name="android.permission.CAMERA" />
我的Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.gujrat.quiz.qrcodereader"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha5'
compile 'me.dm7.barcodescanner:zxing:1.8.4'
compile 'cn.pedant.sweetalert:library:1.3'
compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
compile 'com.android.support:recyclerview-v7:24.0.+'
testCompile 'junit:junit:4.12'
}
请只看一下阅读按钮和权限
package com.gujrat.quiz.qrcodereader;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.activeandroid.query.Select;
import com.activeandroid.query.Update;
import java.util.List;
import java.util.concurrent.ExecutionException;
public class MenuActivity extends AppCompatActivity {
private String[] qrDescription = {"01234", "56789", "02468", "13579"};
private static final int MY_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button read = (Button) findViewById(R.id.read);
Button show = (Button) findViewById(R.id.show);
Button clear= (Button) findViewById(R.id.clear);
if (getAll().size() == 0) {
saveInitialDataToDatabase();
} else {
// Toast.makeText(this, getAll().size() + "", Toast.LENGTH_SHORT).show();
}
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_REQUEST_CODE);
} else {
startActivity(new Intent(MenuActivity.this, MainActivity.class));
}
} else {
startActivity(new Intent(MenuActivity.this, MainActivity.class));
}
}
});
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MenuActivity.this, ShowReadQrCodeActivity.class));
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
List<QRDataHelper> scannedProductList = getAlreadyScannedList();
for (int i = 0; i < scannedProductList.size(); i++) {
updateDatabase(scannedProductList.get(i));
}
Toast.makeText(MenuActivity.this, "List Cleared", Toast.LENGTH_SHORT).show();
}catch (Exception e){
Toast.makeText(MenuActivity.this, "List Empty", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
startActivity(new Intent(MenuActivity.this, MainActivity.class));
} else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}
}
private void saveInitialDataToDatabase() {
try {
for (int i = 0; i < qrDescription.length; i++) {
QRDataHelper qrDataHelper = new QRDataHelper();
qrDataHelper.setProduct_id(i + "");
qrDataHelper.setProduct_description(qrDescription[i]);
qrDataHelper.setProduct_used("0");
qrDataHelper.save();
}
} finally {
//
}
}
public static List<QRDataHelper> getAll() {
return new Select()
.from(QRDataHelper.class)
.where("1")
.execute();
}
public static List<QRDataHelper> getAlreadyScannedList() {
return new Select()
.from(QRDataHelper.class)
.where("product_used = 1")
.execute();
}
public static void updateDatabase(QRDataHelper qrDataHelper) {
new Update(QRDataHelper.class)
.set("product_used = 0")
.where("product_id = ?", qrDataHelper.getProduct_id())
.execute();
}
}
调用QrReaderFragment的活动
package com.gujrat.quiz.qrcodereader;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
openFragment(new ScannerFragment());
}
public void openFragment(Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.container,
fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commitAllowingStateLoss();
}
}
你的QrCodeReader片段
package com.gujrat.quiz.qrcodereader;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.activeandroid.query.Select;
import com.activeandroid.query.Update;
import com.google.zxing.Result;
import java.util.List;
import cn.pedant.SweetAlert.SweetAlertDialog;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScannerFragment extends MasterFragment implements ZXingScannerView.ResultHandler {
private static final String TAG = ScannerFragment.class.getSimpleName();
private String mReadText;
private ZXingScannerView mScannerView;
public ScannerFragment() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mScannerView = new ZXingScannerView(getActivity()); // Programmatically initialize the scanner view
return mScannerView;
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
public void handleResult(Result rawResult) {
mReadText = rawResult.getText();
Log.v(TAG, "qrcode rawresult: " + rawResult.getText()); // Prints scan results
Log.v(TAG, "qrcode format: " + rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
}
private void showSuccessDialog() {
SweetAlertDialog dialog = new SweetAlertDialog(mContext, SweetAlertDialog.SUCCESS_TYPE)
.setTitleText("Done!")
.setContentText("Thank you for using service")
.setCancelText("Exit!")
.setConfirmText("Use again!")
.showCancelButton(true)
.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sDialog) {
sDialog.dismiss();
getActivity().finish();
}
})
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
sweetAlertDialog.dismiss();
resumeScanView();
}
});
dialog.setCancelable(false);
dialog.show();
}
private void resumeScanView() {
mReadText = "";
// Note:
// * Wait 2 seconds to resume the preview.
// * On older devices continuously stopping and resuming camera preview can result in freezing the app.
// * I don't know why this is the case but I don't have the time to figure out.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScannerView.resumeCameraPreview(ScannerFragment.this);
}
}, 2000);
}
private void startBeepSound() {
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);
}
@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
}
}