code一代画布错误

code一代画布错误

本文介绍了如何解决QR code一代画布错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照这个网站的,只有一个错误最多拍摄画布不能解析到一个变量

I am trying to execute the QR Code tutorial according to this website http://www.onbarcode.com/products/android_barcode/barcodes/qrcode.html, where only one error shoots up "canvas cannot be resolved to a variable

下面是Java code

Here is the Java code

    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

Here is my 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一代天真,所以请帮助我的code的朋友,其余

Am a naive in qr generation, so please help me with the rest of the code friends

推荐答案

您应该写的所有code以上,在Android的自定义视图的方法的onDraw。创建一个扩展 android.view.View 类的类,然后写你的code在里面。这种方法会给你一个变量是你想要的画布。看到下面的code:

You should write all of the code above, in onDraw method of a custom view in android. create a class that extends android.view.View class, and then write your code in it. this method will give you a variable that is the canvas you want. see the code below:

public class Test extends View {

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

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

创建该类之后,把这个观点在活动的XML文件是这样的:

after creating this class, put this view in your activity xml file like this:

<?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与您的自定义视图类名称

note: replace the somepackage.somepackage with package of your recently created view class, and replace someview with you custom view class name.

这篇关于如何解决QR code一代画布错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:37