如何在Android(ZBAR)中设置条形码扫描仪的高度和宽度

时间:2014-09-24 10:54:10

标签: android android-activity view size zbar

Frnds我是android的新手,试图使用ZBAR Library创建一个扫描仪应用程序,我希望相机扫描仪打开所需的大小(高度和宽度),我不知道如何设置。 我用这篇文章发布了编码。 帮我打开相机(扫描仪)以打开所需的大小。 在此先感谢

 package com.dm.zbar.android.examples;

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.graphics.Color;
 import android.hardware.Camera;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.view.Gravity;
 import android.view.View;
 import android.widget.FrameLayout;
 import android.widget.TextView;
 import android.widget.Toast;
 import com.dm.zbar.android.scanner.ZBarConstants;
 import com.dm.zbar.android.scanner.ZBarScannerActivity;

      import net.sourceforge.zbar.Config;
      import net.sourceforge.zbar.ImageScanner;
      import net.sourceforge.zbar.Symbol;

      public class MainActivity extends Activity 
      {

          private static final int ZBAR_SCANNER_REQUEST = 0;
          private static final int ZBAR_QR_SCANNER_REQUEST = 1;

          @Override
               public void onCreate(Bundle savedInstanceState) 
          {
                   super.onCreate(savedInstanceState);
                   setContentView(R.layout.main);    
               }

               public void launchScanner(View v) 
               {

              if (isCameraAvailable()) 
              {                
                Intent intent = new Intent(MainActivity.this, ZBarScannerActivity.class);
                startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
                     /*this.setCrop(100,230,600,10);*/
                   }
                   else 
              {
           Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
         }
     }

     public void launchQRScanner(View v) 
     {
         if (isCameraAvailable()) 
         {
            Intent intent = new Intent(this, ZBarScannerActivity.class);
            intent.putExtra(ZBarConstants.SCAN_MODES, new int[]{Symbol.QRCODE});
            startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
         } 
         else 
         {                 Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
         }
          }

public boolean isCameraAvailable()
{
    PackageManager pm = getPackageManager();
    return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    TextView myMsg = new TextView(this);
    myMsg.setText("Scanned Successfully");
    myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
    myMsg.setTextSize(20); 
    myMsg.setTextColor(Color.WHITE);
    switch (requestCode) {
        case ZBAR_SCANNER_REQUEST:
        case ZBAR_QR_SCANNER_REQUEST:
            if (resultCode == RESULT_OK) 
            {
           AlertDialog.Builder builder = new AlertDialog.Builder(this);
           builder.setTitle("Scanned Successfully").setMessage
           ("Result:" +data.getStringExtra(ZBarConstants.SCAN_RESULT))
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
       Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
           }
           });
           builder.create().show();
         Toast.makeText(this, "Scan Result = " + data.getStringExtra(ZBarConstants.SCAN_RESULT), 
         Toast.LENGTH_SHORT).show();
            } 
   else if(resultCode == RESULT_CANCELED && data != null)
         {
            String error = data.getStringExtra(ZBarConstants.ERROR_INFO);
            if(!TextUtils.isEmpty(error)) 
            {
             Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
            }
          }
                 break;
              }
     }
      }

XML文件

        <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:padding="5dp"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button
              android:id="@+id/scan_btn"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:onClick="launchScanner"
              android:text="@string/scan" />
        <Button      
              android:id="@+id/qrscan_btn"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:onClick="launchQRScanner"
              android:text="@string/scan_only_qr_codes"/>
        </LinearLayout>

0 个答案:

没有答案