第58章、拍照功能实现(从零开始学Android)

来自:http://blog.****.net/jianghuiquan/article/details/8569187

Android有两种拍照方法,一种是直接调用系统的照相Intent,使用 onActivityResult获取图片资源或者指定图片路径,拍照返回成功后去指定路径读取图片;一种是用SurfaceView自定义界面,添加业务个性化功能。

一、第一种方法

1、设计界面

  (1)、布局文件

  打开activity_main.xml文件。

  输入以下代码:

?

[html]?view plain?copy
  1. <?xml?version="1.0"?encoding="utf-8"??>??
  2. ??
  3. <LinearLayout???
  4. ????xmlns:android="http://schemas.android.com/apk/res/android"???
  5. ????android:orientation="vertical"???
  6. ????android:layout_width="fill_parent"???
  7. ????android:layout_height="fill_parent">??
  8. ??
  9. ????<Button??
  10. ????????android:id="@+id/bysystem"??
  11. ????????android:layout_width="wrap_content"??
  12. ????????android:layout_height="wrap_content"??
  13. ????????android:text="调用系统相机不返回结果"?/>??
  14. ??
  15. ????<Button??
  16. ????????android:id="@+id/byself"??
  17. ????????android:layout_width="wrap_content"??
  18. ????????android:layout_height="wrap_content"??
  19. ????????android:text="调用系统相机并返回结果"?/>??
  20. ??
  21. ????<ImageView??
  22. ????????android:id="@+id/photo"??
  23. ????????android:layout_width="wrap_content"??
  24. ????????android:layout_height="wrap_content"?/>??
  25. ??
  26. </LinearLayout>??


2、程序文件

  打开“src/com.genwoxue.camera/MainActivity.java”文件。

  然后输入以下代码:

[java]?view plain?copy
  1. package?com.genwoxue.camera;??
  2. ??
  3. ??
  4. import?java.io.File;??
  5. import?android.app.Activity;??
  6. import?android.content.Intent;??
  7. import?android.net.Uri;??
  8. import?android.os.Bundle;??
  9. import?android.os.Environment;??
  10. import?android.provider.MediaStore;??
  11. import?android.view.View;??
  12. import?android.view.View.OnClickListener;??
  13. import?android.widget.Button;??
  14. import?android.widget.Toast;??
  15. ??
  16. public?class?MainActivity?extends?Activity?{??
  17. ??????
  18. ????private?Button?btnSystem=null;??
  19. ????private?Button?btnSelf=null;??
  20. ????private?File?file=null;???
  21. ????private?static?final?String?FILENAME="photo.jpg";??
  22. ??????
  23. ????private?static?String?path="";??
  24. ??
  25. ????@Override??
  26. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  27. ????????super.onCreate(savedInstanceState);??
  28. ????????setContentView(R.layout.activity_main);??
  29. ??????????
  30. ????????btnSystem=(Button)super.findViewById(R.id.bysystem);??
  31. ????????btnSelf=(Button)super.findViewById(R.id.byself);??
  32. ??????????
  33. ????????//调用系统照相机,不返回结果??
  34. ????????btnSystem.setOnClickListener(new?OnClickListener(){??
  35. ????????????public?void?onClick(View?v)??
  36. ????????????{???
  37. ????????????????Intent?intent?=?new?Intent();????
  38. ????????????????intent.setAction("android.media.action.STILL_IMAGE_CAMERA");???
  39. ????????????????startActivity(intent);???
  40. ????????????}??
  41. ????????});??
  42. ??????????
  43. ????????//调用系统照相机,返回结果??
  44. ????????btnSelf.setOnClickListener(new?OnClickListener(){??
  45. ????????????public?void?onClick(View?v)??
  46. ????????????{????
  47. ????????????????//判断外部存储卡是否存在??
  48. ????????????????if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){??
  49. ????????????????????Toast.makeText(getApplicationContext(),?"读取失败,SD存储卡不存在!",?Toast.LENGTH_LONG).show();????
  50. ????????????????????return;??
  51. ????????????????}??
  52. ??????????????????
  53. ????????????????//判断文件是否存在??
  54. ????????????????path=Environment.getExternalStorageDirectory().toString()+File.separator+"genwoxue"+File.separator+FILENAME;??
  55. ????????????????file=new?File(path);??
  56. ????????????????if(!file.exists()){??
  57. ????????????????????File?vDirPath?=?file.getParentFile();???
  58. ????????????????????vDirPath.mkdirs();???
  59. ????????????????????Toast.makeText(getApplicationContext(),?"photo.jpg文件不存在!",?Toast.LENGTH_LONG).show();????
  60. ????????????????????return;??
  61. ????????????????}??
  62. ??????????????????
  63. ????????????????Uri?uri?=?Uri.fromFile(file);???
  64. ????????????????Intent?intent?=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE);???
  65. ????????????????intent.putExtra(MediaStore.EXTRA_OUTPUT,?uri);??
  66. ????????????????startActivityForResult(intent,?1);???
  67. ??????????????????
  68. ????????????}??
  69. ????????});??
  70. ??????????
  71. ????}??
  72. ??????
  73. }??


?3、运行结果

  第58章、拍照功能实现(从零开始学Android) 

  第58章、拍照功能实现(从零开始学Android)

?

二、第二种方法。

1、设计界面

  (1)、布局文件

  打开activity_main.xml文件。

  输入以下代码:

[html]?view plain?copy
  1. <?xml?version="1.0"?encoding="utf-8"??>??
  2. ??
  3. <LinearLayout???
  4. ????xmlns:android="http://schemas.android.com/apk/res/android"???
  5. ????android:orientation="vertical"???
  6. ????android:layout_width="fill_parent"???
  7. ????android:layout_height="fill_parent">??
  8. ??
  9. ??
  10. ????<Button??
  11. ????????android:id="@+id/byself"??
  12. ????????android:layout_width="wrap_content"??
  13. ????????android:layout_height="wrap_content"??
  14. ????????android:text="拍照(自定义相机)"?/>??
  15. ??????
  16. ????<SurfaceView??
  17. ????????android:id="@+id/photo"??
  18. ????????android:layout_width="300dip"??
  19. ????????android:layout_height="400dip"?/>??
  20. ??
  21. </LinearLayout>??


2、程序文件

  打开“src/com.genwoxue.cameradiy/MainActivity.java”文件。

  然后输入以下代码:

[java]?view plain?copy
  1. package?com.genwoxue.cameradiy;??
  2. ??
  3. ??
  4. import?java.io.BufferedOutputStream;??
  5. import?java.io.File;??
  6. import?java.io.FileNotFoundException;??
  7. import?java.io.FileOutputStream;??
  8. import?java.io.IOException;??
  9. import?android.app.Activity;??
  10. import?android.graphics.Bitmap;??
  11. import?android.graphics.BitmapFactory;??
  12. import?android.graphics.PixelFormat;??
  13. import?android.hardware.Camera;??
  14. import?android.hardware.Camera.AutoFocusCallback;??
  15. import?android.hardware.Camera.Parameters;??
  16. import?android.hardware.Camera.PictureCallback;??
  17. import?android.hardware.Camera.ShutterCallback;??
  18. import?android.os.Bundle;??
  19. import?android.os.Environment;??
  20. import?android.util.Log;??
  21. import?android.view.SurfaceHolder;??
  22. import?android.view.SurfaceView;??
  23. import?android.view.View;??
  24. import?android.view.View.OnClickListener;??
  25. import?android.widget.Button;??
  26. import?android.widget.Toast;??
  27. ??
  28. public?class?MainActivity?extends?Activity?{??
  29. ??????
  30. ????private?Button?btnSelf=null;??
  31. ????private?Camera?camera=null;??
  32. ????private?static?final?String?TAG="PhotoDIY";??
  33. ????private?String?path="";??
  34. ????private?boolean?previewRuning=true;??
  35. ??
  36. ????@Override??
  37. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  38. ????????super.onCreate(savedInstanceState);??
  39. ????????setContentView(R.layout.activity_main);??
  40. ??????????
  41. ????????//初始化SurfaceView??
  42. ????????SurfaceView?mpreview?=?(SurfaceView)?this.findViewById(R.id.photo);???
  43. ????????SurfaceHolder?mSurfaceHolder?=?mpreview.getHolder();???
  44. ????????mSurfaceHolder.addCallback(new?SurfaceViewCallback());???
  45. ????????mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);???
  46. ??
  47. ??????????
  48. ????????btnSelf=(Button)super.findViewById(R.id.byself);??
  49. ??????????
  50. ????????//拍照(自定义相机)??
  51. ????????btnSelf.setOnClickListener(new?OnClickListener(){??
  52. ????????????public?void?onClick(View?v)??
  53. ????????????{????
  54. ????????????????if(camera!=null){??
  55. ????????????????????camera.autoFocus(new?AutoFocusCallbackimpl());??
  56. ????????????????}??
  57. ????????????}??
  58. ????????});??
  59. ??????????
  60. ????}??
  61. ??????
  62. ????public?class?SurfaceViewCallback?implements?SurfaceHolder.Callback{??
  63. ??????????
  64. ????????@Override??
  65. ????????public?void?surfaceChanged(SurfaceHolder?holder,int?format,int?width,int?heith){??
  66. ??
  67. ????????}??
  68. ??????????
  69. ????????@Override??
  70. ????????public?void?surfaceCreated(SurfaceHolder?holder){??
  71. ????????????//现在智能机可能会有多个镜头:一般前置为1;后置为0??
  72. ????????????MainActivity.this.camera=Camera.open(0);??
  73. ????????????//设置参数??
  74. ????????????Parameters?param=camera.getParameters();??
  75. ????????????param.setPictureFormat(PixelFormat.JPEG);??
  76. ????????????param.set("jpeg-quality",85);??
  77. ????????????param.setPreviewFrameRate(5);??
  78. ????????????camera.setParameters(param);??
  79. ??????????????
  80. ????????????try?{??
  81. ????????????????camera.setPreviewDisplay(holder);???//成像在SurfaceView??
  82. ????????????}?catch?(IOException?e)?{??
  83. ????????????????e.printStackTrace();??
  84. ????????????}??
  85. ??????????????
  86. ????????????//开始预览??
  87. ????????????camera.startPreview();??
  88. ????????????previewRuning=true;??
  89. ????????}??
  90. ??????????
  91. ????????@Override??
  92. ????????public?void?surfaceDestroyed(SurfaceHolder?holder){??
  93. ????????????if(camera!=null){??
  94. ????????????????if(previewRuning){??
  95. ????????????????????camera.stopPreview();??
  96. ????????????????????previewRuning=false;??
  97. ????????????????}??
  98. ????????????????camera.release();??
  99. ????????????}??
  100. ????????}??
  101. ????}??
  102. ??????
  103. ????//调用takePicture()方法时,自动执行pictureCallback回调方法??
  104. ????public?PictureCallback?picture=new?PictureCallback(){??
  105. ????????@Override??
  106. ????????public?void?onPictureTaken(byte[]?data,Camera?camera){????????
  107. ????????????Bitmap?bmp=BitmapFactory.decodeByteArray(data,?0,?data.length);??
  108. ????????????//判断外部存储卡是否存在??
  109. ????????????if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){??
  110. ????????????????Toast.makeText(getApplicationContext(),?"读取失败,SD存储卡不存在!",?Toast.LENGTH_LONG).show();????
  111. ????????????????return;??
  112. ????????????}??
  113. ??????????????
  114. ????????????//判断文件是否存在??
  115. ????????????path=Environment.getExternalStorageDirectory().toString()??
  116. ????????????????????+File.separator??
  117. ????????????????????+"genwoxue"??
  118. ????????????????????+File.separator??
  119. ????????????????????+System.currentTimeMillis()??
  120. ????????????????????+".jpg";??
  121. ??????????????
  122. ????????????File?file=new?File(path);??
  123. ????????????if(!file.exists()){??
  124. ????????????????File?vDirPath?=?file.getParentFile();???
  125. ????????????????vDirPath.mkdirs();???
  126. ????????????????Toast.makeText(getApplicationContext(),?"photo.jpg文件不存在!",?Toast.LENGTH_LONG).show();????
  127. ????????????????return;??
  128. ????????????}??
  129. ??????????????
  130. ????????????try?{??
  131. ????????????????BufferedOutputStream?bos=new?BufferedOutputStream(new?FileOutputStream(file));??
  132. ????????????????bmp.compress(Bitmap.CompressFormat.JPEG,?80,?bos);??
  133. ????????????????try?{??
  134. ????????????????????bos.flush();??
  135. ????????????????????bos.close();??
  136. ????????????????}?catch?(IOException?e)?{??
  137. ????????????????????e.printStackTrace();??
  138. ????????????????}??
  139. ??????????????????
  140. ????????????}?catch?(FileNotFoundException?e)?{??
  141. ????????????????e.printStackTrace();??
  142. ????????????}??
  143. ??????????????
  144. ????????????camera.stopPreview();??
  145. ????????????camera.startPreview();??
  146. ??????????????
  147. ????????}??
  148. ????};??
  149. ??
  150. ????//对焦回回调??
  151. ????public?class?AutoFocusCallbackimpl?implements?AutoFocusCallback{??
  152. ????????public?void?onAutoFocus(boolean?success,Camera?camera){??
  153. ??????????????
  154. ????????????if(success){??
  155. ????????????????camera.takePicture(shutter,?null,?picture);??
  156. ????????????????camera.stopPreview();??
  157. ????????????}??
  158. ????????}??
  159. ????}??
  160. ??????
  161. ????//快门回调??
  162. ????public?ShutterCallback?shutter=new?ShutterCallback(){??
  163. ????????public?void?onShutter(){??
  164. ??????????????
  165. ????????}??
  166. ????};??
  167. }??


3、运行结果

  第58章、拍照功能实现(从零开始学Android)


来自:http://blog.****.net/jianghuiquan/article/details/8569187

Android有两种拍照方法,一种是直接调用系统的照相Intent,使用 onActivityResult获取图片资源或者指定图片路径,拍照返回成功后去指定路径读取图片;一种是用SurfaceView自定义界面,添加业务个性化功能。

一、第一种方法

1、设计界面

  (1)、布局文件

  打开activity_main.xml文件。

  输入以下代码:

?

[html]?view plain?copy
  1. <?xml?version="1.0"?encoding="utf-8"??>??
  2. ??
  3. <LinearLayout???
  4. ????xmlns:android="http://schemas.android.com/apk/res/android"???
  5. ????android:orientation="vertical"???
  6. ????android:layout_width="fill_parent"???
  7. ????android:layout_height="fill_parent">??
  8. ??
  9. ????<Button??
  10. ????????android:id="@+id/bysystem"??
  11. ????????android:layout_width="wrap_content"??
  12. ????????android:layout_height="wrap_content"??
  13. ????????android:text="调用系统相机不返回结果"?/>??
  14. ??
  15. ????<Button??
  16. ????????android:id="@+id/byself"??
  17. ????????android:layout_width="wrap_content"??
  18. ????????android:layout_height="wrap_content"??
  19. ????????android:text="调用系统相机并返回结果"?/>??
  20. ??
  21. ????<ImageView??
  22. ????????android:id="@+id/photo"??
  23. ????????android:layout_width="wrap_content"??
  24. ????????android:layout_height="wrap_content"?/>??
  25. ??
  26. </LinearLayout>??


2、程序文件

  打开“src/com.genwoxue.camera/MainActivity.java”文件。

  然后输入以下代码:

[java]?view plain?copy
  1. package?com.genwoxue.camera;??
  2. ??
  3. ??
  4. import?java.io.File;??
  5. import?android.app.Activity;??
  6. import?android.content.Intent;??
  7. import?android.net.Uri;??
  8. import?android.os.Bundle;??
  9. import?android.os.Environment;??
  10. import?android.provider.MediaStore;??
  11. import?android.view.View;??
  12. import?android.view.View.OnClickListener;??
  13. import?android.widget.Button;??
  14. import?android.widget.Toast;??
  15. ??
  16. public?class?MainActivity?extends?Activity?{??
  17. ??????
  18. ????private?Button?btnSystem=null;??
  19. ????private?Button?btnSelf=null;??
  20. ????private?File?file=null;???
  21. ????private?static?final?String?FILENAME="photo.jpg";??
  22. ??????
  23. ????private?static?String?path="";??
  24. ??
  25. ????@Override??
  26. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  27. ????????super.onCreate(savedInstanceState);??
  28. ????????setContentView(R.layout.activity_main);??
  29. ??????????
  30. ????????btnSystem=(Button)super.findViewById(R.id.bysystem);??
  31. ????????btnSelf=(Button)super.findViewById(R.id.byself);??
  32. ??????????
  33. ????????//调用系统照相机,不返回结果??
  34. ????????btnSystem.setOnClickListener(new?OnClickListener(){??
  35. ????????????public?void?onClick(View?v)??
  36. ????????????{???
  37. ????????????????Intent?intent?=?new?Intent();????
  38. ????????????????intent.setAction("android.media.action.STILL_IMAGE_CAMERA");???
  39. ????????????????startActivity(intent);???
  40. ????????????}??
  41. ????????});??
  42. ??????????
  43. ????????//调用系统照相机,返回结果??
  44. ????????btnSelf.setOnClickListener(new?OnClickListener(){??
  45. ????????????public?void?onClick(View?v)??
  46. ????????????{????
  47. ????????????????//判断外部存储卡是否存在??
  48. ????????????????if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){??
  49. ????????????????????Toast.makeText(getApplicationContext(),?"读取失败,SD存储卡不存在!",?Toast.LENGTH_LONG).show();????
  50. ????????????????????return;??
  51. ????????????????}??
  52. ??????????????????
  53. ????????????????//判断文件是否存在??
  54. ????????????????path=Environment.getExternalStorageDirectory().toString()+File.separator+"genwoxue"+File.separator+FILENAME;??
  55. ????????????????file=new?File(path);??
  56. ????????????????if(!file.exists()){??
  57. ????????????????????File?vDirPath?=?file.getParentFile();???
  58. ????????????????????vDirPath.mkdirs();???
  59. ????????????????????Toast.makeText(getApplicationContext(),?"photo.jpg文件不存在!",?Toast.LENGTH_LONG).show();????
  60. ????????????????????return;??
  61. ????????????????}??
  62. ??????????????????
  63. ????????????????Uri?uri?=?Uri.fromFile(file);???
  64. ????????????????Intent?intent?=?new?Intent(MediaStore.ACTION_IMAGE_CAPTURE);???
  65. ????????????????intent.putExtra(MediaStore.EXTRA_OUTPUT,?uri);??
  66. ????????????????startActivityForResult(intent,?1);???
  67. ??????????????????
  68. ????????????}??
  69. ????????});??
  70. ??????????
  71. ????}??
  72. ??????
  73. }??


?3、运行结果

  第58章、拍照功能实现(从零开始学Android) 

  第58章、拍照功能实现(从零开始学Android)

?

二、第二种方法。

1、设计界面

  (1)、布局文件

  打开activity_main.xml文件。

  输入以下代码:

[html]?view plain?copy
  1. <?xml?version="1.0"?encoding="utf-8"??>??
  2. ??
  3. <LinearLayout???
  4. ????xmlns:android="http://schemas.android.com/apk/res/android"???
  5. ????android:orientation="vertical"???
  6. ????android:layout_width="fill_parent"???
  7. ????android:layout_height="fill_parent">??
  8. ??
  9. ??
  10. ????<Button??
  11. ????????android:id="@+id/byself"??
  12. ????????android:layout_width="wrap_content"??
  13. ????????android:layout_height="wrap_content"??
  14. ????????android:text="拍照(自定义相机)"?/>??
  15. ??????
  16. ????<SurfaceView??
  17. ????????android:id="@+id/photo"??
  18. ????????android:layout_width="300dip"??
  19. ????????android:layout_height="400dip"?/>??
  20. ??
  21. </LinearLayout>??


2、程序文件

  打开“src/com.genwoxue.cameradiy/MainActivity.java”文件。

  然后输入以下代码:

[java]?view plain?copy
  1. package?com.genwoxue.cameradiy;??
  2. ??
  3. ??
  4. import?java.io.BufferedOutputStream;??
  5. import?java.io.File;??
  6. import?java.io.FileNotFoundException;??
  7. import?java.io.FileOutputStream;??
  8. import?java.io.IOException;??
  9. import?android.app.Activity;??
  10. import?android.graphics.Bitmap;??
  11. import?android.graphics.BitmapFactory;??
  12. import?android.graphics.PixelFormat;??
  13. import?android.hardware.Camera;??
  14. import?android.hardware.Camera.AutoFocusCallback;??
  15. import?android.hardware.Camera.Parameters;??
  16. import?android.hardware.Camera.PictureCallback;??
  17. import?android.hardware.Camera.ShutterCallback;??
  18. import?android.os.Bundle;??
  19. import?android.os.Environment;??
  20. import?android.util.Log;??
  21. import?android.view.SurfaceHolder;??
  22. import?android.view.SurfaceView;??
  23. import?android.view.View;??
  24. import?android.view.View.OnClickListener;??
  25. import?android.widget.Button;??
  26. import?android.widget.Toast;??
  27. ??
  28. public?class?MainActivity?extends?Activity?{??
  29. ??????
  30. ????private?Button?btnSelf=null;??
  31. ????private?Camera?camera=null;??
  32. ????private?static?final?String?TAG="PhotoDIY";??
  33. ????private?String?path="";??
  34. ????private?boolean?previewRuning=true;??
  35. ??
  36. ????@Override??
  37. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  38. ????????super.onCreate(savedInstanceState);??
  39. ????????setContentView(R.layout.activity_main);??
  40. ??????????
  41. ????????//初始化SurfaceView??
  42. ????????SurfaceView?mpreview?=?(SurfaceView)?this.findViewById(R.id.photo);???
  43. ????????SurfaceHolder?mSurfaceHolder?=?mpreview.getHolder();???
  44. ????????mSurfaceHolder.addCallback(new?SurfaceViewCallback());???
  45. ????????mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);???
  46. ??
  47. ??????????
  48. ????????btnSelf=(Button)super.findViewById(R.id.byself);??
  49. ??????????
  50. ????????//拍照(自定义相机)??
  51. ????????btnSelf.setOnClickListener(new?OnClickListener(){??
  52. ????????????public?void?onClick(View?v)??
  53. ????????????{????
  54. ????????????????if(camera!=null){??
  55. ????????????????????camera.autoFocus(new?AutoFocusCallbackimpl());??
  56. ????????????????}??
  57. ????????????}??
  58. ????????});??
  59. ??????????
  60. ????}??
  61. ??????
  62. ????public?class?SurfaceViewCallback?implements?SurfaceHolder.Callback{??
  63. ??????????
  64. ????????@Override??
  65. ????????public?void?surfaceChanged(SurfaceHolder?holder,int?format,int?width,int?heith){??
  66. ??
  67. ????????}??
  68. ??????????
  69. ????????@Override??
  70. ????????public?void?surfaceCreated(SurfaceHolder?holder){??
  71. ????????????//现在智能机可能会有多个镜头:一般前置为1;后置为0??
  72. ????????????MainActivity.this.camera=Camera.open(0);??
  73. ????????????//设置参数??
  74. ????????????Parameters?param=camera.getParameters();??
  75. ????????????param.setPictureFormat(PixelFormat.JPEG);??
  76. ????????????param.set("jpeg-quality",85);??
  77. ????????????param.setPreviewFrameRate(5);??
  78. ????????????camera.setParameters(param);??
  79. ??????????????
  80. ????????????try?{??
  81. ????????????????camera.setPreviewDisplay(holder);???//成像在SurfaceView??
  82. ????????????}?catch?(IOException?e)?{??
  83. ????????????????e.printStackTrace();??
  84. ????????????}??
  85. ??????????????
  86. ????????????//开始预览??
  87. ????????????camera.startPreview();??
  88. ????????????previewRuning=true;??
  89. ????????}??
  90. ??????????
  91. ????????@Override??
  92. ????????public?void?surfaceDestroyed(SurfaceHolder?holder){??
  93. ????????????if(camera!=null){??
  94. ????????????????if(previewRuning){??
  95. ????????????????????camera.stopPreview();??
  96. ????????????????????previewRuning=false;??
  97. ????????????????}??
  98. ????????????????camera.release();??
  99. ????????????}??
  100. ????????}??
  101. ????}??
  102. ??????
  103. ????//调用takePicture()方法时,自动执行pictureCallback回调方法??
  104. ????public?PictureCallback?picture=new?PictureCallback(){??
  105. ????????@Override??
  106. ????????public?void?onPictureTaken(byte[]?data,Camera?camera){????????
  107. ????????????Bitmap?bmp=BitmapFactory.decodeByteArray(data,?0,?data.length);??
  108. ????????????//判断外部存储卡是否存在??
  109. ????????????if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){??
  110. ????????????????Toast.makeText(getApplicationContext(),?"读取失败,SD存储卡不存在!",?Toast.LENGTH_LONG).show();????
  111. ????????????????return;??
  112. ????????????}??
  113. ??????????????
  114. ????????????//判断文件是否存在??
  115. ????????????path=Environment.getExternalStorageDirectory().toString()??
  116. ????????????????????+File.separator??
  117. ????????????????????+"genwoxue"??
  118. ????????????????????+File.separator??
  119. ????????????????????+System.currentTimeMillis()??
  120. ????????????????????+".jpg";??
  121. ??????????????
  122. ????????????File?file=new?File(path);??
  123. ????????????if(!file.exists()){??
  124. ????????????????File?vDirPath?=?file.getParentFile();???
  125. ????????????????vDirPath.mkdirs();???
  126. ????????????????Toast.makeText(getApplicationContext(),?"photo.jpg文件不存在!",?Toast.LENGTH_LONG).show();????
  127. ????????????????return;??
  128. ????????????}??
  129. ??????????????
  130. ????????????try?{??
  131. ????????????????BufferedOutputStream?bos=new?BufferedOutputStream(new?FileOutputStream(file));??
  132. ????????????????bmp.compress(Bitmap.CompressFormat.JPEG,?80,?bos);??
  133. ????????????????try?{??
  134. ????????????????????bos.flush();??
  135. ????????????????????bos.close();??
  136. ????????????????}?catch?(IOException?e)?{??
  137. ????????????????????e.printStackTrace();??
  138. ????????????????}??
  139. ??????????????????
  140. ????????????}?catch?(FileNotFoundException?e)?{??
  141. ????????????????e.printStackTrace();??
  142. ????????????}??
  143. ??????????????
  144. ????????????camera.stopPreview();??
  145. ????????????camera.startPreview();??
  146. ??????????????
  147. ????????}??
  148. ????};??
  149. ??
  150. ????//对焦回回调??
  151. ????public?class?AutoFocusCallbackimpl?implements?AutoFocusCallback{??
  152. ????????public?void?onAutoFocus(boolean?success,Camera?camera){??
  153. ??????????????
  154. ????????????if(success){??
  155. ????????????????camera.takePicture(shutter,?null,?picture);??
  156. ????????????????camera.stopPreview();??
  157. ????????????}??
  158. ????????}??
  159. ????}??
  160. ??????
  161. ????//快门回调??
  162. ????public?ShutterCallback?shutter=new?ShutterCallback(){??
  163. ????????public?void?onShutter(){??
  164. ??????????????
  165. ????????}??
  166. ????};??
  167. }??


3、运行结果

  第58章、拍照功能实现(从零开始学Android)