首页 > 代码库 > [Unity+Android]横版扫描二维码

[Unity+Android]横版扫描二维码

终于解决了一个忧伤好久的问题,严重拖了项目进度,深感惭愧!一直被一系列的问题所困扰,然后又只能自己一个人摸索,也是一段辛酸忧伤史,现在小结一下上个月在做二维码的过程中所碰到的问题以及解决办法,现在庆幸终于解决好了,终于能将这个功能告一段落,一下小结也是分享一下Unity的某些“坑”,让同行少走弯路,起码在二维码这方面应该会有所启迪,欣慰的是接下来几天终于可以做自己应该做的事情了!

效果图:

20140506015422031

20140506015441296

先小结一下碰到的问题:

1.Unity工程屏幕方向与Android工程屏幕方向要一致的问题

本来我测试Android扫码的功能,已经测是通过了,可以参考之前写的博文:http://blog.csdn.net/dingxiaowei2013/article/details/24677795,后来将这这个插件添加到现有的项目中,但导出项目apk后,打开应用怎么就是闪退,表示很忧伤,后来一步一步的精简对比测试,表示自己五一那天一直摸索到凌晨三点才发现这一问题根源,有了这个基础才有接下来思路的转变。我的Android工程设置的屏幕旋转方向是portrait,但由于项目原因,Unity的导出方向必须是landscapeleft,这两者方向不一致会导致应用异常奔溃闪退!当然我的解决思路可能是比较笨的,我将Android工程做成的插件的屏幕方向也做成了横屏的,为了和Unity的方向一致,我想应该就不会出现那种问题。我原本的思路是将Unity导出屏幕设置成auto,然后代码控制屏幕的方向,但貌似还是不成功,没辙只能还是换种思路!网上有不少Android的扫码工程,最多的就是基于zxing,但是都是有很多冗余,不适合初学者,尤其是像我这种Android初学者,我倒是看到一个比较适合入门的精简的扫码工程,可惜是纵屏的,上一篇文章就是介绍的这个,但并不适合我的项目需求,所以我就想办法将这个工程改成横屏工程,本以为只要改改xml配置文件就OK的,结果并不是想象的那么简单,仅仅那么该会出现扫的过程中图片压缩以及移动别扭等现象,还是需要修改里面的工程的!这里可以参考:http://dingxiaowei2013.blog.163.com/blog/static/21965310720144595534507/,这篇是将横屏改纵屏的解决方案,然后将其逆序操作,但我还是出现了一些问题,比如扫到一半的时候应用异常崩溃,又表示很忧伤,为啥都不是一帆风顺呢!然后反复重做,终于成功了!精简版的工程源码我会贴出来共享!为了实现这个功能,可谓是一次有一次的导出,一次有一次的测试,千言万语无法言表其中的忧伤,总而言之,坚韧,是程序猿必须具备的品质!

2.Unity与Android场景跳转的问题

Unity和Android交互不仅仅是调用函数,交互数据,很大的一个还是需要交互视图和场景,将其很好的进行切换。当然场景的跳转或者是切换还是通过调用接口函数来实现,但这里通过了一个很重要的Activity来实现了这一效果,就是UnityPlayerActivity,Android的接口视图就是继承了这个UnityPlayerActivity,这个是为Unity和Android搭建了一个桥梁,当然这个类必须导入Unity的接口包,在Unity的安装目录下,详细可以参考上一篇文章,继承了UnityPlayerActivity的视图是作为Unity和Android的一个通用视图,它是Android插件的一个入口,无论是在Unity切换到Android界面还是Android切换到Unity界面,必须都要通过这个Activity来操作,记得是必须,这里我也是吃过苦头,我想在其他的Activity中来调用Unity的接口函数来实现Unity界面的跳转,但都尝试失败!后来再重头再来,这些都是经过了一遍又一遍的实验才得出的结论!

3.LG的手机作为Unity的测试机貌似有一些问题(纯属个人感觉)

我测试最简单的OnGUI的方法,GUI里面包含中文,LG的显示不出来,由于公司所有的Android机和个人手机基本都是LG的,测试下来中文不显示,估计还得做字体,难道是因为它是韩国货?!纯属我个人瞎猜,解决办法就是你可以在工程里面做一套字体,然后做GUISkin供GUI用,我用其他的手机,比如我的破旧的中兴还有其他的非LG手机就能显示,这个横版的二维码扫码插件工程用LG测试还是有点问题,会出现扫的时候卡屏的现像,不知道是不是因为内存飙升,这个可以用Eclipse测试一下看看是不是因为这个原因!

附带修改的Android精简版的扫码工程代码(只贴修改过的脚本):

AndroidManifest.xml

[java]view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="com.example.qr_codescan"

  4.    android:versionCode="1"

  5.    android:versionName="1.0" >  

  6.    <uses-sdk  

  7.        android:minSdkVersion="8"

  8.        android:targetSdkVersion="16" />  

  9.    <application  

  10.        android:allowBackup="true"

  11.        android:icon="@drawable/ic_launcher"

  12.        android:label="@string/app_name"

  13.        android:theme="@android:style/Theme.NoTitleBar" >  

  14.           <activity  

  15.            android:name="com.example.qr_codescan.MainActivity"

  16.            android:screenOrientation="landscape"

  17.            android:label="@string/app_name" >  

  18.            <intent-filter>  

  19.                <action android:name="android.intent.action.MAIN" />  

  20.                <category android:name="android.intent.category.LAUNCHER" />  

  21.            </intent-filter>  

  22.        </activity>  

  23.        <activity  

  24.            android:name=".MipcaActivityCapture"

  25.            android:configChanges="orientation|keyboardHidden"

  26.            android:screenOrientation="landscape"

  27.            android:windowSoftInputMode="stateAlwaysHidden" >  

  28.        </activity>  

  29.    </application>  

  30.    <uses-permission android:name="android.permission.VIBRATE" />  

  31.    <uses-permission android:name="android.permission.CAMERA" />  

  32.    <uses-feature android:name="android.hardware.camera" />  

  33.    <uses-feature android:name="android.hardware.camera.autofocus" />  

  34. </manifest>  

MainActivity.java

[java]view plaincopyprint?
  1. package com.example.qr_codescan;  

  2. import android.app.Activity;  

  3. import android.content.Intent;  

  4. import android.graphics.Bitmap;  

  5. import android.os.Bundle;  

  6. import android.view.View;  

  7. import android.view.View.OnClickListener;  

  8. import android.widget.Button;  

  9. import android.widget.ImageView;  

  10. import android.widget.TextView;  

  11. import com.unity3d.player.UnityPlayer;  

  12. import com.unity3d.player.UnityPlayerActivity;  

  13. publicclass MainActivity extends UnityPlayerActivity {  

  14. privatefinalstaticint SCANNIN_GREQUEST_CODE = 1;  

  15. /**

  16.     * 显示扫描结果

  17.     */

  18. //  private TextView mTextView ;

  19. /**

  20.     * 显示扫描拍的图片

  21.     */

  22. //  private ImageView mImageView;

  23. @Override

  24. protectedvoid onCreate(Bundle savedInstanceState) {  

  25. super.onCreate(savedInstanceState);  

  26. //      setContentView(R.layout.activity_main);

  27. //      

  28. //      mTextView = (TextView) findViewById(R.id.result);

  29. //      mImageView = (ImageView) findViewById(R.id.qrcode_bitmap);

  30. //      

  31. //      //点击按钮跳转到二维码扫描界面,这里用的是startActivityForResult跳转

  32. //      //扫描完了之后调到该界面

  33. //      Button mButton = (Button) findViewById(R.id.button1);

  34. //      mButton.setOnClickListener(new OnClickListener() {

  35. //          

  36. //          @Override

  37. //          public void onClick(View v) {

  38. //              Intent intent = new Intent();

  39. //              intent.setClass(MainActivity.this, MipcaActivityCapture.class);

  40. //              intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

  41. //              startActivityForResult(intent, SCANNIN_GREQUEST_CODE);

  42. //          }

  43. //      });

  44.    }  

  45. publicvoid Show()  

  46.    {  

  47.        Intent intent = new Intent();  

  48.        intent.setClass(MainActivity.this, MipcaActivityCapture.class);  

  49.        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  

  50.        startActivityForResult(intent, SCANNIN_GREQUEST_CODE);  

  51.    }  

  52. @Override

  53. protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {  

  54. super.onActivityResult(requestCode, resultCode, data);  

  55. switch (requestCode) {  

  56. case SCANNIN_GREQUEST_CODE:  

  57. if(resultCode == RESULT_OK){  

  58.                Bundle bundle = data.getExtras();  

  59. //显示扫描到的内容

  60. //              mTextView.setText(bundle.getString("result"));

  61. //显示

  62. //              mImageView.setImageBitmap((Bitmap) data.getParcelableExtra("bitmap"));

  63.                UnityPlayer.UnitySendMessage("GameObject","GetString",bundle.getString("result"));  

  64.            }  

  65. break;  

  66.        }  

  67.    }    

  68. }  


MipcaActivityCapture.java

修改中文编码
characterSet = "GBK";

CameraConfigurationManager.java

[java]view plaincopyprint?
  1. /*

  2. * Copyright (C) 2010 ZXing authors

  3. *

  4. * Licensed under the Apache License, Version 2.0 (the "License");

  5. * you may not use this file except in compliance with the License.

  6. * You may obtain a copy of the License at

  7. *

  8. *      http://www.apache.org/licenses/LICENSE-2.0

  9. *

  10. * Unless required by applicable law or agreed to in writing, software

  11. * distributed under the License is distributed on an "AS IS" BASIS,

  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  13. * See the License for the specific language governing permissions and

  14. * limitations under the License.

  15. */

  16. package com.mining.app.zxing.camera;  

  17. import android.content.Context;  

  18. import android.graphics.Point;  

  19. import android.hardware.Camera;  

  20. import android.os.Build;  

  21. import android.util.Log;  

  22. import android.view.Display;  

  23. import android.view.WindowManager;  

  24. import java.lang.reflect.Method;  

  25. import java.util.regex.Pattern;  

  26. finalclass CameraConfigurationManager {  

  27. privatestaticfinal String TAG = CameraConfigurationManager.class.getSimpleName();  

  28. privatestaticfinalint TEN_DESIRED_ZOOM = 27;  

  29. privatestaticfinalint DESIRED_SHARPNESS = 30;  

  30. privatestaticfinal Pattern COMMA_PATTERN = Pattern.compile(",");  

  31. privatefinal Context context;  

  32. private Point screenResolution;  

  33. private Point cameraResolution;  

  34. privateint previewFormat;  

  35. private String previewFormatString;  

  36.  CameraConfigurationManager(Context context) {  

  37. this.context = context;  

  38.  }  

  39. /**

  40.   * Reads, one time, values from the camera that are needed by the app.

  41.   */

  42. void initFromCameraParameters(Camera camera) {  

  43.    Camera.Parameters parameters = camera.getParameters();  

  44.    previewFormat = parameters.getPreviewFormat();  

  45.    previewFormatString = parameters.get("preview-format");  

  46.    Log.d(TAG, "Default preview format: " + previewFormat + ‘/‘ + previewFormatString);  

  47.    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  

  48.    Display display = manager.getDefaultDisplay();  

  49.    screenResolution = new Point(display.getWidth(), display.getHeight());  

  50.    Log.d(TAG, "Screen resolution: " + screenResolution);  

  51.    cameraResolution = getCameraResolution(parameters, screenResolution);  

  52.    Log.d(TAG, "Camera resolution: " + screenResolution);  

  53.  }  

  54. /**

  55.   * Sets the camera up to take preview images which are used for both preview and decoding.

  56.   * We detect the preview format here so that buildLuminanceSource() can build an appropriate

  57.   * LuminanceSource subclass. In the future we may want to force YUV420SP as it‘s the smallest,

  58.   * and the planar Y can be used for barcode scanning without a copy in some cases.

  59.   */

  60. void setDesiredCameraParameters(Camera camera) {  

  61.    Camera.Parameters parameters = camera.getParameters();  

  62.    Log.d(TAG, "Setting preview size: " + cameraResolution);  

  63.    parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);  

  64.    setFlash(parameters);  

  65.    setZoom(parameters);  

  66. //setSharpness(parameters);

  67. //modify here

  68. //    camera.setDisplayOrientation(90);

  69. //兼容2.1

  70. //setDisplayOrientation(camera, 90);

  71.    camera.setParameters(parameters);  

  72.  }  

  73.  Point getCameraResolution() {  

  74. return cameraResolution;  

  75.  }  

  76.  Point getScreenResolution() {  

  77. return screenResolution;  

  78.  }  

  79. int getPreviewFormat() {  

  80. return previewFormat;  

  81.  }  

  82.  String getPreviewFormatString() {  

  83. return previewFormatString;  

  84.  }  

  85. privatestatic Point getCameraResolution(Camera.Parameters parameters, Point screenResolution) {  

  86.    String previewSizeValueString = parameters.get("preview-size-values");  

  87. // saw this on Xperia

  88. if (previewSizeValueString == null) {  

  89.      previewSizeValueString = parameters.get("preview-size-value");  

  90.    }  

  91.    Point cameraResolution = null;  

  92. if (previewSizeValueString != null) {  

  93.      Log.d(TAG, "preview-size-values parameter: " + previewSizeValueString);  

  94.      cameraResolution = findBestPreviewSizeValue(previewSizeValueString, screenResolution);  

  95.    }  

  96. if (cameraResolution == null) {  

  97. // Ensure that the camera resolution is a multiple of 8, as the screen may not be.

  98.      cameraResolution = new Point(  

  99.          (screenResolution.x >> 3) << 3,  

  100.          (screenResolution.y >> 3) << 3);  

  101.    }  

  102. return cameraResolution;  

  103.  }  

  104. privatestatic Point findBestPreviewSizeValue(CharSequence previewSizeValueString, Point screenResolution) {  

  105. int bestX = 0;  

  106. int bestY = 0;  

  107. int diff = Integer.MAX_VALUE;  

  108. for (String previewSize : COMMA_PATTERN.split(previewSizeValueString)) {  

  109.      previewSize = previewSize.trim();  

  110. int dimPosition = previewSize.indexOf(‘x‘);  

  111. if (dimPosition < 0) {  

  112.        Log.w(TAG, "Bad preview-size: " + previewSize);  

  113. continue;  

  114.      }  

  115. int newX;  

  116. int newY;  

  117. try {  

  118.        newX = Integer.parseInt(previewSize.substring(0, dimPosition));  

  119.        newY = Integer.parseInt(previewSize.substring(dimPosition + 1));  

  120.      } catch (NumberFormatException nfe) {  

  121.        Log.w(TAG, "Bad preview-size: " + previewSize);  

  122. continue;  

  123.      }  

  124. int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);  

  125. if (newDiff == 0) {  

  126.        bestX = newX;  

  127.        bestY = newY;  

  128. break;  

  129.      } elseif (newDiff < diff) {  

  130.        bestX = newX;  

  131.        bestY = newY;  

  132.        diff = newDiff;  

  133.      }  

  134.    }  

  135. if (bestX > 0 && bestY > 0) {  

  136. returnnew Point(bestX, bestY);  

  137.    }  

  138. returnnull;  

  139.  }  

  140. privatestaticint findBestMotZoomValue(CharSequence stringValues, int tenDesiredZoom) {  

  141. int tenBestValue = http://www.mamicode.com/0;  

  142. for (String stringValue : COMMA_PATTERN.split(stringValues)) {  

  143.      stringValue = http://www.mamicode.com/stringValue.trim();

  144. double value;  

  145. try {  

  146.        value = http://www.mamicode.com/Double.parseDouble(stringValue);

  147.      } catch (NumberFormatException nfe) {  

  148. return tenDesiredZoom;  

  149.      }  

  150. int tenValue = http://www.mamicode.com/(int) (10.0 * value);  

  151. if (Math.abs(tenDesiredZoom - value) < Math.abs(tenDesiredZoom - tenBestValue)) {  

  152.        tenBestValue = http://www.mamicode.com/tenValue;

  153.      }  

  154.    }  

  155. return tenBestValue;  

  156.  }  

  157. privatevoid setFlash(Camera.Parameters parameters) {  

  158. // FIXME: This is a hack to turn the flash off on the Samsung Galaxy.

  159. // And this is a hack-hack to work around a different value on the Behold II

  160. // Restrict Behold II check to Cupcake, per Samsung‘s advice

  161. //if (Build.MODEL.contains("Behold II") &&

  162. //    CameraManager.SDK_INT == Build.VERSION_CODES.CUPCAKE) {

  163. if (Build.MODEL.contains("Behold II") && CameraManager.SDK_INT == 3) { // 3 = Cupcake

  164.      parameters.set("flash-value", 1);  

  165.    } else {  

  166.      parameters.set("flash-value", 2);  

  167.    }  

  168. // This is the standard setting to turn the flash off that all devices should honor.

  169.    parameters.set("flash-mode", "off");  

  170.  }  

  171. privatevoid setZoom(Camera.Parameters parameters) {  

  172.    String zoomSupportedString = parameters.get("zoom-supported");  

  173. if (zoomSupportedString != null && !Boolean.parseBoolean(zoomSupportedString)) {  

  174. return;  

  175.    }  

  176. int tenDesiredZoom = TEN_DESIRED_ZOOM;  

  177.    String maxZoomString = parameters.get("max-zoom");  

  178. if (maxZoomString != null) {  

  179. try {  

  180. int tenMaxZoom = (int) (10.0 * Double.parseDouble(maxZoomString));  

  181. if (tenDesiredZoom > tenMaxZoom) {  

  182.          tenDesiredZoom = tenMaxZoom;  

  183.        }  

  184.      } catch (NumberFormatException nfe) {  

  185.        Log.w(TAG, "Bad max-zoom: " + maxZoomString);  

  186.      }  

  187.    }  

  188.    String takingPictureZoomMaxString = parameters.get("taking-picture-zoom-max");  

  189. if (takingPictureZoomMaxString != null) {  

  190. try {  

  191. int tenMaxZoom = Integer.parseInt(takingPictureZoomMaxString);  

  192. if (tenDesiredZoom > tenMaxZoom) {  

  193.          tenDesiredZoom = tenMaxZoom;  

  194.        }  

  195.      } catch (NumberFormatException nfe) {  

  196.        Log.w(TAG, "Bad taking-picture-zoom-max: " + takingPictureZoomMaxString);  

  197.      }  

  198.    }  

  199.    String motZoomValuesString = parameters.get("mot-zoom-values");  

  200. if (motZoomValuesString != null) {  

  201.      tenDesiredZoom = findBestMotZoomValue(motZoomValuesString, tenDesiredZoom);  

  202.    }  

  203.    String motZoomStepString = parameters.get("mot-zoom-step");  

  204. if (motZoomStepString != null) {  

  205. try {  

  206. double motZoomStep = Double.parseDouble(motZoomStepString.trim());  

  207. int tenZoomStep = (int) (10.0 * motZoomStep);  

  208. if (tenZoomStep > 1) {  

  209.          tenDesiredZoom -= tenDesiredZoom % tenZoomStep;  

  210.        }  

  211.      } catch (NumberFormatException nfe) {  

  212. // continue

  213.      }  

  214.    }  

  215. // Set zoom. This helps encourage the user to pull back.

  216. // Some devices like the Behold have a zoom parameter

  217. if (maxZoomString != null || motZoomValuesString != null) {  

  218.      parameters.set("zoom", String.valueOf(tenDesiredZoom / 10.0));  

  219.    }  

  220. // Most devices, like the Hero, appear to expose this zoom parameter.

  221. // It takes on values like "27" which appears to mean 2.7x zoom

  222. if (takingPictureZoomMaxString != null) {  

  223.      parameters.set("taking-picture-zoom", tenDesiredZoom);  

  224.    }  

  225.  }  

  226. publicstaticint getDesiredSharpness() {  

  227. return DESIRED_SHARPNESS;  

  228.    }  

  229. /**

  230.     * compatible  1.6

  231.     * @param camera

  232.     * @param angle

  233.     */

  234. protectedvoid setDisplayOrientation(Camera camera, int angle){    

  235.        Method downPolymorphic;    

  236. try

  237.        {    

  238.            downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });    

  239. if (downPolymorphic != null)    

  240.                downPolymorphic.invoke(camera, new Object[] { angle });    

  241.        }    

  242. catch (Exception e1)    

  243.        {    

  244.        }    

  245.   }    

  246. }  


能解决这一系列的问题还要感觉龙哥的指导,在此感谢!
这时比较晚了就不贴原工程了,明天贴出来!
如果有谁知道以上问题有更好的解决方案,还望指教,相互学习,Aladdin在此感谢,哈哈!
我的微博:http://weibo.com/dingxiaowei2013


本文出自 “丁小未的专栏” 博客,请务必保留此出处http://dingxiaowei.blog.51cto.com/4561335/1406958