由于我不拥有用于开发目的的G1,因此我正在尽最大努力使用模拟器。
这就是说,我正在尝试使用ZXing(Zebra Zrossing)库扫描sdCard中的JPEG图像或PNG图像。

我试图更改Android项目中的代码,因此它将从sdCard中的图像而不是从相机进行扫描,而没有任何运气。

接下来的操作可能是问题的根源。

我试图在一个新的Android项目中使用JAVASE代码为“修改后的” CommandLineRunner提供图像,这是事情:

Eclipse将构建该项目,但不会运行它。
VM日志使我看到以下消息:



我在网上搜索了一个答案,然后有人说这是Dalvikvm上的常见错误,原因是可能存在一个类或库已经过预编译(在Eclipse之外,是真实的),并且VM无法运行使用它。

我将代码发布到主要 Activity 中:

package com.magoco.fread;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.TextView;

import com.google.zxing.DecodeHintType;
import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ResultParser;

public class FRead extends Activity {
    /** Called when the activity is first created. */
    @Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv = (TextView) this.findViewById(R.id.BarcodeResult);
    try {
        tv.setText(this.decodeOneArgument2("", false));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public String decodeOneArgument2(String argument, boolean dumpResults)
        throws Exception {
    String barcode = "";
    // File inputFile = new File(argument);
    File inputFile = new File("sdcard/dcim/pueblo.JPG");

    /* TESTING THAT I'VE GOT A FILE */
    System.out.println("FILE " + inputFile.toString());

    // decode(new URI(argument), hints);
    decode2(inputFile, null, barcode);
    return barcode;

}

public String decode2(File f, Hashtable<DecodeHintType, Object> hints,
        String barcode) throws IOException {

    /* IF I COMMENT THE NEXT LINE, IT RUNS BUT OF COURSE NO RESULT */
    BufferedImage image;
    try {
        image = ImageIO.read(f);
    } catch (IllegalArgumentException iae) {
        throw new FileNotFoundException("Resource not found: " + f);
    }
    try {
        MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(
                image);
        Result result = new MultiFormatReader().decode(source, hints);
        ParsedResult parsedResult = ResultParser.parseResult(result);
        barcode = " format: " + result.getBarcodeFormat()+ result.getText() + "\nParsed result:\n"+ parsedResult.getDisplayResult();
        System.out.println(" format: " + result.getBarcodeFormat()+ result.getText() + "\nParsed result:\n"+ parsedResult.getDisplayResult());
        //return result;
    } catch (ReaderException e) {
        System.out.println(": No barcode found");
        return null;
    }
    return barcode;
}

}

提前致谢

monn3t

最佳答案


我试图从条形码中提取数据,而这些是我遵循的步骤。

1.下载并下载ZXing 1.3。

2.通过设置属性将提取的zxing floder中的core/src和androidtest/src添加到android应用程序。
使用此链接设置属性http://groups.google.com/group/zxing/browse_thread/thread/7d8693e6e42408f2

现在包括以下代码,

package payoda.android.zxingApp;
import android.app.Activity;
import android.database.CursorJoiner.Result;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Reader;
import com.google.zxing.ReaderException;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.client.androidtest.*;
import com.google.zxing.common.BaseMonochromeBitmapSource;


public class ZXingApplication1 extends Activity
{
  TextView tv;
  com.google.zxing.Result result;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv=(TextView)findViewById(R.id.text);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bar2);
    MultiFormatReader reader1=new MultiFormatReader();//For all barcode formats
    QRCodeReader reader=new QRCodeReader();//Only for QRCode format

   try
    {

       result=reader1.decode(new RGBMonochromeBitmapSource(bitmap));
       tv.setText(result.getText());
    }
    catch (Exception e)
    {
        tv.setText("Within Catch block");
     }
  }
 }

这可能对您有帮助。

10-06 14:50
查看更多