android之基于Zxing二维码扫描
做的功能極其簡(jiǎn)單,就是掃個(gè)二維碼,如果是合法網(wǎng)址,就使用webView打開
APK:http://fir.im/3uqc
開發(fā)環(huán)境:android studio
其實(shí)說實(shí)話,這種代碼一百度一堆,這位前輩的就可以?http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html
上面的代碼有個(gè)問題就是是橫屏掃描的,想修改為豎屏的,可以參考這位前輩:http://blog.csdn.net/chenbin520/article/details/16362459
還把掃描框擴(kuò)大了
看看代碼結(jié)構(gòu):
識(shí)別的還可以吧,反正是做出來了,雖然不是很好看,
當(dāng)時(shí)在修改橫豎屏的時(shí)候,想著改成動(dòng)態(tài)的,即手機(jī)橫屏?xí)r橫屏識(shí)別,豎屏?xí)r豎屏識(shí)別,寫代碼時(shí)遇到一些問題,先在突然知道怎么解決啦
搞一個(gè)全局變量,在要修改的地方判斷一下,是橫屏就按橫屏的方式處理,在activity里
//重寫onConfigurationChanged()方法@Overridepublic void onConfigurationChanged(Configuration newConfig) {// TODO Auto-generated method stubsuper.onConfigurationChanged(newConfig);if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){//橫向//重新實(shí)例化組件和設(shè)置監(jiān)聽}else{//豎向//重新實(shí)例化組件和設(shè)置監(jiān)聽}}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 #11width = 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 er.java中,在setDesiredCameraParamete rs方法中添加一句
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; } // 下句第二參數(shù)要根據(jù)豎屏修改 cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);這樣就修改為豎屏了
識(shí)別出來后使用WebView打開網(wǎng)頁(yè),在打開之前先判斷一下是不是合法網(wǎng)址
//判斷是否是合法網(wǎng)址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()修改當(dāng)前文件的布局,后來發(fā)現(xiàn)有問題,一直報(bào)錯(cuò),寫這篇的時(shí)候想起來了,因?yàn)檎{(diào)用了攝像頭,應(yīng)該是先把攝像頭關(guān)閉,在使用這個(gè)方法更改布局,當(dāng)時(shí)的處理是新開一個(gè)Activity ,專門用作WebView ,所以需要兩個(gè)activity 之間傳值,有兩種方式,一種是設(shè)置一個(gè)全局變量,activity 間共享,還有一種是使用Bundle,我使用了第二種
發(fā)送:
Intent intent = new Intent();//第一參數(shù)取的是這個(gè)應(yīng)用程序的Context,生命周期是整個(gè)應(yīng)用//第二個(gè)參數(shù)是要跳轉(zhuǎn)的頁(yè)面的全路徑intent.setClassName(getApplicationContext(), "android.cl.com.zxing02.WebViewActivity");//Bundle類用作攜帶數(shù)據(jù),它類似于Map,用于存放key-value名值對(duì)形式的值Bundle b = new Bundle();b.putString("url", url);//此處使用putExtras,接受方就響應(yīng)的使用getExtraintent.putExtras(b);startActivity(intent);// 關(guān)閉當(dāng)前頁(yè)面System.exit(0);接受: //最后的參數(shù)一定要和發(fā)送方的相同,否則得到空值String url = getIntent().getExtras().getString("url");還有就是網(wǎng)頁(yè)在加載時(shí)會(huì)有一段時(shí)間,這段時(shí)間我使用了一個(gè)?ProgressDialog,配合網(wǎng)頁(yè)加載的兩個(gè)函數(shù)使用 public class WebViewActivity extends AppCompatActivity {private WebView webView;private ProgressDialog roundProgressDialog;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.open_in_web);//最后的參數(shù)一定要和發(fā)送方的相同,否則得到空值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);// 創(chuàng)建ProgressDialog對(duì)象roundProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 設(shè)置進(jìn)度條風(fēng)格,風(fēng)格為圓形,旋轉(zhuǎn)的 // roundProgressDialog.setTitle("提示");// 設(shè)置ProgressDialog 標(biāo)題roundProgressDialog.setMessage("Just a minute...");// 設(shè)置ProgressDialog提示信息 // roundProgressDialog.setIcon(R.drawable.icon);// 設(shè)置ProgressDialog標(biāo)題圖標(biāo) // // 設(shè)置ProgressDialog 的進(jìn)度條是否不明確 false 就是不設(shè)置為不明確roundProgressDialog.setIndeterminate(false);roundProgressDialog.setCancelable(true); // 設(shè)置ProgressDialog 是否可以按退回鍵取消//覆蓋WebView默認(rèn)使用第三方或系統(tǒng)默認(rèn)瀏覽器打開網(wǎng)頁(yè)的行為,使網(wǎng)頁(yè)用WebView打開webView.setWebViewClient(new WebViewClient() {@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {// TODO Auto-generated method stub//返回值是true的時(shí)候控制去WebView打開,為false調(diào)用系統(tǒng)瀏覽器或第三方瀏覽器view.loadUrl(url);return true;}@Overridepublic void onPageStarted(WebView view, String url, Bitmap favicon) {// TODO Auto-generated method stubsuper.onPageStarted(view, url, favicon);roundProgressDialog.show();}@Overridepublic void onPageFinished(WebView view, String url) {// TODO Auto-generated method stubsuper.onPageFinished(view, url);roundProgressDialog.hide();}});}}這樣就解決了
module源碼:http://download.csdn.net/detail/i_do_can/9395023
總結(jié)
以上是生活随笔為你收集整理的android之基于Zxing二维码扫描的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 读决战大数据-车品觉
- 下一篇: 三菱FX5U plc个人学习时写的功能样