我的条形码应用程序在运行时关闭

时间:2013-03-15 17:11:27

标签: android barcode

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

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。

Log Cat:

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

2 个答案:

答案 0 :(得分:0)

尝试移动它:

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

下:

setContentView(R.layout.main);

答案 1 :(得分:0)

在你的祝酒词中,你应该尝试Main.this而不是getApplicationContext()。另请注意,Activity是一种Context