0

我让我的应用程序读取一个应用程序,然后告诉我代码的内容,但现在我试图让它将字符串编码回条形码并将其显示为屏幕上的图像。但是,它总是在启动之前强制关闭应用程序。这是我的代码,请帮助:

import java.util.EnumMap;
import java.util.Map;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

public class Main extends Activity {

IntentIntegrator integrator = new IntentIntegrator(this);

private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;
LinearLayout myLayout = (LinearLayout)findViewById(R.id.myLayout);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //LinearLayout myLayout = (LinearLayout)findViewById(R.id.myLayout);

    Button btn =(Button)findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            integrator.initiateScan();
        }
    });
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (scanResult != null) {
          Toast.makeText(Main.this,
                    "works",
                    Toast.LENGTH_SHORT).show();
          String contents = intent.getStringExtra("SCAN_RESULT");
          String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
          Toast.makeText(Main.this,
                    contents,
                    Toast.LENGTH_SHORT).show();
          Bitmap bitmap = null;
          ImageView iv = new ImageView(this);
          try {

                bitmap = encodeAsBitmap(contents, BarcodeFormat.CODE_128, 600, 300);
                iv.setImageBitmap(bitmap);

            } catch (WriterException e) {
                e.printStackTrace();
            }
          myLayout.addView(iv);
          TextView tv = new TextView(this);
            tv.setGravity(Gravity.CENTER_HORIZONTAL);
            tv.setText(contents);

            myLayout.addView(tv);
      }


    }
Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException {
    String contentsToEncode = contents;
    if (contentsToEncode == null) {
        return null;
    }
    Map<EncodeHintType, Object> hints = null;

        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result;
    try {
        result = writer.encode(contentsToEncode, format, img_width, img_height, hints);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
        pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

IntentResult 和 IntentIntegrator 均来自 ZXing。

原木猫:

03-15 13:05:04.303: E/AndroidRuntime(4363): FATAL EXCEPTION: main
4

2 回答 2

0

在您的吐司中,Main.this您应该尝试getApplicationContext(). 还记得 anActivity是 的一种Context

于 2013-03-15T17:32:00.900 回答
0

尝试移动这个:

LinearLayout myLayout = (LinearLayout)findViewById(R.id.myLayout);

在下面:

setContentView(R.layout.main);
于 2013-03-15T17:14:46.507 回答