使用第三方包实现二维码扫描,生成不带log的二维码,和生成带log的二维码
包见文件library_qrcode
xml:
<?xml version="1.0" encoding="utf-8"?> <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="fanggao.qf.qrcode.MainActivity"> <Button android:id="@+id/btn_scan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="扫描二维码" android:onClick="onClick"/> <Button android:id="@+id/btn_create_qrcode" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成二维码" android:onClick="onClick"/> <Button android:id="@+id/btn_create_qrcode_log" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成带log的二维码" android:onClick="onClick"/> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
main:
public class MainActivity extends Activity { private ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.image); } public void onClick(View view){ switch (view.getId()){ case R.id.btn_scan://开启扫描 //开启一个扫描的activity Intent intent = new Intent(MainActivity.this, CaptureActivity.class); startActivityForResult(intent,106); break; case R.id.btn_create_qrcode://生成二维码 /* String content, 二维码的内容 Context context, Bitmap logo, 二维码中的logo int hightAndWidth 宽高*/ Bitmap bitmap = EncodingHandler.enCodeStringWithLogo("你好.t啊", MainActivity.this, null, 200); image.setImageBitmap(bitmap); break; case R.id.btn_create_qrcode_log: Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); image.setImageBitmap(EncodingHandler.enCodeStringWithLogo("你好啊",MainActivity.this,bitmap1,200)); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == 106 && resultCode == RESULT_OK && data != null){ //获取数据信息 String result = data.getStringExtra("result"); Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); } } }
效果: