QR code encoder
(qrcode_java0.50beta10.tar\qrcode_java0.50beta10\qrcode_java0.50beta10\lib\Qrcode.jar)
2. Code:
package com.qrcode.encoder.qrcodeencoder; import java.io.UnsupportedEncodingException; import android.app.Activity; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import com.swetake.util.Qrcode; public class MainActivity extends Activity implements SurfaceHolder.Callback{ private final static String TAG = "QRCode"; private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private EditText mEtInput; private Button mBtnEncode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e(TAG, "onCreate"); DisplayMetrics mDisplayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics); mSurfaceView = (SurfaceView)findViewById(R.id.sv_qrcode); mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mEtInput = (EditText)findViewById(R.id.et_input); mBtnEncode = (Button)findViewById(R.id.btn_encode); mBtnEncode.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { Log.e(TAG, "mBtnEncode onClick"); encodeQR(mEtInput.getText().toString(), 7); } }); } protected void encodeQR(String str, int version) { Log.e(TAG, "encodeQR"); Qrcode mQrcode = new Qrcode(); mQrcode.setQrcodeErrorCorrect('M'); mQrcode.setQrcodeEncodeMode('B'); mQrcode.setQrcodeVersion(version); try { byte[] bytesEncoding = str.getBytes("utf-8"); if(bytesEncoding.length>0 && bytesEncoding.length<120){ boolean[][] bEncoding = mQrcode.calQrcode(bytesEncoding); drawQRCode(bEncoding, Color.BLACK); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } private void drawQRCode(boolean[][] bRect, int colorFill) { Log.e(TAG, "drawQRCode"); int mPadding = 40; int mLength = 20; Canvas mCanvas = mSurfaceHolder.lockCanvas(); mCanvas.drawColor(Color.WHITE); Paint mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(colorFill); mPaint.setStrokeWidth(1.0F); for(int i=0;i<bRect.length;i++){ for(int j=0;j<bRect.length;j++){ if(bRect[j][i]){ mCanvas.drawRect( new Rect( mPadding+j*mLength, mPadding+i*mLength, mPadding+j*mLength+mLength, mPadding+i*mLength+mLength ), mPaint); } } } mSurfaceHolder.unlockCanvasAndPost(mCanvas); } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { Log.e(TAG, "surfaceChanged"); } @Override public void surfaceCreated(SurfaceHolder arg0) { Log.e(TAG, "surfaceCreated"); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { Log.e(TAG, "surfaceDestroyed"); } }
layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <EditText android:id="@+id/et_input" android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" /> <SurfaceView android:id="@+id/sv_qrcode" android:layout_below="@id/et_input" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_encode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="@string/encode"/> </RelativeLayout>
Manifest :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.qrcode.encoder.qrcodeencoder" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" 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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。