Android - 二维码扫一扫
1、教学视频:慕课网 http://www.imooc.com/note/648?sort=last
2、第三方包ZXing实现二维码扫描解析与生成功能;
3、Android Studio 项目目录:1、app 2、libzxing,app引用libzxing Module;
4、源代码:
解析二维码: startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0); //0:requestCode
protected void onActivityResult(int requestCode, int resultCode, Intent data) {...}
生成二维码: EncodingUtils.createQRCode("codeText", 500, 500, logoBitmap) ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | public class MainActivity extends AppCompatActivity { private TextView qrCodeResult; private EditText qrCodeEditText; private ImageView qrCodeImageView; private CheckBox useLogo; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); qrCodeResult = (TextView) findViewById(R.id.qrCodeResult); qrCodeEditText = (EditText) findViewById(R.id.qrCodeText); qrCodeImageView = (ImageView) findViewById(R.id.qrCodeImageView); useLogo = (CheckBox) findViewById(R.id.chk_useLogo); } public void scan(View view) { startActivityForResult( new Intent(MainActivity. this , CaptureActivity. class ), 0 ); //0:requestCode } public void make(View view) { String qrcodeText = qrCodeEditText.getText().toString(); if (qrcodeText.equals( "" )) { Toast.makeText(MainActivity. this , "输入不能为空" , Toast.LENGTH_SHORT).show(); return ; } Bitmap qrcodeBitmap = EncodingUtils.createQRCode(qrcodeText, 500 , 500 , useLogo.isChecked() ? BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher) : null ); qrCodeImageView.setImageBitmap(qrcodeBitmap); } @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { super .onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == 0 ) { Bundle bundle = data.getExtras(); qrCodeResult.setText(bundle.getString( "result" )); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?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= "com.yizhui.qrcode.MainActivity" > <Button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "@string/scan_qrcode" android:onClick= "scan" /> <TextView android:id= "@+id/qrCodeResult" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_margin= "10dp" /> <EditText android:id= "@+id/qrCodeText" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <Button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "@string/make_qrcode" android:onClick= "make" /> <CheckBox android:id= "@+id/chk_useLogo" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "@string/use_logo" /> <ImageView android:id= "@+id/qrCodeImageView" android:layout_gravity= "center_horizontal" android:layout_margin= "20dp" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> </LinearLayout> |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步