Android第七期 - 二维码扫描与生成图
16lz
2021-12-04
首先是MainActivity.java部分:
package com.qrcode;import com.google.zxing.WriterException;import com.zxing.activity.CaptureActivity;import com.zxing.encoding.EncodingHandler;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private TextView resultTextView;private EditText qrStrEditText;private ImageView qrImgImageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); resultTextView = (TextView) this.findViewById(R.id.tv_scan_result); qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string); qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_p_w_picpath); Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode); scanBarCodeButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent openCameraIntent = new Intent(MainActivity.this,CaptureActivity.class);startActivityForResult(openCameraIntent, 0);}}); Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode); generateQRCodeButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {String contentString = qrStrEditText.getText().toString();if (!contentString.equals("")) {Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);qrImgImageView.setImageBitmap(qrCodeBitmap);}else {Toast.makeText(MainActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();}} catch (WriterException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}); }@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) {Bundle bundle = data.getExtras();String scanResult = bundle.getString("result");resultTextView.setText(scanResult);}}}
下面就是构成拍摄页面的部分由于很多都是继承的代码,这里我就不粘贴了,给大家一个图看一下,下面放出整理后的代码。
最后别忘了给权限部分:
<?xml version="1.0" encoding="utf-8"?>
效果图:
更多相关文章
- Android天气预报项目
- Android(安卓)按键驱动
- 删除android ScrollView边界阴影方法
- android计算器布局(TableLayout)
- android秒表计时代码
- Android利用tcpdump抓包
- android带图片的AlertDialog和文件管理器(代码)
- Android中隐藏ActionBar的方法
- Android去掉头部标题