如何扫描android中的条码和QR码?

时间:2017-07-21 08:56:32

标签: android qr-code barcode zxing barcode-scanner

我正在创建一个应用程序并希望通过我的应用程序扫描条形码。我正在使用ZXing库进行扫描,但总是收到错误 - java.lang.UnsatisfiedLinkError
有什么想法吗?

ScannerWristBandActivity.java

public class ScannerWristBandActivity extends AppCompatActivity
 {
  private Camera mCamera;
  private ScannerPreviewWristBand mPreview;
  private Handler autoFocusHandler;
  TextView scanText;
  Button scanButton;
  int stage;
ImageScanner scanner;

private boolean barcodeScanned = false;
private boolean previewing = true;

static 
{
    System.loadLibrary("iconv");
} 

public void onCreate(Bundle savedInstanceState)
{
    Log.i("Scanner Wrist Band","Inside on create");
    super.onCreate(savedInstanceState);

    setContentView(R.layout.patientwristband);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    autoFocusHandler = new Handler();
    mCamera = getCameraInstance();

    /* Instance barcode scanner */
    scanner = new ImageScanner();
    scanner.setConfig(0, Config.X_DENSITY, 3);
    scanner.setConfig(0, Config.Y_DENSITY, 3);

    mPreview = new ScannerPreviewWristBand(this, mCamera, previewCb, autoFocusCB);
    FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
    preview.addView(mPreview);

    scanText = (TextView)findViewById(R.id.scanText);

    scanButton = (Button)findViewById(R.id.ScanButton);

    scanButton.setOnClickListener(new OnClickListener() 
    {
            public void onClick(View v) 
            {
                if (barcodeScanned)
                {
                    Log.i("Scanner Wrist Band","barcode scanned"+barcodeScanned);
                    barcodeScanned = false;
                    scanText.setText("Scanning...");
                    mCamera.setPreviewCallback(previewCb);
                    mCamera.startPreview();
                    previewing = true;
                    mCamera.autoFocus(autoFocusCB);
                }
            }
    });
}

public void onPause() 
{
    super.onPause();
    releaseCamera();
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance()
{
    Camera c = null;
    try
    {
        c = Camera.open();
    }
    catch (Exception e)
    {

    }
    return c;
}

private void releaseCamera()
{
    if (mCamera != null)
    {
        previewing = false;
        mCamera.setPreviewCallback(null);
        mCamera.release();
        mCamera = null;
    }
}

private Runnable doAutoFocus = new Runnable()
{
        public void run() 
        {
            if (previewing)
                mCamera.autoFocus(autoFocusCB);
        }
    };

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);
            Log.i("Scanner Wrist Band","Result of barcode"+result);
            if (result != 0)
            {
                previewing = false;
                mCamera.setPreviewCallback(null);
                mCamera.stopPreview();

                SymbolSet syms = scanner.getResults();
                for (Symbol sym : syms) 
                {
                    scanText.setText("barcode result " + sym.getData());
                    barcodeScanned = true;
                    Toast.makeText(getApplicationContext(), "barcode And Stpin"+sym.getData()+stpinTextField, Toast.LENGTH_SHORT).show();
                    Log.i("Scanner Wrist Band","Result Of Barcode"+sym.getData() +" Stpin"+stpinTextField);
                        String barcodecoderesult =sym.getData();
                        if(barcodecoderesult.equals(stpinTextField) )
                        {
                            Log.i("Scanner Wrist Band","Result Of Barcode and Stpin Matches"+barcodecoderesult +" 
                        }

                }
            }
        }
    };

// Mimic continuous auto-focusing
AutoFocusCallback autoFocusCB = new AutoFocusCallback()
{
        public void onAutoFocus(boolean success, Camera camera) 
        {
            autoFocusHandler.postDelayed(doAutoFocus, 1000);
        }
};}

logcat的

java.lang.UnsatisfiedLinkError: 
dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.admin.application-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.admin.application-2/lib/x86, /system/lib, /vendor/lib]]] couldn't find "libiconv.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:972)
at java.lang.System.loadLibrary(System.java:1530)
at com.example.admin.application.ScannerWristBandActivity.<clinit>(ScannerWristBandActivity.java:49)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:0)

从此处获取libiconv.so
https://github.com/chentao0707/ZBarAndroidSDK/tree/master/ZBarScanProjAll/libs/arm64-v8a
然后复制到您的jniLib。