如何解决QR码生成中的画布错误

时间:2012-03-09 20:13:23

标签: java android qr-code android-canvas

我正在尝试根据这个网站http://www.onbarcode.com/products/android_barcode/barcodes/qrcode.html执行QR码教程,其中只有一个错误出现"画布无法解析为变量

这是Java代码

    package com.qrcode;

    import com.onbarcode.barcode.android.AndroidColor;
    import com.onbarcode.barcode.android.IBarcode;
    import com.onbarcode.barcode.android.QRCode;
    import android.app.Activity;
    import android.graphics.Canvas;
    import android.graphics.RectF;
    import android.os.Bundle;

    public class QrcodeActivity extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    QRCode barcode = new QRCode();

    /*
       QRCode Valid data char set:
            numeric data (digits 0 - 9);
            alphanumeric data (digits 0 - 9; upper case letters A -Z; nine other characters: space, $ % * + - . / : );
            byte data (default: ISO/IEC 8859-1);
            Kanji characters
    */
    barcode.setData("http://www.onbarcode.com");
    barcode.setDataMode(QRCode.M_AUTO);
    barcode.setVersion(10);
    barcode.setEcl(QRCode.ECL_M);

    //  if you want to encode GS1 compatible QR Code, you need set FNC1 mode to IBarcode.FNC1_ENABLE
    barcode.setFnc1Mode(IBarcode.FNC1_NONE);

    //  Set the processTilde property to true, if you want use the tilde character "~" to
    //  specify special characters in the input data. Default is false.
    //  1-byte character: ~ddd (character value from 0 ~ 255)
    //  ASCII (with EXT): from ~000 to ~255
    //  2-byte character: ~6ddddd (character value from 0 ~ 65535)
    //  Unicode: from ~600000 to ~665535
    //  ECI: from ~7000000 to ~7999999
    //  SJIS: from ~9ddddd (Shift JIS 0x8140 ~ 0x9FFC and 0xE040 ~ 0xEBBF)
    barcode.setProcessTilde(false);

    // unit of measure for X, Y, LeftMargin, RightMargin, TopMargin, BottomMargin
    barcode.setUom(IBarcode.UOM_PIXEL);
    // barcode module width in pixel
    barcode.setX(3f);

    barcode.setLeftMargin(15f);
    barcode.setRightMargin(15f);
    barcode.setTopMargin(15f);
    barcode.setBottomMargin(15f);
    // barcode image resolution in dpi
    barcode.setResolution(72);

    // barcode bar color and background color in Android device
    barcode.setForeColor(AndroidColor.black);
    barcode.setBackColor(AndroidColor.white);

    /*
    specify your barcode drawing area
        */
        RectF bounds = new RectF(30, 30, 0, 0);
    ************************ERROR Shoots up in the below line stating "canvas cannot be resolved into a variable***************
    barcode.drawBarcode(canvas, bounds);

}

    }

这是我的main.xml

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


    </LinearLayout>

在qr一代是天真的,所以请帮助我与其余的代码朋友

2 个答案:

答案 0 :(得分:0)

在某处您必须定义要传递给barcode.drawBarcode(canvas,bounds)的变量画布;

您可能需要获取Activity中View的句柄,然后覆盖使用Canvas调用的onDraw()方法

看看这个从Activity在画布上绘制的简单示例: https://stackoverflow.com/a/4628298/391755

答案 1 :(得分:0)

你应该在android中的自定义视图的onDraw方法中编写上面的所有代码。创建一个扩展android.view.View类的类,然后在其中编写代码。这个方法会给你一个你想要的画布变量。请参阅以下代码:

public class Test extends View {

public Test(Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas) {
    //write your code here!
}

创建此类后,将此视图放在您的活动xml文件中,如下所示:

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

            <somepackage.somepackage.someview 
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
            </somepackage.somepackage.someview>
</LinearLayout>

注意:将somepackage.somepackage替换为最近创建的视图类的包,并将someview替换为自定义视图类名。