做的功能极其简单,就是扫个二维码,如果是合法网址,就使用webView打开
开发环境:android studio
其实说实话,这种代码一百度一堆,这位前辈的就可以 http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html
上面的代码有个问题就是是横屏扫描的,想修改为竖屏的,可以参考这位前辈:http://blog.youkuaiyun.com/chenbin520/article/details/16362459
还把扫描框扩大了
看看代码结构:
识别的还可以吧,反正是做出来了,虽然不是很好看,
当时在修改横竖屏的时候,想着改成动态的,即手机横屏时横屏识别,竖屏时竖屏识别,写代码时遇到一些问题,先在突然知道怎么解决啦
搞一个全局变量,在要修改的地方判断一下,是横屏就按横屏的方式处理,在activity里
//重写onConfigurationChanged()方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//横向
//重新实例化组件和设置监听
}else{
//竖向
//重新实例化组件和设置监听
}
}
oncreate方法里也要加上判断
//加上判断 屏幕是横屏还是竖屏
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
//横向
}else {
//竖向
}
在判断里修改全局变量的属性就可以啦,该竖屏需要修改:
1.在DecodeHandler.java中,修改decode方法
PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);
为
byte[] rotatedData = new byte[data.length];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++)
rotatedData[x * height + height - y - 1] = data[x + y * width];
}
int tmp = width; // Here we are swapping, that's the difference to #11
width = height;
height = tmp;
PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);
2.在CameraManager.java中,注释代码:
// rect.left = rect.left * cameraResolution.x / screenResolution.x;
// rect.right = rect.right * cameraResolution.x / screenResolution.x;
// rect.top = rect.top * cameraResolution.y / screenResolution.y;
// rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
修改为
rect.left = rect.left * cameraResolution.y / screenResolution.x;
rect.right = rect.right * cameraResolution.y / screenResolution.x;
rect.top = rect.top * cameraResolution.x / screenResolution.y;
rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;
3.在CameraConfigurationManag
camera.setDisplayOrientation(90);
4.在AndroidManifest.xml中,把Activity的属性android:screenOrientation="landscape"
改为
android:screenOrientation="portrait"
5.
在CameraConfigurationManager中的initFromCameraParameters()方法的Log.d(TAG, "Screen resolution: " + screenResolution);句后面添加如下代码, 这段代码是为了解决摄像头竖过来后图像拉伸的问题:
//为竖屏添加
Point screenResolutionForCamera =new Point();
screenResolutionForCamera.x = screenResolution.x;
screenResolutionForCamera.y = screenResolution.y;
if (screenResolution.x < screenResolution.y) {
screenResolutionForCamera.x = screenResolution.y;
screenResolutionForCamera.y = screenResolution.x;
} // 下句第二参数要根据竖屏修改
cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);
这样就修改为竖屏了
识别出来后使用WebView打开网页,在打开之前先判断一下是不是合法网址
//判断是否是合法网址
private Boolean check(String str){
String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]" ;
Pattern patt = Pattern. compile(regex);
Matcher matcher = patt.matcher(str);
if(matcher.matches()){
url = str;
// http://36kr.com/ 这样的也是合法地址,但是要去掉最后一位
if(str.lastIndexOf("/") == (str.length()-1)){
url = str.substring(0,(str.length()-1));
}
return true;
}
return false;
}
本来想通过setContentView()修改当前文件的布局,后来发现有问题,一直报错,写这篇的时候想起来了,因为调用了摄像头,应该是先把摄像头关闭,在使用这个方法更改布局,当时的处理是新开一个Activity ,专门用作WebView ,所以需要两个activity 之间传值,
有两种方式,一种是设置一个全局变量,activity 间共享,还有一种是使用Bundle,我使用了第二种
发送:
Intent intent = new Intent();
//第一参数取的是这个应用程序的Context,生命周期是整个应用
//第二个参数是要跳转的页面的全路径
intent.setClassName(getApplicationContext(), "android.cl.com.zxing02.WebViewActivity");
//Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值
Bundle b = new Bundle();
b.putString("url", url);
//此处使用putExtras,接受方就响应的使用getExtra
intent.putExtras(b);
startActivity(intent);
// 关闭当前页面
System.exit(0);
接受:
//最后的参数一定要和发送方的相同,否则得到空值
String url = getIntent().getExtras().getString("url");
还有就是网页在加载时会有一段时间,这段时间我使用了一个 ProgressDialog,配合网页加载的两个函数使用
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
private ProgressDialog roundProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.open_in_web);
//最后的参数一定要和发送方的相同,否则得到空值
String url = getIntent().getExtras().getString("url");
// http://36kr.com/ 类似这样的,需要处理成 http://36kr.com
// if(url.lastIndexOf("/") == (url.length()-1)){
// url = url.substring(0,(url.length()-1));
// }
// Log.e("msg",url);
//WebView加载web资
webView = (WebView) findViewById(R.id.webView);
// webView.loadUrl("http://baidu.com");
webView.loadUrl(url);
roundProgressDialog = new ProgressDialog(WebViewActivity.this);// 创建ProgressDialog对象
roundProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条风格,风格为圆形,旋转的
// roundProgressDialog.setTitle("提示");// 设置ProgressDialog 标题
roundProgressDialog.setMessage("Just a minute...");// 设置ProgressDialog提示信息
// roundProgressDialog.setIcon(R.drawable.icon);// 设置ProgressDialog标题图标
// // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
roundProgressDialog.setIndeterminate(false);
roundProgressDialog.setCancelable(true); // 设置ProgressDialog 是否可以按退回键取消
//覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
roundProgressDialog.show();
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
roundProgressDialog.hide();
}
});
}
}
这样就解决了
module源码:http://download.youkuaiyun.com/detail/i_do_can/9395023