2017-01-17 27 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 –

回答