按照本教程(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
}
答案 0 :(得分:4)
我已经尝试过某种方法来解决这个问题,最后得到了解决方案。
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
答案 1 :(得分:0)
尝试此代码,如果您仍然遇到任何问题,请告知我们
public class TabFragment1 extends Fragment implements ZXingScannerView.ResultHandler
{
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
else if(mScannerView != null) {
mScannerView.stopCamera();
}
}
}
答案 2 :(得分:0)
使用TabFragment1
启动StartActivityForResult()
活动,然后处理结果并停止onActivityResult()