这里简单介绍一下ZXing库。ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。该项目可实现的条形码编码和解码。目前支持以下格式:UPC-A,UPC-E、EAN-8,EAN-13、39码、93码。ZXing是个很经典的条码/二维码识别的开源类库,以前在功能机上,就有开发者使用J2ME运用ZXing了,不过要支持JSR-234规范(自动对焦)的手机才能发挥其威力。
ZXingGitHub地址
效果图:
主要实现步骤:
导入libzxing这个模块
ZXing源代码很大,功能也很多,这里只是抽取了其中的一部分代码整合到了一起
扫描
在main_activity中添加一个Button和一个TextView 点击Button后开始调照相机功能,扫描二维码
TextView会显示扫描后的结果
<Button android:text=\"Strat Scan\" android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:onClick=\"scan\"/> <TextView android:id=\"@+id/tv_showResult\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\"Hello World!\"/>
在ActivityMain中分别初始化这两个控件
private TextView mTextView; mTextView= (TextView) this.findViewById(R.id.tv_showResult); //扫描二维码 //https://cli.im/text?2dd0d2b267ea882d797f03abf5b97d88二维码生成网站 public void scan(View view) { startActivityForResult(new Intent(this, CaptureActivity.class),0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode==RESULT_OK){ Bundle bundle = data.getExtras(); if (bundle != null) { String result=bundle.getString(\"result\"); mTextView.setText(result); } } }
生成二维码
这里就把整个项目的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:paddingBottom=\"@dimen/activity_vertical_margin\" android:paddingLeft=\"@dimen/activity_horizontal_margin\" android:paddingRight=\"@dimen/activity_horizontal_margin\" android:paddingTop=\"@dimen/activity_vertical_margin\" android:orientation=\"vertical\" tools:context=\"com.example.hfs.zxingdemo.MainActivity\"> <Button android:text=\"Strat Scan\" android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:onClick=\"scan\"/> <TextView android:id=\"@+id/tv_showResult\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:text=\"Hello World!\"/> <EditText android:id=\"@+id/et_text\" android:hint=\"Imput\" android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\"/> <Button android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:onClick=\"make\" android:text=\"Make QRCode\"/> <CheckBox android:id=\"@+id/cb_logo\" android:layout_width=\"wrap_content\" android:text=\"Logo\" android:layout_height=\"wrap_content\"/> <ImageView android:id=\"@+id/img_shouw\" android:layout_width=\"wrap_content\" android:layout_gravity=\"center\" android:background=\"@mipmap/ic_launcher\" android:layout_height=\"wrap_content\"/> </LinearLayout>
MainActivity中代码
package com.example.hfs.zxingdemo; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.hardware.camera2.CaptureRequest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.xys.libzxing.zxing.activity.CaptureActivity; import com.xys.libzxing.zxing.encoding.EncodingUtils; public class MainActivity extends AppCompatActivity { private TextView mTextView; private EditText mEditText; private ImageView mImageView; private CheckBox mCheckBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mTextView= (TextView) this.findViewById(R.id.tv_showResult); mEditText= (EditText) this.findViewById(R.id.et_text); mImageView= (ImageView) this.findViewById(R.id.img_shouw); mCheckBox= (CheckBox) this.findViewById(R.id.cb_logo); } //扫描二维码 //https://cli.im/text?2dd0d2b267ea882d797f03abf5b97d88二维码生成网站 public void scan(View view) { startActivityForResult(new Intent(this, CaptureActivity.class),0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode==RESULT_OK){ Bundle bundle = data.getExtras(); if (bundle != null) { String result=bundle.getString(\"result\"); mTextView.setText(result); } } } //生成二维码 可以设置Logo public void make(View view) { String input = mEditText.getText().toString(); if (input.equals(\"\")){ Toast.makeText(this,\"输入不能为空\",Toast.LENGTH_SHORT).show(); }else{ Bitmap qrCode = EncodingUtils.createQRCode(input, 500, 500, mCheckBox.isChecked()? BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher):null);//CheckBox选中就设置Logo mImageView.setImageBitmap(qrCode); } } }
好了 到这里就写完了
项目地址
以上所述是小编给大家介绍的Android实现二维码扫描和生成的简单方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对phpstudy网站的支持!
本文地址:https://www.stayed.cn/item/7574
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我