免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2383 | 回复: 0
打印 上一主题 下一主题

Android 调用系统Camera 程序照相,获取照片 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-30 17:33 |只看该作者 |倒序浏览
Android 调用系统Camera 程序照相,获取照片








下面是我自己整理的源码,网络上好多不能够运行,或者有bug。
我在emulator android 2.1运行良好,源码注释一定程度能够自我解释
强烈推荐配合adb locat Take2:d *:s查看程序运行函数调用情况

对Activity生命周期不是很理解的,请先看我之前的一片文章
http://menuz.iteye.com/blog/1255320



Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>  
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.    android:orientation="vertical" android:layout_width="fill_parent"  
  4. 4.    android:layout_height="fill_parent">  
  5. 5.    <Button android:layout_width="fill_parent"  
  6. 6.        android:layout_height="wrap_content" android:id="@+id/cameraButton"  
  7. 7.        android:text="拍照" />  
  8. 8.  
  9. 9.    <SurfaceView android:id="@+id/surface_camera"  
  10. 10.        android:layout_width="fill_parent" android:layout_height="10dip"  
  11. 11.        android:layout_weight="1">  
  12. 12.    </SurfaceView>  
  13. 13.</LinearLayout>  
  14. <?xml version="1.0" encoding="utf-8"?>
  15. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  16.         android:orientation="vertical" android:layout_width="fill_parent"
  17.         android:layout_height="fill_parent">
  18.         <Button android:layout_width="fill_parent"
  19.                 android:layout_height="wrap_content" android:id="@+id/cameraButton"
  20.                 android:text="拍照" />

  21.         <SurfaceView android:id="@+id/surface_camera"
  22.                 android:layout_width="fill_parent" android:layout_height="10dip"
  23.                 android:layout_weight="1">
  24.         </SurfaceView>
  25. </LinearLayout>
复制代码
Xml代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>  
  2. 2.<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.      package="com.busclient"  
  4. 4.      android:versionCode="1"  
  5. 5.      android:versionName="1.0">  
  6. 6.    <uses-sdk android:minSdkVersion="7" />  
  7. 7.      
  8. 8.    <uses-permission android:name="android.permission.CAMERA" /> <!-- 注意必须在application上面-->  
  9. 9.    <application android:icon="@drawable/icon" android:label="@string/app_name">  
  10. 10.    <uses-library android:name="com.google.android.maps" />  
  11. 11.        <activity android:name=".Take2"  
  12. 12.                  android:label="@string/app_name">  
  13. 13.            <intent-filter>  
  14. 14.                <action android:name="android.intent.action.MAIN" />  
  15. 15.                <category android:name="android.intent.category.LAUNCHER" />  
  16. 16.            </intent-filter>  
  17. 17.        </activity>  
  18. 18.    </application>  
  19. 19.    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/>  
  20. 20.    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  21. 21.</manifest>  
  22. <?xml version="1.0" encoding="utf-8"?>
  23. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  24.       package="com.busclient"
  25.       android:versionCode="1"
  26.       android:versionName="1.0">
  27.     <uses-sdk android:minSdkVersion="7" />
  28.    
  29.     <uses-permission android:name="android.permission.CAMERA" /> <!-- 注意必须在application上面-->
  30.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  31.     <uses-library android:name="com.google.android.maps" />
  32.         <activity android:name=".Take2"
  33.                   android:label="@string/app_name">
  34.             <intent-filter>
  35.                 <action android:name="android.intent.action.MAIN" />
  36.                 <category android:name="android.intent.category.LAUNCHER" />
  37.             </intent-filter>
  38.         </activity>
  39.     </application>
  40.     <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/>
  41.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  42. </manifest>
复制代码
Java代码
  1. 1.package com.busclient;   
  2. 2.  
  3. 3.import java.io.File;   
  4. 4.import java.io.FileOutputStream;   
  5. 5.import java.io.IOException;   
  6. 6.  
  7. 7.import android.app.Activity;   
  8. 8.import android.hardware.Camera;   
  9. 9.import android.hardware.Camera.PictureCallback;   
  10. 10.import android.os.Bundle;   
  11. 11.import android.util.Log;   
  12. 12.import android.view.SurfaceHolder;   
  13. 13.import android.view.SurfaceView;   
  14. 14.import android.view.View;   
  15. 15.import android.view.View.OnClickListener;   
  16. 16.import android.widget.Button;   
  17. 17.  
  18. 18.public class Take2 extends Activity implements SurfaceHolder.Callback {   
  19. 19.    private final static String TAG = "Take2";;   
  20. 20.  
  21. 21.    private SurfaceView surfaceView;   
  22. 22.  
  23. 23.    private SurfaceHolder surfaceHolder;   
  24. 24.  
  25. 25.    private Camera mCam;   
  26. 26.  
  27. 27.    private boolean hasStartPreview = false;   
  28. 28.  
  29. 29.    private Button btnTakePicture;   
  30. 30.  
  31. 31.    // Camera API:   
  32. 32.    // Call release() to release the camera for use by other applications.   
  33. 33.    // Applications should release the camera immediately in onPause() (and   
  34. 34.    // re-open() it in onResume()).   
  35. 35.  
  36. 36.    @Override  
  37. 37.    protected void onCreate(Bundle savedInstanceState) {   
  38. 38.        super.onCreate(savedInstanceState);   
  39. 39.        setContentView(R.layout.takephoto);   
  40. 40.        Log.d(TAG, "onCreate");   
  41. 41.  
  42. 42.        // 取得surfaceView的引用,surface在surfaceView中展现   
  43. 43.        // 当surfaceView可见时,surface会被创建,实现surfaceCreated进行特定操作   
  44. 44.        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);   
  45. 45.        btnTakePicture = (Button) findViewById(R.id.cameraButton);   
  46. 46.        // 注册按钮实现拍照   
  47. 47.        btnTakePicture.setOnClickListener(new OnClickListener() {   
  48. 48.  
  49. 49.            @Override  
  50. 50.            public void onClick(View v) {   
  51. 51.                if (mCam != null) {   
  52. 52.                    // 调用mCam进行拍照   
  53. 53.                    // param1 Camera.ShutterCallback   
  54. 54.                    // param2 Camera.PictureCallback raw 当原始图片数据获得时,会执行PictureCallback   
  55. 55.                    // param3 Camera.PictureCallback compressed 当压缩数据获得时,会执行PictureCallback   
  56. 56.                    mCam.takePicture(null, null, pictureCallBack);   
  57. 57.                }   
  58. 58.            }   
  59. 59.  
  60. 60.        });   
  61. 61.  
  62. 62.        // 要操作surface,只有通过surfaceHolder   
  63. 63.        surfaceHolder = surfaceView.getHolder();   
  64. 64.        surfaceHolder.addCallback(this);   
  65. 65.        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);   
  66. 66.    }   
  67. 67.  
  68. 68.    private PictureCallback pictureCallBack = new Camera.PictureCallback() {   
  69. 69.  
  70. 70.        @Override  
  71. 71.        public void onPictureTaken(byte[] data, Camera camera) {   
  72. 72.            if (mCam != null) {   
  73. 73.                if (data != null) {   
  74. 74.                    File f = new File("/data/data/com.busclient/picture1.jpg");   
  75. 75.                    FileOutputStream fout = null;   
  76. 76.                    try {   
  77. 77.                        if (!f.exists())   
  78. 78.                            f.createNewFile();   
  79. 79.                        fout = new FileOutputStream(f);   
  80. 80.                        fout.write(data);   
  81. 81.                        fout.close();   
  82. 82.                        Log.d(TAG, "write success");   
  83. 83.                           
  84. 84.                        Thread.sleep(1500);   
  85. 85.                    } catch (IOException e) {   
  86. 86.                        e.printStackTrace();   
  87. 87.                    } catch (InterruptedException e) {   
  88. 88.                        e.printStackTrace();   
  89. 89.                    } finally {   
  90. 90.                        try {   
  91. 91.                            if (fout != null) {   
  92. 92.                                fout.close();   
  93. 93.                                fout = null;   
  94. 94.                            }   
  95. 95.                        } catch (Exception e) {   
  96. 96.                            e.printStackTrace();   
  97. 97.                        }   
  98. 98.                    }   
  99. 99.                }   
  100. 100.                finish();   
  101. 101.            }   
  102. 102.        }   
  103. 103.    };   
  104. 104.  
  105. 105.    // surface只能够由一个线程操作,一旦被操作,其他线程就无法操作surface   
  106. 106.    @Override  
  107. 107.    public void surfaceCreated(SurfaceHolder holder) {   
  108. 108.        Log.d(TAG, "surfaceCreated");   
  109. 109.        try {   
  110. 110.            // 必须设置一个初始化的surfaceHolder,若没有surface(由holder操作surface),   
  111. 111.            // 则camera无法启动预览 (就是一般打开照相机屏幕能够动态显示场景??描述的不够好)   
  112. 112.            mCam.setPreviewDisplay(surfaceHolder);   
  113. 113.        } catch (IOException e) {   
  114. 114.            Log.d(TAG, "error");   
  115. 115.            mCam.release();   
  116. 116.            mCam = null;   
  117. 117.        }   
  118. 118.    }   
  119. 119.  
  120. 120.    // 在surfaceCreated后调用,当surface发生变化也会触发该方法,这个方法   
  121. 121.    // 一般至少被调用一次   
  122. 122.    @Override  
  123. 123.    public void surfaceChanged(SurfaceHolder holder, int format, int width,   
  124. 124.            int height) {   
  125. 125.        Log.d(TAG, "surfaceChanged");   
  126. 126.        // 调用startPreview使预览surface可以更新,拍照   
  127. 127.        // 必须启动预览,而startPreview必须在setPreviewDisplay(surfaceHolder)之后   
  128. 128.        if (mCam != null && hasStartPreview == false) {   
  129. 129.  
  130. 130.            mCam.startPreview();   
  131. 131.            hasStartPreview = true;   
  132. 132.        }   
  133. 133.    }   
  134. 134.  
  135. 135.    // 析构surface   
  136. 136.    @Override  
  137. 137.    public void surfaceDestroyed(SurfaceHolder holder) {   
  138. 138.        Log.d(TAG, "surfaceDestroyed");   
  139. 139.        // 活都被onPause抢去了   
  140. 140.    }   
  141. 141.  
  142. 142.    @Override  
  143. 143.    protected void onDestroy() {   
  144. 144.        Log.d(TAG, "onDestroy");   
  145. 145.        super.onDestroy();   
  146. 146.    }   
  147. 147.  
  148. 148.    // onPause比surfaceDestroyed() 先调用   
  149. 149.    @Override  
  150. 150.    protected void onPause() {   
  151. 151.        Log.d(TAG, "onPause");   
  152. 152.        if (hasStartPreview) {   
  153. 153.            mCam.stopPreview();   
  154. 154.        }   
  155. 155.        mCam.release();   
  156. 156.        mCam = null;   
  157. 157.        hasStartPreview = false;   
  158. 158.        super.onPause();   
  159. 159.    }   
  160. 160.  
  161. 161.    @Override  
  162. 162.    protected void onRestart() {   
  163. 163.        Log.d(TAG, "onRestart");   
  164. 164.        super.onRestart();   
  165. 165.    }   
  166. 166.  
  167. 167.    @Override  
  168. 168.    protected void onResume() {   
  169. 169.        Log.d(TAG, "onResume");   
  170. 170.        if (mCam == null)   
  171. 171.            mCam = Camera.open();   
  172. 172.        super.onResume();   
  173. 173.    }   
  174. 174.  
  175. 175.    @Override  
  176. 176.    protected void onStart() {   
  177. 177.        Log.d(TAG, "onStart");   
  178. 178.        super.onStart();   
  179. 179.    }   
  180. 180.  
  181. 181.    @Override  
  182. 182.    protected void onStop() {   
  183. 183.        Log.d(TAG, "onStop");   
  184. 184.        super.onStop();   
  185. 185.    }   
  186. 186.  
  187. 187.}  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP