简单二维码生成以及识别-创新互联
1. 二维码
目前创新互联已为成百上千的企业提供了网站建设、域名、虚拟主机、网站托管、企业网站设计、名山网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。本质:生成过程是将字符串转换为图形
识别:扫描图形转换为字符串
首先需要依赖lib_QRCode库(在附件中可以下载)
1.在代码中设置
package com.example.ex_templete;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.ericssonlabs.BarCodeTestActivity;
import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;
public class MainActivity extends Activity {
private ImageView mImageView;
public void scan(View v) {
//打开扫描界面扫描条形码或二维码
Intent openCameraIntent = new Intent(this, CaptureActivity.class);
startActivityForResult(openCameraIntent, 0);
}
@Override
protected 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);
}
}
public void onClick(View v) {
try {
String contentString = "http://www.baidu.com";
if (contentString != null && contentString.trim().length() > 0) {
// 根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(350*350)
Bitmap qrCodeBitmap = EncodingHandler.createQRCode(
contentString, 350);
mImageView.setImageBitmap(qrCodeBitmap);
} else {
Toast.makeText(this, "Text can not be empty",
Toast.LENGTH_SHORT).show();
}
} catch (WriterException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.p_w_picpathView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2.xml中
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" tools:context="com.example.ex_templete.MainActivity" >
3.在文档中配置权限以及注册activity
package="com.xinbo.demo" android:versionCode="1" android:versionName="1.0" > android:minSdkVersion="8" android:targetSdkVersion="18" /> android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > android:name="com.xinbo.demo.MainActivity" android:label="@string/app_name" > android:name="com.zxing.activity.CaptureActivity" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden" >
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享标题:简单二维码生成以及识别-创新互联
URL网址:http://ybzwz.com/article/csddeo.html