开发安卓过程中,经常会用到相机,有时候系统相机不能满足开发者的需要,这时候就需要自定义相机来实现。(注意:本文只是提供思路,有关键代码,但是代码不全)
自定义相机Activity需要用到Camera这个安卓原生的相机类,需要一个surfaceView来预览,如果要拍照声音还需要一个ToneGenerator类,如果需要监听屏幕旋转角度,需要实现SensorEventListener这个监听,还需要感应重力的Sensor以及SensorManager。
SurfaceHolder以及SurfaceHolder.Callback
surfaceHolder它是一个用于控制surface的接口,它提供了控制surface 的大小,格式,上面的像素,即监视其改变的。SurfaceHolder.Callback在底层的Surface状态发生变化的时候通知View。
启动相机是在surfaceHolder的回调函数surfaceCreated里面做,需要注意启动相机前要做一些配置:
配置文件
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">surfaceview<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.getHolder</span>() <span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setType</span>(SurfaceHolder<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.SURFACE</span>_TYPE_PUSH_BUFFERS)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;//设置surfaceview不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前</span> surfaceview<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.getHolder</span>()<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setFixedSize</span>(width, height)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> surfaceview<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.getHolder</span>()<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setKeepScreenOn</span>(true)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;// 屏幕常亮</span> surfaceview<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.getHolder</span>()<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.addCallback</span>(new SurfaceCallback())<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;// 为SurfaceView的句柄添加一个回数调函</span> sensorManager = (SensorManager) getSystemService(Context<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.SENSOR</span>_SERVICE)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> sensor = sensorManager<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.getDefaultSensor</span>(Sensor<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.TYPE</span>_ACCELEROMETER)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> orientation_SensorListener = new OrientationSensorListener( sensor_handler)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> sensorManager<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.registerListener</span>(orientation_SensorListener, sensor, SensorManager<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.SENSOR</span>_DELAY_UI)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
启动相机的方法
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-javadoc" style="color:#8800;box-sizing: border-box;">/** * 启动相机 */</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">startCamera</span>() { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span> == mCamera) { <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 获得Camera对象</span> mCamera = Camera.open(); <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 设置用于显示拍照摄像的SurfaceHolder对象</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (mCamera != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) { SurfaceHolder mSurfaceHolder =surfaceview.getHolder(); mCamera.setPreviewDisplay(mSurfaceHolder); mCamera.setErrorCallback(<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> CameraonError());<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//记录相机错误的回调函数</span> mCamera.startPreview(); } } } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (Exception e) { e.printStackTrace(); <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 释放手机摄像头</span> Toast.makeText(PersonPhotoActivity.<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">this</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"相机启动失败请重新启动"</span>,Toast.LENGTH_SHORT).show(); <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (mCamera != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) { mCamera.release(); mCamera = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>; } finish(); } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">finally</span> { is_takingPhoto = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//防止连拍的标志</span> } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul>
surfaceHolder的回调函数
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">final</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">class</span> <span class="hljs-title" style="color:#66066;box-sizing: border-box;">SurfaceCallback</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">implements</span> <span class="hljs-title" style="color:#66066;box-sizing: border-box;">Callback</span> {</span> <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 预览变化时调用该方法</span> <span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">surfaceChanged</span>(SurfaceHolder holder, <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">int</span> format, <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">int</span> width,<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">int</span> height) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> { Camera.Parameters parameters = mCamera.getParameters(); parameters.setPictureFormat(PixelFormat.JPEG); parameters.setPictureSize(<span class="hljs-number" style="color:#06666;box-sizing: border-box;">1920</span>, <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1080</span>);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//设置照片分辨率16:9</span> mCamera.setErrorCallback(<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> CameraonError()); mCamera.setParameters(parameters); mCamera.startPreview(); } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (Exception e) { } } <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 开始预览时调用该方法</span> <span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">surfaceCreated</span>(SurfaceHolder holder) { startCamera(); } <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 停止预览时调用该方法</span> <span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">surfaceDestroyed</span>(SurfaceHolder holder) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (mCamera != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) { mCamera.release(); <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 释放照相机</span> mCamera = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>; } } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li></ul>
点击拍照按钮,拍照时调用自动对焦(无法自动对焦时怎么做?)
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">takePicture</span>() { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!is_takingPhoto) { is_takingPhoto = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">true</span>;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//防止多次调用</span> Toast.makeText(getApplication(), <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"点击拍照按钮后请不要晃动,以免影响图片拍摄效果!"</span>,Toast.LENGTH_SHORT).show(); <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (mCamera == <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) { Reset(); startCamera(); } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> { <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 自动对焦</span> mCamera.autoFocus(<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> AutoFocusCallback() { <span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onAutoFocus</span>(<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">boolean</span> success, Camera camera) { <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 自动对焦成功后才拍摄</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (success) { img_focus.setImageResource(R.drawable.focus2); sensorManag.unregisterListener( orientation_SensorListener); mCamera.takePicture(shutterCallback, <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>,<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> MyPictureCallback()); mCamera.cancelAutoFocus();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//有人说加这行可以使无法对焦的机器去自动对焦,在小米手机上试了好用,但是不知道其他机器</span> } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> { img_focus.setImageResource(R.drawable.focus1); Toast.makeText(getApplication(), <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"对焦失败,请重拍!"</span>, Toast.LENGTH_LONG).show(); is_takingPhoto = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>; } } }); } } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li></ul>
照片捕获成功时候的处理
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">final</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">class</span> <span class="hljs-title" style="color:#66066;box-sizing: border-box;">MyPictureCallback</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">implements</span> <span class="hljs-title" style="color:#66066;box-sizing: border-box;">PictureCallback</span> {</span> <span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onPictureTaken</span>(<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">byte</span>[] data, Camera camera) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> { BitmapFactory.Options options = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> BitmapFactory.Options(); <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// options.inSampleSize = 2;</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (data != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (data.length / <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1024</span> > <span class="hljs-number" style="color:#06666;box-sizing: border-box;">350</span> && data.length / <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1024</span> < <span class="hljs-number" style="color:#06666;box-sizing: border-box;">450</span>) { options.inSampleSize = <span class="hljs-number" style="color:#06666;box-sizing: border-box;">3</span>; } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (data.length / <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1024</span> > <span class="hljs-number" style="color:#06666;box-sizing: border-box;">450</span>) { options.inSampleSize = <span class="hljs-number" style="color:#06666;box-sizing: border-box;">4</span>; } datas = data;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//字节流(保存图片时候使用)</span> } options.inPurgeable = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">true</span>;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//内存不足时可被回收</span> options.inInputShareable = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">true</span>; mBitmap = BitmapFactory.decodeByteArray(data, <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>, data.length,options);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//此bitmap作为预览时候使用,用完一定注意回收</span> BitmapDrawable bitmapDrawable = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> BitmapDrawable(getResources(), mBitmap); img_background.setBackgroundDrawable(bitmapDrawable); img_background.setVisibility(View.VISIBLE); <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//以下全是控件状态的变化</span> <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//surfaceview.setVisibility(View.GONE);</span> <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//bt_remake.setVisibility(View.VISIBLE);</span> <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//bt_photograph.setVisibility(View.VISIBLE);</span> <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//photograph_btn.setVisibility(View.GONE);</span> <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//lin_takephoto.setVisibility(View.GONE);</span> <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//btn_cancle.setVisibility(View.GONE);</span> } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (Exception e) { e.printStackTrace(); } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">finally</span> { is_takingPhoto = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>; } } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li></ul>
保存图片返回本地地址url
<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> String <span class="hljs-title" style="box-sizing: border-box;">savephoto</span>(String path, String name, <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">byte</span>[] imagedata) { String msgPath = path + name; <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">try</span> { <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//判断目录是否可用</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>; } File file = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> File(path);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//创建文件</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (!file.exists()) { file.mkdirs(); } <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// file.createNewFile();</span> BitmapFactory.Options options = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> BitmapFactory.Options(); options.inSampleSize = <span class="hljs-number" style="color:#06666;box-sizing: border-box;">4</span>; options.inPurgeable = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">true</span>; options.inInputShareable = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">true</span>; Bitmap bitmap = BitmapFactory.decodeByteArray(imagedata, <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>,imagedata.length,options); FileOutputStream fi = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> FileOutputStream(path + name); bitmap.compress(Bitmap.CompressFormat.JPEG, <span class="hljs-number" style="color:#06666;box-sizing: border-box;">90</span>, fi);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//压缩质量</span> fi.close(); fi.flush(); bitmap.recycle();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//bitmap释放</span> bitmap = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>; } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">catch</span> (Exception e) { e.printStackTrace(); msgPath = <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"error"</span>;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//保存出错</span> } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span> msgPath; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li></ul>
保存图片调用
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">bt_photograph.setOnClickListener(<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> OnClickListener() { <span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onClick</span>(View v) { String path = ImageUtils.getInstance().savephoto(filePath,fileName, datas); is_takingPhoto = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">false</span>; <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (path.equals(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"error"</span>) || path == <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) { Toast.makeText(PersonPhotoActivity.<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">this</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"图片保存失败,请重新拍照!"</span>, <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>).show(); Reset();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//保存失败重置相机</span> } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> { img_focus.setImageResource(R.drawable.focus1);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//对焦图片设置</span> img_focus.setVisibility(View.VISIBLE); Intent intent = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> Intent(); <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (degree == -<span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>) { Log.d(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"toast"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"未捕获旋转角度"</span>); } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> { intent.putExtra(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"degree"</span>, degree); } intent.putExtra(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"url"</span>, path); setResult(<span class="hljs-number" style="color:#06666;box-sizing: border-box;">10</span>, intent);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//成功</span> PersonPhotoActivity.<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">this</span>.finish(); } } });</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>
重置状态,重置bitmap ,重置图标
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">private void reset() { img_focus<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setImageResource</span>(R<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.drawable</span><span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.focus</span>1)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> img_focus<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setVisibility</span>(View<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.VISIBLE</span>)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> img_background<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setBackgroundDrawable</span>(null)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> img_background<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setVisibility</span>(View<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.GONE</span>)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> surfaceview<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setVisibility</span>(View<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.VISIBLE</span>)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;//预览关闭</span> bt_remake<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setVisibility</span>(View<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.GONE</span>)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> bt_photograph<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setVisibility</span>(View<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.GONE</span>)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> photograph_btn<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setVisibility</span>(View<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.VISIBLE</span>)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> lin_takephoto<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setVisibility</span>(View<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.VISIBLE</span>)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> btn_cancle<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setVisibility</span>(View<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.VISIBLE</span>)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> if(mBitmap != null){ mBitmap<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.recycle</span>()<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> mBitmap = null<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> } if(datas != null ){ datas = null<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> } if (null != mCamera) { mCamera<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.startPreview</span>()<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;//重置相机</span> } else { startCamera()<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>
拍照声音的实现
<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> ShutterCallback shutterCallback = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> ShutterCallback() { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onShutter</span>() { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (tone == <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 发出提示用户的声音</span> tone = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> ToneGenerator( AudioManager.AUDIOFOCUS_REQUEST_GRANTED, ToneGenerator.MIN_VOLUME); tone.startTone(ToneGenerator.TONE_PROP_BEEP); } };</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
开关闪光灯
相机闪光灯是通过parameter来配置。
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> <span class="hljs-literal" style="color:#06666;box-sizing: border-box;">void</span> turnLightOn() { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (mCamera <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">==</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">null</span>) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span>; } Parameters parameters <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> mCamera<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>getParameters(); <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (parameters <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">==</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">null</span>) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span>; } <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">List</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;"><</span><span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">String</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">></span> flashModes <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> parameters<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>getSupportedFlashModes(); <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 是否支持闪光灯</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (flashModes <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">==</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">null</span>) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">return</span>; } <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">String</span> flashMode <span class="hljs-subst" style="color:#000000;box-sizing: border-box;">=</span> parameters<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>getFlashMode(); <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (<span class="hljs-subst" style="color:#000000;box-sizing: border-box;">!</span>Parameters<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>FLASH_MODE_TORCH<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">equals</span>(flashMode)) { <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// 开启闪光灯</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (flashModes<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>contains(Parameters<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>FLASH_MODE_TORCH)) { parameters<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>setFlashMode(Parameters<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>FLASH_MODE_TORCH); mCamera<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>setParameters(parameters);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//为相机开启闪光灯</span> } } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul>
关闭闪光灯,跟开启闪光灯判断一样,只是配置不同,关键代码
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">//支持关闭闪光灯模式 if (!Parameters<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.FLASH</span>_MODE_OFF<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.equals</span>(flashMode)) { if (flashModes<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.contains</span>(Parameters<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.FLASH</span>_MODE_OFF)) { parameters<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setFlashMode</span>(Parameters<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.FLASH</span>_MODE_OFF)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> mCamera<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.setParameters</span>(parameters)<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">;</span> } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
捕获屏幕旋转角度
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">private</span> Handler sensor_handler= <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> Handler() { <span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">handleMessage</span>(android.os.Message msg) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (msg.what == <span class="hljs-number" style="color:#06666;box-sizing: border-box;">888</span>) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">int</span> orientation = msg.arg1; <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (orientation > <span class="hljs-number" style="color:#06666;box-sizing: border-box;">45</span> && orientation < <span class="hljs-number" style="color:#06666;box-sizing: border-box;">135</span>) { degree = <span class="hljs-number" style="color:#06666;box-sizing: border-box;">180</span>; Log.e(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"180"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"右横"</span>); } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (orientation > <span class="hljs-number" style="color:#06666;box-sizing: border-box;">135</span> && orientation < <span class="hljs-number" style="color:#06666;box-sizing: border-box;">225</span>) { degree = <span class="hljs-number" style="color:#06666;box-sizing: border-box;">270</span>; Log.e(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"270"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"倒屏"</span>); } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (orientation > <span class="hljs-number" style="color:#06666;box-sizing: border-box;">225</span> && orientation < <span class="hljs-number" style="color:#06666;box-sizing: border-box;">315</span>) { degree = <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>; Log.e(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"0"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"左横"</span>); } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> ((orientation > <span class="hljs-number" style="color:#06666;box-sizing: border-box;">315</span> && orientation < <span class="hljs-number" style="color:#06666;box-sizing: border-box;">360</span>) || (orientation > <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span> && orientation < <span class="hljs-number" style="color:#06666;box-sizing: border-box;">45</span>)) { degree = <span class="hljs-number" style="color:#06666;box-sizing: border-box;">90</span>; Log.e(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"90"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"竖屏"</span>); } } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">super</span>.handleMessage(msg); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul>
重力感应改变的监听(谷歌提供)
<code class="hljs cs has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> synchronized <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onSensorChanged</span>(SensorEvent <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">event</span>) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">float</span>[] values = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">event</span>.values; <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">int</span> orientation = ORIENTATION_UNKNOWN; <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">float</span> X = -values[_DATA_X]; <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">float</span> Y = -values[_DATA_Y]; <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">float</span> Z = -values[_DATA_Z]; <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">float</span> magnitude = X*X + Y*Y; <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// Don't trust the angle if the magnitude is small compared to the y value</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (magnitude * <span class="hljs-number" style="color:#06666;box-sizing: border-box;">4</span> >= Z*Z) { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">float</span> OneEightyOverPi = <span class="hljs-number" style="color:#06666;box-sizing: border-box;">57.29577957855</span>f; <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">float</span> angle = (<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">float</span>)Math.atan2(-Y, X) * OneEightyOverPi; orientation = <span class="hljs-number" style="color:#06666;box-sizing: border-box;">90</span> - (<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">int</span>)Math.round(angle); <span class="hljs-comment" style="color:#8800;box-sizing: border-box;">// normalize to 0 - 359 range</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">while</span> (orientation >= <span class="hljs-number" style="color:#06666;box-sizing: border-box;">360</span>) { orientation -= <span class="hljs-number" style="color:#06666;box-sizing: border-box;">360</span>; } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">while</span> (orientation < <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>) { orientation += <span class="hljs-number" style="color:#06666;box-sizing: border-box;">360</span>; } } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (rotateHandler!=<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) { rotateHandler.obtainMessage(<span class="hljs-number" style="color:#06666;box-sizing: border-box;">888</span>, orientation, <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>).sendToTarget();<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//发送消息通知改变</span> } }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>
类的销毁
<code class="hljs java has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-annotation" style="color:#9b859d;box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onDestroy</span>() { <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span> (mBitmap != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>) { mBitmap.recycle(); mBitmap = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>; } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">if</span>(datas != <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>){ datas = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">null</span>; } <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">super</span>.onDestroy(); }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
1.为什么保存要压缩图片?
因为每个型号手机相机配置不一样,相机非常好的手机拍摄一张图片有可能是6-12m,如果不做处理直接放到bitmap中,展示的时候就会内存溢出,可以根据自己需要,改变压缩保存图片的方法。
2.为什么要释放bitmap?
相机启动本身就是一件耗费资源的事情,如果bitmap一直存在activity中不释放,当activity销毁的时候内存回收机制不会立即执行,可能导致内存溢出。
3.为何要加入重力感应?
相机是有角度的,不监听角度改变,拍摄的照片就会按照相机拍摄角度显示,有感应之后放置照片按角度放置就可以。