2015-09-17 18 views
11

Tôi phát triển máy quét liên tục mã vạch ZXing sau this page trên Android Studio.Máy quét mã vạch ZXing trong bố cục tùy chỉnh trong đoạn

My App build.gradle đã bao gồm:

repositories { 
    mavenCentral() 

    maven { 
     url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/" 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    compile 'com.android.support:appcompat-v7:23.0.1' 
    compile files('src/main/jniLibs/scanditsdk-android-4.7.5.jar') 
    compile files('src/main/jniLibs/httpclient-4.0.jar') 

    compile 'com.journeyapps:zxing-android-embedded:[email protected]' 
    compile 'com.google.zxing:core:3.2.0' 
} 

Fragment.xml tôi 's bố trí:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#00CC00" 
    android:orientation="vertical" 
    android:weightSum="100"> 

    <com.journeyapps.barcodescanner.CompoundBarcodeView 
     android:id="@+id/barcode_scanner" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="40" 
     > 

    </com.journeyapps.barcodescanner.CompoundBarcodeView> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:weightSum="100" 
     style="?android:attr/buttonBarStyle" 
     > 


     <Button 
      android:id="@+id/btnStartScan" 
      android:layout_width="0dp" 
      android:layout_height="50dp" 
      android:layout_weight="25" 
      android:text="Start" 
      android:background="@drawable/buttonstyle" 
      style="@style/button_style"/> 

     <Button 
      android:id="@+id/btnStopScan" 
      android:layout_width="0dp" 
      android:layout_height="50dp" 
      android:layout_weight="25" 
      android:text="Stop" 
      android:background="@drawable/buttonstyle" 
      style="@style/button_style"/> 

     <Button 
      android:id="@+id/btnPauseScan" 
      android:layout_width="0dp" 
      android:layout_height="50dp" 
      android:layout_weight="25" 
      android:text="Pause" 
      android:background="@drawable/buttonstyle" 
      style="@style/button_style"/> 

     <Button 
      android:id="@+id/btnResumeScan" 
      android:layout_width="0dp" 
      android:layout_height="50dp" 
      android:layout_weight="25" 
      android:text="Resume" 
      android:background="@drawable/buttonstyle" 
      style="@style/button_style"/> 
    </LinearLayout> 

</LinearLayout> 

Sau đó, mã nhìn Fragment của tôi như sau:

public class CMCSMOFragment extends Fragment implements View.OnClickListener { 

    private CompoundBarcodeView barcodeView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 

     View v; 
     v = inflater.inflate(R.layout.cmcsmo_layout, container, false); 

     barcodeView = (CompoundBarcodeView) v.findViewById(R.id.barcode_scanner); 
     barcodeView.decodeContinuous(callback); 

     return v; 
    } 

    private BarcodeCallback callback = new BarcodeCallback() { 
     @Override 
     public void barcodeResult(BarcodeResult result) { 
      if (result.getText() != null) { 
       barcodeView.setStatusText(result.getText()); 
      } 

      //Do something with code result 
     } 

     @Override 
     public void possibleResultPoints(List<ResultPoint> resultPoints) { 
     } 
    }; 
} 

Và khi tôi xây dựng ứng dụng của tôi, CompoundBarcodeView chỉ hiển thị chế độ xem màu đen với văn bản ZXing:

Đặt mã vạch bên trong hình chữ nhật kính ngắm để quét.

Edit:

Thực hiện theo Lennon gợi ý, tôi đã sử dụng zxing-minimum nhưng nó không cho phép làm việc trên chế độ Portrait :(

Làm thế nào tôi nên làm gì để giải quyết vấn đề này. Cảm ơn mọi sự giúp đỡ!

+0

tại sao bạn thực hiện view.onClicklistener không có chức năng onclick? –

+0

Lớp học của tôi quá dài nên tôi chỉ đăng mã cho vấn đề của mình. –

Trả lời

7

Nó dễ dàng như vậy, chủ sở hữu của ZXing nói rằng chỉ thêm đoạn mã sau vào onResume và phương pháp overrided:

@Override 
public void onResume() { 
    barcodeView.resume(); 
    super.onResume(); 
} 

@Override 
public void onPause() { 
    barcodeView.pause(); 
    super.onPause(); 
} 
+0

Tôi đã sao chép tất cả mã của bạn nhưng tôi vẫn chỉ nhận được màn hình màu đen –

+1

Vui lòng thực hiện theo 'https: // github.com/journeyapps/zxing-android-embedded' để biết thêm chi tiết. –

1

Hãy thử sử dụng thư viện tối thiểu của zxing như liên kết sau: https://github.com/andreipro/zxing-android-minimal

Thật dễ dàng. Bạn chỉ cần thêm những dòng này vào bạn gradle

repositories { 
    mavenCentral() 

    maven { 
     url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/" 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:support-v13:22.2.0' 

    // Zxing minimal libraries 
    compile 'com.embarkmobile:zxing-android-minimal:[email protected]' 
    compile 'com.embarkmobile:zxing-android-integration:[email protected]' 
    compile 'com.google.zxing:core:3.0.1' 
} 

Và sau đó chỉ cần gọi mã vạch sử dụng này

new IntentIntegrator(this).initiateScan(); // `this` is the current Activity 

Trong bạn trường hợp, bạn muốn sử dụng một cách bố trí tùy chỉnh, vì vậy bạn phải tạo bố trí tùy chỉnh của bạn sau một số thông số, chẳng hạn như cách bố trí bên dưới:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"> 

    <SurfaceView android:id="@+id/zxing_preview_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

    <com.google.zxing.client.android.ViewfinderView 
     android:id="@+id/zxing_viewfinder_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

    <LinearLayout 
     android:id="@+id/zxing_result_view" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@color/zxing_result_view" 
     android:visibility="gone" 
     android:baselineAligned="false"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:padding="@dimen/zxing_standard_padding"> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:gravity="right|center_vertical"> 

       <ImageView android:id="@+id/zxing_barcode_image_view" 
        android:layout_width="160dip" 
        android:layout_height="wrap_content" 
        android:maxWidth="160dip" 
        android:maxHeight="160dip" 
        android:layout_marginBottom="@dimen/zxing_half_padding" 
        android:adjustViewBounds="true" 
        android:scaleType="centerInside" 
        tools:ignore="ContentDescription"/> 

       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <TextView android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/zxing_msg_default_format" 
         android:textColor="@color/zxing_result_minor_text" 
         android:textStyle="bold" 
         android:paddingRight="@dimen/zxing_half_padding"/> 

        <TextView android:id="@+id/zxing_format_text_view" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textColor="@color/zxing_result_minor_text"/> 

       </LinearLayout> 

       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <TextView android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/zxing_msg_default_type" 
         android:textColor="@color/zxing_result_minor_text" 
         android:textStyle="bold" 
         android:paddingRight="@dimen/zxing_half_padding"/> 

        <TextView android:id="@+id/zxing_type_text_view" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textColor="@color/zxing_result_minor_text"/> 

       </LinearLayout> 

       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <TextView android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/zxing_msg_default_time" 
         android:textColor="@color/zxing_result_minor_text" 
         android:textStyle="bold" 
         android:paddingRight="@dimen/zxing_half_padding"/> 

        <TextView android:id="@+id/zxing_time_text_view" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textColor="@color/zxing_result_minor_text"/> 

       </LinearLayout> 

       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <TextView android:id="@+id/zxing_meta_text_view_label" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/zxing_msg_default_meta" 
         android:textColor="@color/zxing_result_minor_text" 
         android:textStyle="bold" 
         android:paddingRight="@dimen/zxing_half_padding"/> 

        <TextView android:id="@+id/zxing_meta_text_view" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textColor="@color/zxing_result_minor_text"/> 

       </LinearLayout> 

      </LinearLayout> 

      <ScrollView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"> 

        <TextView android:id="@+id/zxing_contents_text_view" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textColor="@color/zxing_result_text" 
         android:textColorLink="@color/zxing_result_text" 
         android:textSize="22sp" 
         android:paddingLeft="12dip" 
         android:autoLink="web" 
         android:textIsSelectable="true"/> 

        <TextView android:id="@+id/zxing_contents_supplement_text_view" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textColor="@color/zxing_result_text" 
         android:textColorLink="@color/zxing_result_text" 
         android:paddingLeft="12dip" 
         android:autoLink="web" 
         android:textIsSelectable="true"/> 

       </LinearLayout> 

      </ScrollView> 

     </LinearLayout> 

     <LinearLayout android:id="@+id/zxing_result_button_view" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:gravity="center"> 

      <Button style="@style/zxing_ResultButton" 
       android:visibility="gone"/> 

      <Button style="@style/zxing_ResultButton" 
       android:visibility="gone"/> 

      <Button style="@style/zxing_ResultButton" 
       android:visibility="gone"/> 

      <Button style="@style/zxing_ResultButton" 
       android:visibility="gone"/> 

     </LinearLayout> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_gravity="bottom|center_horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView android:id="@+id/zxing_status_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|center_horizontal" 
      android:background="@color/zxing_transparent" 
      android:text="@string/zxing_msg_default_status" 
      android:textColor="@color/zxing_status_text"/> 

     <Button android:id="@id/zxing_back_button" 
      android:layout_marginTop="10dp" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:height="60dp" 
      android:textAlignment="center" 
      android:layout_gravity="bottom|center_horizontal" 
      android:text="@string/zxing_button_cancel"/> 

    </LinearLayout> 

</merge> 

sau đó, bạn có thể thiết lập cách bố trí trên vào integrator như

IntentIntegrator integrator = new IntentIntegrator(this); 
integrator.setCaptureLayout(R.layout.custom_layout); 
integrator.initiateScan(); 

Nhưng hãy nhớ, bạn phải tuân theo các thông số đó. Nó có nghĩa là bạn phải sử dụng cùng một tên cho tất cả các khung nhìn.

+0

Tôi đã làm điều này. Cảm ơn nhiều. Nhưng tôi có thể sử dụng ZXing tối thiểu trên màn hình Chân dung không? –

+0

Không may là tôi đã cố gắng sử dụng nó trên Portrait, nhưng không thành công. –

+0

Cảm ơn bạn rất nhiều vì đã dành thời gian, bro ^^ –

0

Liệu ứng dụng của bạn đã được phép sử dụng camera của điện thoại? Thêm quyền đó vào tệp kê khai của bạn và sau đó sau khi cài đặt ứng dụng, hãy đi tới cài đặt điện thoại> ứng dụng> ứng dụng của bạn> quyền. Và sau đó cấp quyền cho máy ảnh

Các vấn đề liên quan