天天看點

使用ZXing實作二維碼的掃描和生成帶logo的二維碼

公司主要從事pos機程式的開發,掃碼刷卡一類的肯定是避免不了的,花點時間簡單整理了一下使用ZXing進行掃碼和生成帶Logo的二維碼。

效果圖如下:

使用ZXing實作二維碼的掃描和生成帶logo的二維碼
使用ZXing實作二維碼的掃描和生成帶logo的二維碼
使用ZXing實作二維碼的掃描和生成帶logo的二維碼
使用ZXing實作二維碼的掃描和生成帶logo的二維碼

使用步驟很簡單:

第一步:下載下傳ZXing的library,我會在最後的提供(ZXing預設沒有提供生成Logo的二維碼)

第二步:建立一個 Android項目,關聯ZXing的library

第三部:建立自己的Activity

我的主界面布局如下:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.scandemo.MainActivity" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="scan"
        android:text="掃描二維碼" />
	<TextView 
	    android:id="@+id/show"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    />
	<EditText 
	    android:id="@+id/input"
	    android:layout_height="wrap_content"
	    android:layout_width="match_parent"
	    />
	<Button 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:onClick="genButton"
	    android:text="生成二維碼"
	    />
	<ImageView 
	    android:id="@+id/img"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center"
	    />
</LinearLayout>
           

第三部 在java代碼中調用 library的掃碼

public class MainActivity extends Activity {
	private TextView show;
	private EditText input;
	private ImageView img;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		show=(TextView) findViewById(R.id.show);
		input=(EditText) findViewById(R.id.input);
		img=(ImageView) findViewById(R.id.img);
	}
	public void scan(View v){
		Intent intent=new Intent(MainActivity.this,CaptureActivity.class);
//		startActivity(intent);
		startActivityForResult(intent, 0);
	}
	public void genButton(View v){
		String content=input.getText().toString();
		if(TextUtils.isEmpty(content)){
			Toast.makeText(MainActivity.this, "請輸入内容", Toast.LENGTH_LONG).show();
		}else{
			try {
				Bitmap logo=BitmapFactory.decodeResource(getResources(), R.drawable.logo);
				Bitmap bitmap=EncodingHandler.createQRCode(content, logo,300);
//				Bitmap bitmap=EncodingHandler.createCode(str, logo, 300);
				img.setImageBitmap(bitmap);
			} catch (WriterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK){
			String result=data.getStringExtra("result");
			show.setText(result);
		}
	}
}
           

注意 别忘了在自己的 項目中的AndroidManifest.xml 檔案中配置 library中的Activity

 <activity

            android:configChanges="orientation|keyboardHidden"

            android:name="com.zxing.activity.CaptureActivity"

            android:screenOrientation="portrait"

            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

            android:windowSoftInputMode="stateAlwaysHidden" >

        </activity>

源碼位址

我的主界面布局如下: