[英]QR code AND zbar in Android
我的qrreader有問題。
我只需要在中心屏幕上掃描。
我需要擺正地方,在掃描代碼之前必須在其中放置二維碼。
我嘗試在FrameLayout中添加邊距,但是框架上有黑屏。
這是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
>
</FrameLayout>
<TextView
android:id="@+id/scanText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Scanning..."
android:layout_marginTop="20dp"
android:textColor="@android:color/white"
android:textSize="21sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/prv" />
<Button
android:id="@+id/ScanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:visibility="invisible"
android:text="Scan" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button"
android:onClick="turnFlash"
/>
</RelativeLayout>
和我要掃描的代碼:
PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0 && !is_checking) {
is_checking = true;
String text="";
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
text = sym.getData();
}
if(text.substring(0, 3).matches("htt|www")){
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
scanText.setText(text);
v.vibrate(500);
try{
if(text.substring(0, 3).equalsIgnoreCase("www")){
scanText.setText("http://"+scanText.getText().toString());
}
startWebsite(scanText.getText().toString());
}catch(Exception e){
}
}
is_checking = false;
}
}
};
我正在使用Zbar庫。
看看我的應用程序JustScan(可以在code.google.com上作為eclipse項目找到),在這里我使用了與您相同的示例代碼,並根據需要對其進行了修改。
這個想法是重寫CameraPreview類的draw(Canvas canvas)方法,您可以在其中為掃描區域繪制矩形。 為了確保您的重寫方法被調用,您必須在surfaceCreated(...)方法中調用setWillNotDraw(false)。
要將預覽圖像裁剪到掃描區域,請先使用圖像方法setCrop(...)裁剪條形碼圖像,然后再將其傳遞給PreviewCallback函數中onPreviewFrame(...)方法中的scanImage()。
很抱歉,我的解釋很簡短,但是我無法在此處列出完整的代碼。 希望無論如何都會有幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.