Android二維碼條形碼生成

jopen 11年前發布 | 74K 次閱讀 Android Android開發 移動開發

準備目前Google的zxing jar包不支持中文碼的生成,所以本示例中也不支持中文。需要中文支持的朋友,請自行修改zxing.jar包再編譯下。 廢話不說,直接上效果圖:

Android二維碼條形碼生成

Android二維碼條形碼生成

 

具體示例代碼如下:

package com.test.createcode;

import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast;

import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix;

/**

  • 用于創建和掃描二維碼 */ public class CreateCodeActivity extends Activity {
/**
 * 將要生成二維碼的內容
 */
private EditText codeEdit;

/**
 * 生成二維碼代碼
 */
private Button twoCodeBtn;
/**
 * 用于展示生成二維碼的imageView
 */
private ImageView codeImg;

/**
 * 生成一維碼按鈕
 */
private Button oneCodeBtn;

/**
 * 界面的初始化事件
 *
 * @param savedInstanceState Bundle對象
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initView();
    setListener();

}

/**
 * 用于初始化界面展示的view
 */
private void initView() {
    codeEdit = (EditText) findViewById(R.id.code_edittext);
    twoCodeBtn = (Button) findViewById(R.id.code_btn);
    oneCodeBtn = (Button) findViewById(R.id.btn_code);
    codeImg = (ImageView) findViewById(R.id.code_img);
}

/**
 * 設置生成二維碼和掃描二維碼的事件
 */
private void setListener() {
    twoCodeBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String str = codeEdit.getText().toString().trim();
            Bitmap bmp = null;
            try {
                if (str != null && !"".equals(str)) {
                    bmp = CreateTwoDCode(str);
                }
            } catch (WriterException e) {
                e.printStackTrace();
            }
            if (bmp != null) {
                codeImg.setImageBitmap(bmp);
            }

        }
    });


    oneCodeBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            String str = codeEdit.getText().toString().trim();
            int size = str.length();
            for (int i = 0; i < size; i++) {
                int c = str.charAt(i);
                if ((19968 <= c && c < 40623)) {
                    Toast.makeText(CreateCodeActivity.this, "生成條形碼的時刻不能是中文", Toast.LENGTH_SHORT).show();
                    return;
                }

            }
            Bitmap bmp = null;
            try {
                if (str != null && !"".equals(str)) {
                    bmp = CreateOneDCode(str);
                }
            } catch (WriterException e) {
                e.printStackTrace();
            }
            if (bmp != null) {
                codeImg.setImageBitmap(bmp);
            }
        }
    });
}

/**
 * 將指定的內容生成成二維碼
 *
 * @param content 將要生成二維碼的內容
 * @return 返回生成好的二維碼事件
 * @throws WriterException WriterException異常
 */
public Bitmap CreateTwoDCode(String content) throws WriterException {
    // 生成二維矩陣,編碼時指定大小,不要生成了圖片以后再進行縮放,這樣會模糊導致識別失敗
    BitMatrix matrix = new MultiFormatWriter().encode(content,
            BarcodeFormat.QR_CODE, 300, 300);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    // 二維矩陣轉為一維像素數組,也就是一直橫著排了
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = 0xff000000;
            }
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    // 通過像素數組生成bitmap,具體參考api
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

/**
 * 用于將給定的內容生成成一維碼 注:目前生成內容為中文的話將直接報錯,要修改底層jar包的內容
 *
 * @param content 將要生成一維碼的內容
 * @return 返回生成好的一維碼bitmap
 * @throws WriterException WriterException異常
 */
public Bitmap CreateOneDCode(String content) throws WriterException {
    // 生成一維條碼,編碼時指定大小,不要生成了圖片以后再進行縮放,這樣會模糊導致識別失敗
    BitMatrix matrix = new MultiFormatWriter().encode(content,
            BarcodeFormat.CODE_128, 500, 200);
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (matrix.get(x, y)) {
                pixels[y * width + x] = 0xff000000;
            }
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    // 通過像素數組生成bitmap,具體參考api
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

}</pre>main.xml文件如下:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ff999999">
<EditText android:layout_width="fill_parent" android:id="@+id/code_edittext"
android:layout_height="wrap_content" android:text=""/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_marginTop="20dip" android:layout_gravity="center_horizontal">
<Button android:id="@+id/code_btn" android:layout_width="100dip"
android:layout_height="40dip" android:text="生成二維碼"/>
<Button android:id="@+id/btn_code" android:layout_width="100dip" android:layout_height="40dip"
android:text="生成一維碼"></Button>
</LinearLayout>

    <ImageView android:layout_width="wrap_content" android:id="@+id/code_img"  
               android:layout_height="wrap_content" android:layout_marginTop="20dip"  
               android:layout_gravity="center_horizontal"/>  
</LinearLayout>  </pre>清單文件如下:<pre class="brush:xml; toolbar: true; auto-links: false;">    <?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.test.createcode"  
    android:versionCode="1"  
    android:versionName="1.0" >  

    <uses-sdk android:minSdkVersion="10" />  

    <application  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name" >  
        <activity  
            android:name=".CreateCodeActivity"  
            android:label="@string/app_name" >  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  

                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  

</manifest>  </pre> <p></p>

最后不要下載個zxing.jar放在libs文件夾中,即可。(上述的BitMatrix及BarcodeFormat等類是依賴于zxing.jar的。)


下載地址:

http://code.google.com/p/zxing/

來自:http://blog.csdn.net/chenshufei2/article/details/8682934

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!