0

我正在使用 (zxing 2.2 API & its core.jar) 编写车辆条形码扫描仪活动,并且在继续之前我开始测试功能。

尝试启动活动时出现“运行时错误”。

实际错误片段:10-08 04:36:49.308: E/dalvikvm(780): 找不到类 'com.google.zxing.RGBLuminanceSource',引用自方法 com.toyota.toyotaownerspoc.barcode.ScanVinFromBarcodeActivity.cameraBytesToBinaryBitmap -08 04:36:49.326: E/dalvikvm(780): 找不到类 'com.google.zxing.MultiFormatReader',引用自方法 com.toyota.toyotaownerspoc.barcode.ScanVinFromBarcodeActivity.decodeBitmapToString

活动 (ScanVinFromBarcodeActivity) 在此之后崩溃。

下面的类实际上是通过 Eclipse 在我的“ScanVinFromBarcodeActivity)”活动中解决的,当我编写代码并编译时,但在运行时找不到

com.google.zxing.MultiFormatReader //运行时未找到 com.google.zxing.RGBLuminanceSource //运行时未找到

1)“服务”活动,其中包含启动我的扫描仪活动的按钮:

package com.toyota.toyotaownerspoc.service;

import com.toyota.toyotaownerspoc.R;
import com.toyota.toyotaownerspoc.barcode.ScanVinFromBarcodeActivity;
import com.toyota.toyotaownerspoc.nearby.Nearby;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;

public class Service extends Activity {

    private View scanVinButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        scanVinButton = findViewById(R.id.scanVinButton);

        scanVinButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent i = new Intent(v.getContext(),
                        ScanVinFromBarcodeActivity.class);
                startActivity(i);

            }
        });
    }

}

2)Service活动的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Service" >

    <Button
        android:id="@+id/scanVinButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:text="Scan VIN" />

</RelativeLayout>

现在到有运行时错误和崩溃的实际活动

3) 包含条码扫描器&的实际“ScanVinFromBarcodeActivity”活动(使用zxing 2.2 liberary & core.jar):

package com.toyota.toyotaownerspoc.barcode;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.MultiFormatReader; //found via import at compile time, however was found at run time 
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;//found via import at compile time, however was found at run time 
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.toyota.toyotaownerspoc.R;

public class ScanVinFromBarcodeActivity extends Activity {
    private Camera camera;
    private int cameraId = 0;
    private TextView VINtext = null;
    private View scanButton = null;
    // bitmap from camera
    private Bitmap bmpOfTheImageFromCamera = null;

    PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] imgData, Camera camera) {
            // get the bitmap from camera imageData
            bmpOfTheImageFromCamera = BitmapFactory.decodeByteArray(imgData, 0,
                    imgData.length);
            BinaryBitmap bitmap = null;
            if (bmpOfTheImageFromCamera != null) {
                // convert bitmap to binary bitmap
                bitmap = cameraBytesToBinaryBitmap(bmpOfTheImageFromCamera);

                if (bitmap != null) {
                    // decode the VIN
                    String VIN = decodeBitmapToString(bitmap);
                    Log.d("ClassScanViewBarcodeActivity , scanButton.setOnClickListener(): ",
                            VIN);
                    VINtext.setText(VIN);
                } else {
                    Log.d("ClassScanViewBarcodeActivity , scanButton.setOnClickListener(): bitmap=",
                            String.valueOf(bitmap));
                }
            } else {
                Log.d("ClassScanViewBarcodeActivity , scanButton.setOnClickListener(): bmpOfTheImageFromCamera = ",
                        String.valueOf(bmpOfTheImageFromCamera));
            }
        }

    };// jpegCallback implementation

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

        // do we have any camera's on this device
        // check for back camera
        if (!getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
                    .show();
        } else { // check for front camera
            cameraId = findFrontFacingCamera();
            if (cameraId < 0) {
                Toast.makeText(this, "No front facing camera found.",
                        Toast.LENGTH_LONG).show();
            } else {
                camera = Camera.open(cameraId);
            }
        }// end else ,check for front camera

        // if camera is not null , than display incoming images on a preview screen on SurfaceView
        if (camera != null) {
            //implement later 
        }

        // create text area & scan button
        VINtext = (TextView) findViewById(R.id.mytext);
        scanButton = findViewById(R.id.webbutton);

        scanButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                camera.takePicture(null, null, jpegCallback);

            }
        });

    }// end onCreate

    private int findFrontFacingCamera() {
        int cameraId = -1;
        // Search for the front facing camera
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                Log.d("ClassScanViewBarcodeActivity , findFrontFacingCamera(): ",
                        "Camera found");
                cameraId = i;
                break;
            }
        }
        return cameraId;
    }// end findFrontFacingCamera()

    @Override
    protected void onPause() {
        if (camera != null) {
            camera.release();
            camera = null;
        }
        super.onPause();
    }// end onPause()

    public String decodeBitmapToString(BinaryBitmap bitmap) {
        Reader reader = null;
        Result result = null;
        String textResult = null;
        try {

            reader = new MultiFormatReader();
            if (bitmap != null) {
                result = reader.decode(bitmap);
                if (result != null) {
                    textResult = result.getText();
                } else {
                    Log.d("ClassScanViewBarcodeActivity , String decodeBitmapToString (BinaryBitmap bitmap): result = ",
                            String.valueOf(result));
                }
            } else {
                Log.d("ClassScanViewBarcodeActivity , String decodeBitmapToString (BinaryBitmap bitmap): bitmap = ",
                        String.valueOf(bitmap));
            }
            /*
             * byte[] rawBytes = result.getRawBytes(); BarcodeFormat format =
             * result.getBarcodeFormat(); ResultPoint[] points =
             * result.getResultPoints();
             */

        } catch (NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ChecksumException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

        return textResult;
    }// end decodeBitmapToString (BinaryBitmap bitmap)

    public BinaryBitmap cameraBytesToBinaryBitmap(Bitmap bitmap) {
        BinaryBitmap binaryBitmap = null;
        if (bitmap != null) {

            int[] pixels = new int[bitmap.getHeight() * bitmap.getWidth()];
            bitmap.getPixels(pixels, 0, 0, bitmap.getWidth() - 1,
                    bitmap.getHeight() - 1, bitmap.getWidth(),
                    bitmap.getHeight());

            RGBLuminanceSource source = new RGBLuminanceSource(
                    bitmap.getWidth(), bitmap.getHeight(), pixels);

            HybridBinarizer bh = new HybridBinarizer(source);
            binaryBitmap = new BinaryBitmap(bh);
        } else {
            Log.d("ClassScanViewBarcodeActivity , cameraBytesToBinaryBitmap (Bitmap bitmap): bitmap = ",
                    String.valueOf(bitmap));
        }

        return binaryBitmap;
    }

}// end activity

4) ScanVinFromBarcodeActivity 活动布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dip"
    >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/mbackground1"
android:gravity="center_horizontal"
android:text="@string/decode_label"
android:padding="20dip" />

<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@color/mbackground2" 
android:textColor="@color/mytextcolor" 
android:padding="20dip"/>


<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label"
android:gravity="center_horizontal"
android:textColor="@color/mytextcolor"
android:padding="20dip"/>

<Button 
android:id="@+id/webbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/web_button"
android:textColor="@color/mytextcolor"/>

</LinearLayout>

Eclipse 能够解析这些类,我使用了另外两个“core.jar”文件,还编译了我自己的版本,但在运行时仍然找不到这些类。

我什至使用了“jar xf”命令并分解了core-2.2.jar,发现这两个类应该是,请看下面:

提取的core.jar

任何帮助,将不胜感激。

谢谢

4

1 回答 1

0

我解决了这个问题。

尽管最初通过 Eclipse 菜单添加了 zxing API 的 core-2.2.jar ,但通过转到项目属性,而不是 Java Build Path ,然后添加外部 JARS 并且核心 jar 出现在项目树中,由于某种原因,在运行时发现了一些类.

所以我手动将 core-2.2.jar 复制粘贴到项目的 /libs 文件夹中,一切都在编译和运行。

于 2013-10-08T21:54:08.543 回答