概述
导入依赖:
compile 'com.github.open-android:Zxing:v1.0.3'
清单文件:
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.FLASHLIGHT" />
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.test.ly.mysao.MainActivity"> <EditText android:id="@+id/edt_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容" /> <Button android:id="@+id/btn_create" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成二维码" /> <Button android:id="@+id/btn_scan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="扫码(支持识别手机相册二维码)" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <ImageView android:id="@+id/iv_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:visibility="gone" /> <TextView android:id="@+id/tv_hint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="扫描返回bitmap" android:textColor="#f00" /> </RelativeLayout> <TextView android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffff00"/> </LinearLayout>
MainActivty代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText mContent; private Button mCreate,mScan; private ImageView mImage; private final static int REQ_CODE = 1028; private TextView mHint; private TextView mResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mContent = (EditText)findViewById(R.id.edt_content); mCreate = (Button)findViewById(R.id.btn_create); mScan = (Button)findViewById(R.id.btn_scan); mImage = (ImageView)findViewById(R.id.iv_image); mHint = (TextView)findViewById(R.id.tv_hint); mResult = (TextView)findViewById(R.id.tv_result); mCreate.setOnClickListener(this); mScan.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_create : //生成二维码 String content = mContent.getText().toString().trim(); Bitmap bitmap = null; try { bitmap = BitmapUtils.create2DCode(content); mImage.setVisibility(View.VISIBLE); mHint.setVisibility(View.GONE); mImage.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } break; case R.id.btn_scan : //扫码 Intent intent = new Intent(Main2Activity.this, CaptureActivity.class); startActivityForResult(intent,REQ_CODE); break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQ_CODE) { mImage.setVisibility(View.VISIBLE); mContent.setVisibility(View.GONE); String result = data.getStringExtra(CaptureActivity.SCAN_QRCODE_RESULT); Bitmap bitmap = data.getParcelableExtra(CaptureActivity.SCAN_QRCODE_BITMAP); mResult.setText("朕扫描的结果是:" + result); showToast("扫码结果:"+result); if (bitmap != null) { mImage.setImageBitmap(bitmap); } } } private void showToast(String msg) { Toast.makeText(MainActivity.this, "" + msg, Toast.LENGTH_SHORT).show(); } }
最后
以上就是鳗鱼板栗为你收集整理的手机扫描二维码原理和二维码生成的实现的全部内容,希望文章能够帮你解决手机扫描二维码原理和二维码生成的实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复