android之基于Zxing二维码扫描
生活随笔
收集整理的這篇文章主要介紹了
android之基于Zxing二维码扫描
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
做的功能極其簡單,就是掃個二維碼,如果是合法網址,就使用webView打開
APK:http://fir.im/3uqc
開發環境:android studio
其實說實話,這種代碼一百度一堆,這位前輩的就可以?http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html
上面的代碼有個問題就是是橫屏掃描的,想修改為豎屏的,可以參考這位前輩:http://blog.csdn.net/chenbin520/article/details/16362459
還把掃描框擴大了
看看代碼結構:
識別的還可以吧,反正是做出來了,雖然不是很好看,
當時在修改橫豎屏的時候,想著改成動態的,即手機橫屏時橫屏識別,豎屏時豎屏識別,寫代碼時遇到一些問題,先在突然知道怎么解決啦
搞一個全局變量,在要修改的地方判斷一下,是橫屏就按橫屏的方式處理,在activity里
//重寫onConfigurationChanged()方法@Overridepublic void onConfigurationChanged(Configuration newConfig) {// TODO Auto-generated method stubsuper.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 #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; } // 下句第二參數要根據豎屏修改 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,接受方就響應的使用getExtraintent.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;@Overrideprotected 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() {@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {// TODO Auto-generated method stub//返回值是true的時候控制去WebView打開,為false調用系統瀏覽器或第三方瀏覽器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
總結
以上是生活随笔為你收集整理的android之基于Zxing二维码扫描的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 读决战大数据-车品觉
- 下一篇: 三菱FX5U plc个人学习时写的功能样