Chapter 58. Photo function realization (learn Android from scratch)

Come from: http://blog.csdn.net/jianghuiquan/article/details/8569187

Android has two ways to take photos: one is to call the system's photo Intent directly, use onActivityResult to get picture resources or specify a picture path, and read the picture after the photo returns successfully; the other is to use SurfaceView to customize the interface and add business personalization function.

1, The first method

1. Design interface

(1) layout documents

Open the activity main.xml file.

Enter the following code:

?

  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="Calling system camera does not return results"?/>??
  14. ??
  15. ????<Button??
  16. ????????android:id="@+id/byself"??
  17. ????????android:layout_width="wrap_content"??
  18. ????????android:layout_height="wrap_content"??
  19. ????????android:text="Call the system camera and return the result"?/>??
  20. ??
  21. ????<ImageView??
  22. ????????android:id="@+id/photo"??
  23. ????????android:layout_width="wrap_content"??
  24. ????????android:layout_height="wrap_content"?/>??
  25. ??
  26. </LinearLayout>??


2. Procedure documents

Open the "src/com.genwoxue.camera/MainActivity.java" file.

Then enter the following code:

  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. ????????//Call system camera, no result returned??
  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. ????????//Call the system camera and return the result??
  44. ????????btnSelf.setOnClickListener(new?OnClickListener(){??
  45. ????????????public?void?onClick(View?v)??
  46. ????????????{????
  47. ????????????????//Determine whether the external memory card exists??
  48. ????????????????if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){??
  49. ????????????????????Toast.makeText(getApplicationContext(),?"Read failed, SD Memory card does not exist!",?Toast.LENGTH_LONG).show();????
  50. ????????????????????return;??
  51. ????????????????}??
  52. ??????????????????
  53. ????????????????//Judge whether the file exists??
  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 File does not exist!",?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. Operation results

   

  

?

2, The second method.

1. Design interface

(1) layout documents

Open the activity main.xml file.

Enter the following code:

  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="Take a picture (custom camera)"?/>??
  15. ??????
  16. ????<SurfaceView??
  17. ????????android:id="@+id/photo"??
  18. ????????android:layout_width="300dip"??
  19. ????????android:layout_height="400dip"?/>??
  20. ??
  21. </LinearLayout>??


2. Procedure documents

Open the "Src / com. Genwoxue. Camera / mainactivity. Java" file.

Then enter the following code:

  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. ????????//Initialize 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. ????????//Take a picture (custom camera)??
  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. ????????????//Now the smart machine may have multiple lenses: generally, the front is 1; the back is 0??
  72. ????????????MainActivity.this.camera=Camera.open(0);??
  73. ????????????//Setting parameters??
  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);???//Imaging in SurfaceView??
  82. ????????????}?catch?(IOException?e)?{??
  83. ????????????????e.printStackTrace();??
  84. ????????????}??
  85. ??????????????
  86. ????????????//Start Preview??
  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. ????//When the takePicture() method is called, the pictureCallback method is automatically executed??
  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. ????????????//Determine whether the external memory card exists??
  109. ????????????if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){??
  110. ????????????????Toast.makeText(getApplicationContext(),?"Read failed, SD Memory card does not exist!",?Toast.LENGTH_LONG).show();????
  111. ????????????????return;??
  112. ????????????}??
  113. ??????????????
  114. ????????????//Judge whether the file exists??
  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 File does not exist!",?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. ????//Focus back callback??
  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. ????//Shutter callback??
  162. ????public?ShutterCallback?shutter=new?ShutterCallback(){??
  163. ????????public?void?onShutter(){??
  164. ??????????????
  165. ????????}??
  166. ????};??
  167. }??


3. Operation results

  


Tags: Android Java SurfaceView xml

Posted on Wed, 01 Apr 2020 15:23:34 -0400 by toxic_brain