android运行时错误使用zxing条形码API,无法在API中找到类

时间:2013-10-08 05:28:19

标签: java android jar zxing

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

尝试启动活动时,我遇到“运行时错误”。

实际错误代码段: 10-08 04:36:49.308:E / dalvikvm(780):找不到类com.toyota.toyotaownerspoc.barcode.ScanVinFromBarcodeActivity.cameraBytesToBinaryBitmap引用的类'com.google.zxing.RGBLuminanceSource' -08 04:36:49.326:E / dalvikvm(780):找不到类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)服务活动的布局:

<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>

现在对具有运行时错误的实际活动&amp;崩溃

3)实际的“ScanVinFromBarcodeActivity”活动,其中包含条形码扫描仪&amp; (使用zxing 2.2 liberary&amp; 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能够解析这些类,我已经使用了其他2个“core.jar”文件,并且还编译了我自己的版本,但仍然无法在运行时找到这些类。

我甚至使用了“jar xf”命令并展开了core-2.2.jar,发现它们应该是两个类,请看下面的内容:

extracted core.jar

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

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

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

相关问题