android-在片段中,如何停止ZXing Scanner的摄像头?
作者:互联网
按照本教程(http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/),我制作了带有片段和ViewPager的Android选项卡.到目前为止,我向其中一个片段添加了QR码扫描仪(使用基于ZXing的条形码扫描仪库),并且工作正常.
问题是当我转到其他选项卡(片段)时,摄像机本身仍在内部工作(内部摄像机不会停止).因此,即使我在屏幕上看不到相机的不同片段,当我将手机(相机部件)放置在QR码附近时,它也会读取并开始新的活动.
那么当我去其他片段时如何停止相机?
这是我的QR码扫描仪的片段.
public class TabFragment1 extends Fragment implements ZXingScannerView.ResultHandler
{
private ZXingScannerView mScannerView;
private LinearLayout qrCameraLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.tab_fragment_1, container, false);
qrCameraLayout = (LinearLayout) v.findViewById(R.id.ll_qrcamera);
mScannerView = new ZXingScannerView(getActivity().getApplicationContext());
mScannerView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
qrCameraLayout.addView(mScannerView);
List<BarcodeFormat> formats = new ArrayList<>();
formats.add(BarcodeFormat.QR_CODE);
mScannerView.setFormats(formats);
return v;
}
@Override
public void onResume()
{
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
@Override
public void onPause()
{
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(final Result result)
{
//handling results
}
解决方法:
我尝试了一些方法来解决此问题,最后得到了解决方案.
public void handleResult(Result result) {
//Hold result
Log.e("handler", result.getText()); // Prints scan results
Log.e("handler", result.getBarcodeFormat().toString()); // Prints the scan format (qrcode)
mScannerView.removeAllViews(); //<- here remove all the views, it will make an Activity having no View
mScannerView.stopCamera(); //<- then stop the camera
setContentView(R.layout.activity_Main); //<- and set the View again.
final String vString = result.getText();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext,vString,Toast.LENGTH_LONG).show();
}
});
// to resume scanning
// mScannerView.resumeCameraPreview(this);<br />
}
}
再次设置ContentView解决了m的问题
标签:android-fragments,android-camera,zxing,qr-code,android 来源: https://codeday.me/bug/20191026/1939447.html