2017-01-17 29 views
0

我想制作一个QR码扫描仪,在一个片段中打开,我正在使用zxing库来这样做。现在我可以成功打开相机扫描QR码了。但是,因为我正在使用意图打开相机,所以它会打开另一个活动。我想要做的是在片段内打开相机,并只在屏幕的中间部分。我相信表面观和摄像头预览可以帮助我这样做,但我不知道如何实现它打开相机在屏幕中间的Android QR代码

public class QRscanner extends Fragment { 
private IntentIntegrator qrScan; 
public QRscanner() { 
} 
public static QRscanner newInstance(String text){ 
    Bundle args = new Bundle(); 
    QRscanner qrScanner = new QRscanner(); 
    qrScanner.setArguments(args); 
    return qrScanner; 
} 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.qr_scanner, container, false); 
    IntentIntegrator qrScan = new IntentIntegrator(getActivity()); 
    qrScan.initiateScan(); 
    return rootView; 
} 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
    if (result != null) { 
     //if qrcode has nothing in it 
     if (result.getContents() == null) { 
      Toast.makeText(getActivity(), "Result Not Found", Toast.LENGTH_LONG).show(); 
     } else { 
      //if qr contains data 
      try { 
       //converting the data to json 
       JSONObject obj = new JSONObject(result.getContents()); 
       String testing = obj.getString("test"); 
       System.out.println(testing); 
       //setting values to textviews 
      } catch (JSONException e) { 
       e.printStackTrace(); 
       //if control comes here 
       //that means the encoded format not matches 
       //in this case you can display whatever data is available on the qrcode 
       //to a toast 
       Toast.makeText(getActivity(), result.getContents(), Toast.LENGTH_LONG).show(); 
      } 
     } 
    } else { 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
} 
} 

the dark block is the camera and the white is the rest of the screen

+0

您可以使用以下库:https://github.com/nipun-birla/QRReaderView –

回答