0810
如题,二维码在物流领域的应用研究是我毕业的论文设计。在此拿出来分享一下,充实一下个人博客库。
先说明一下题目的意义,当前物流飞速发展,伴随着就产生了信息泄露,不法分子利用快递单上的重要信息进行不法行为,严重侵犯了客户的隐私甚至安全。而我们做的就是从快递单下手,将快递单的重要信息如收件地址和联系电话等重要信息生成二维码,表面只用显示大概地址和部分手机号码。当然我们也要对极其重要的门牌号和联系电话进行加密,这样即便不法分子利用移动设备扫描出来的也不是真实信息。
另一个功能在邮递员送货上门前,用设备进行扫描,得出具体信息。
由于网上有很多代码足可以实现二维码的生成和解码,具体不再阐述,本人选择zing包,zing大大缩减了代码量,只保留扫码和解码功能,因此选用zing包。
在Android项目下的res文件夹里的layout里设计页面,这相当于web网站上的前端,这也是在手机里显示的页面,所以美观问题不美观主要看这里。
后台代码主要写在src里面的包里,这里我定义了一个zknu.qrcord包,包里面的activemain文件里写后台代码,这里面有执行为主要函数main函数,整个函数从这里开始,当然我们也可以在外部定义一个文件,从这里进行引用,由于这个程序的代码并不多,因此我写在了这一文件里,贴上代码
package com.zknu.qrcode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.io.InputStream;
import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class MainActivity extends Activity implements OnWheelChangedListener {
private Button button1Button;
private EditText sjxixiEditText;
private EditText lianxifangshi;
String xinxiString;
private JSONObject mJsonObj;
/**
* 省的WheelView控件
*/
private WheelView mProvince;
/**
* 市的WheelView控件
*/
private WheelView mCity;
/**
* 区的WheelView控件
*/
private WheelView mArea;
/**
* 所有省
*/
private String[] mProvinceDatas;
/**
* key - 省 value - 市s
*/
private Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
/**
* key - 市 values - 区s
*/
private Map<String, String[]> mAreaDatasMap = new HashMap<String, String[]>();
/**
* 当前省的名称
*/
private String mCurrentProviceName;
/**
* 当前市的名称
*/
private String mCurrentCityName;
/**
* 当前区的名称
*/
private String mCurrentAreaName = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dingdantijiao);
initJsonData();
mProvince = (WheelView) findViewById(R.id.id_province);
mCity = (WheelView) findViewById(R.id.id_city);
mArea = (WheelView) findViewById(R.id.id_area);
initDatas();
mProvince.setViewAdapter(new ArrayWheelAdapter<String>(this,
mProvinceDatas));
// 添加change事件
mProvince.addChangingListener(this);
// 添加change事件
mCity.addChangingListener(this);
// 添加change事件
mArea.addChangingListener(this);
mProvince.setVisibleItems(5);
mCity.setVisibleItems(5);
mArea.setVisibleItems(5);
updateCities();
updateAreas();
button1Button = (Button) findViewById(R.id.button1);
sjxixiEditText = (EditText) findViewById(R.id.shoujianrendizhi);
lianxifangshi = (EditText) findViewById(R.id.lianxixinxi);
button1Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String mess =mCurrentProviceName + mCurrentCityName + mCurrentAreaName+ sjxixiEditText.getText().toString();
String numberString = lianxifangshi.getText().toString();
String regExp = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
Pattern c = Pattern.compile(regExp);
Matcher x = c.matcher(numberString);
if (x.find() == true) {
long number = Long.parseLong(numberString); // Integer.parseInt(numberString);
number = number + 12345000;
String[] s = new String[4];
String[] z = new String[5];
int[] A = new int[4];
int i = 0, k = 0;
if (mess.equals("")) {
Toast.makeText(MainActivity.this, "请输入地址",
Toast.LENGTH_SHORT).show();
} else {
String regex = "\\d*";
String regex2 = "\\D*";
Pattern p = Pattern.compile(regex);
Pattern p2 = Pattern.compile(regex2);
Matcher m = p.matcher(mess);
Matcher m2 = p2.matcher(mess);
while (m.find()) {
if (!"".equals(m.group())) {
s[i] = m.group();
A[i] = Integer.parseInt(s[i]);
A[i] = A[i] + 2;
i++;
if (i > 3) {
Toast.makeText(MainActivity.this,
"输入错误,不能输入过多数字", Toast.LENGTH_SHORT)
.show();
}
}
}
while (m2.find()) {
if (!"".equals(m2.group())) {
z[k] = m2.group();
k++;
if (k > 4) {
Toast.makeText(MainActivity.this,
"输入错误,不能输入过多字符", Toast.LENGTH_SHORT)
.show();
}
}
}
xinxiString = z[0] + A[0] + z[1] + A[1] + z[2] + A[2]
+ z[3];
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("收件人地址:\n");
stringList.add(xinxiString+"\n");
stringList.add("收件人电话:\n");
stringList.add(String.valueOf(number));
Intent intent = new Intent();
intent.setClass(MainActivity.this, qrcoded.class);
intent.putStringArrayListExtra("ListString", stringList);
startActivity(intent);
}
} else {
Toast.makeText(MainActivity.this, "手机号有误",
Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* 根据当前的市,更新区WheelView的信息
*/
private void updateAreas() {
int pCurrent = mCity.getCurrentItem();
mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
String[] areas = mAreaDatasMap.get(mCurrentCityName);
if (areas == null) {
areas = new String[] { "" };
}
mArea.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));
mArea.setCurrentItem(0);
}
/**
* 根据当前的省,更新市WheelView的信息
*/
private void updateCities() {
int pCurrent = mProvince.getCurrentItem();
mCurrentProviceName = mProvinceDatas[pCurrent];
String[] cities = mCitisDatasMap.get(mCurrentProviceName);
if (cities == null) {
cities = new String[] { "" };
}
mCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
mCity.setCurrentItem(0);
updateAreas();
}
/**
* 解析整个Json对象,完成后释放Json对象的内存
*/
private void initDatas() {
try {
JSONArray jsonArray = mJsonObj.getJSONArray("citylist");
mProvinceDatas = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonP = jsonArray.getJSONObject(i);// 每个省的json对象
String province = jsonP.getString("p");// 省名字
mProvinceDatas[i] = province;
JSONArray jsonCs = null;
try {
/**
* Throws JSONException if the mapping doesn't exist or is
* not a JSONArray.
*/
jsonCs = jsonP.getJSONArray("c");
} catch (Exception e1) {
continue;
}
String[] mCitiesDatas = new String[jsonCs.length()];
for (int j = 0; j < jsonCs.length(); j++) {
JSONObject jsonCity = jsonCs.getJSONObject(j);
String city = jsonCity.getString("n");// 市名字
mCitiesDatas[j] = city;
JSONArray jsonAreas = null;
try {
/**
* Throws JSONException if the mapping doesn't exist or
* is not a JSONArray.
*/
jsonAreas = jsonCity.getJSONArray("a");
} catch (Exception e) {
continue;
}
String[] mAreasDatas = new String[jsonAreas.length()];// 当前市的所有区
for (int k = 0; k < jsonAreas.length(); k++) {
String area = jsonAreas.getJSONObject(k).getString("s");// 区域的名称
mAreasDatas[k] = area;
}
mAreaDatasMap.put(city, mAreasDatas);
}
mCitisDatasMap.put(province, mCitiesDatas);
}
} catch (JSONException e) {
e.printStackTrace();
}
mJsonObj = null;
}
/**
* 从assert文件夹中读取省市区的json文件,然后转化为json对象
*/
private void initJsonData() {
try {
StringBuffer sb = new StringBuffer();
InputStream is = getAssets().open("city.json");
int len = -1;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
sb.append(new String(buf, 0, len, "gbk"));
}
is.close();
mJsonObj = new JSONObject(sb.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* change事件的处理
*/
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
mCurrentAreaName="";
if (wheel == mProvince) {
updateCities();
} else if (wheel == mCity) {
updateAreas();
} else if (wheel == mArea) {
mCurrentAreaName = mAreaDatasMap.get(mCurrentCityName)[newValue];
}
}
public void showChoose(View view) {
Toast.makeText(this,
mCurrentProviceName + mCurrentCityName + mCurrentAreaName+sjxixiEditText.getText().toString(), 3)
.show();
}
}
因为在填写收货地址时需要选用地址,因此我们需要做一个三级联动,上面的代码里有部分代码为选择收货地址的代码。
当然具体的手机号码或者是具体的收货地址如门牌号等需要客户自己填写。
点击确认后后台自动获取填写的详细地址和手机号码,后台进行加密。
在物件送到到大概地址时,快递员利用手持设备进行扫描解码,获得具体地址,然后配货。
代码陪上:
package com.zknu.scanqrcode;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.zxing.activity.CaptureActivity;
import com.zxing.decoding.CaptureActivityHandler;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button scanbButton;
private TextView textViews;
private Button callButton;
private Button messageButton;
String xinxiString;
long number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanbButton = (Button) findViewById(R.id.scan);
textViews = (TextView) findViewById(R.id.text);
callButton = (Button) findViewById(R.id.call);
messageButton = (Button) findViewById(R.id.message);
scanbButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { // TODO Auto-generated method stub
Intent startScan = new Intent(MainActivity.this,
CaptureActivity.class);
startActivityForResult(startScan, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO
// Auto-generated
// method
// stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String result = data.getExtras().getString("result");
if (result != "") {
callButton.setVisibility(0);
messageButton.setVisibility(0);
String[] s = new String[4];
String[] z = new String[5];
long[] A = new long[4];
int i = 0, k = 0;
if (result.equals("")) {
Toast.makeText(MainActivity.this, "请输入地址",
Toast.LENGTH_SHORT).show();
} else {
String regex = "\\d*";
String regex2 = "\\D*";
Pattern p = Pattern.compile(regex);
Pattern p2 = Pattern.compile(regex2);
Matcher m = p.matcher(result);
Matcher m2 = p2.matcher(result);
while (m.find()) {
if (!"".equals(m.group())) {
s[i] = m.group();
A[i] = Long.parseLong(s[i]);
A[i] = A[i] - 2;
i++;
}
}
while (m2.find()) {
if (!"".equals(m2.group())) {
z[k] = m2.group();
k++;
}
}
}
xinxiString = z[0] + A[0] + z[1] + A[1] + z[2] + A[2] + z[3];
// 手机号
Pattern pattern = Pattern.compile("(1|861)(3|5|8)\\d{9}$*");
Matcher matcher = pattern.matcher(result);
StringBuffer numberBuffer = new StringBuffer();
while (matcher.find()) {
numberBuffer.append(matcher.group()).append(",");
}
int len = numberBuffer.length();
if (len > 0) {
numberBuffer.deleteCharAt(len - 1);
}
number = Long.parseLong(numberBuffer.toString());
number = number - 12345000;
textViews.setText(xinxiString + String.valueOf(number));
}
// textViews.setText(result);
}
}
public void Call(View view) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
+ String.valueOf(number)));
startActivity(intent);
}
public void sendSMSs(View view) {
// 初始化发短信SmsManager类
android.telephony.SmsManager smsManager = android.telephony.SmsManager
.getDefault();
// 拆分短信内容(手机短信长度限制)
String messages="您好,您的快递已到,即将给您送去,请保持联系。";
List<String> divideContents = smsManager.divideMessage(messages);
for (String text : divideContents) {
smsManager.sendTextMessage(String.valueOf(number), null, text,
null, null);
}
Toast.makeText(this, "Send Message Success!", Toast.LENGTH_SHORT)
.show();
}
}
以上代码,在获得收获地址时,进行拆分,将加密过的信息按照约定好的加密方式进行解密,之后获取正确的收货地址,手持设备自动进行发短信打电话等功能。
利用二维码,从一定程度上减轻了信息泄露的事件。
项目下载地址: