Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. package com.mycamera2;
  2.  
  3.  
  4.  
  5. public class MainActivity extends ActionBarActivity
  6. {
  7. private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
  8. private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
  9. public static final int MEDIA_TYPE_IMAGE = 1;
  10. public static final int MEDIA_TYPE_VIDEO = 2;
  11.  
  12. // directory name to store captured images and videos
  13. private static final String IMAGE_DIRECTORY_NAME = "Car Camera";
  14. private Uri fileUri; // file url to store image/video
  15. private ImageView imgPreview;
  16. private Button btnCapturePicture;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState)
  19. {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. imgPreview = (ImageView) findViewById(R.id.imgPreview);
  23. btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
  24. btnCapturePicture.setOnClickListener(new View.OnClickListener()
  25. {
  26. @Override
  27. public void onClick(View v)
  28. {
  29. // capture picture
  30. captureImage();
  31. }
  32. });
  33. // Checking camera availability
  34. if (!isDeviceSupportCamera())
  35. {
  36. Toast.makeText(getApplicationContext(),"Sorry! Your device doesn't support camera",Toast.LENGTH_LONG).show();
  37. // will close the app if the device does't have camera
  38. finish();
  39. }
  40. }
  41. private boolean isDeviceSupportCamera()
  42. {
  43. if (getApplicationContext().getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA))
  44. {
  45. // this device has a camera
  46. return true;
  47. } else
  48. {
  49. // no camera on this device
  50. return false;
  51. }
  52. }
  53.  
  54. private void captureImage()
  55. {
  56. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  57. fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
  58. intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
  59. // start the image capture Intent
  60. startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
  61. }
  62. @Override
  63. protected void onSaveInstanceState(Bundle outState)
  64. {
  65. super.onSaveInstanceState(outState);
  66. outState.putParcelable("file_uri", fileUri);
  67. }
  68.  
  69. @Override
  70. protected void onRestoreInstanceState(Bundle savedInstanceState)
  71. {
  72. super.onRestoreInstanceState(savedInstanceState);
  73. // get the file url
  74. fileUri = savedInstanceState.getParcelable("file_uri");
  75. }
  76. /**
  77. * Receiving activity result method will be called after closing the camera
  78. * */
  79. @Override
  80. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  81. {
  82. // if the result is capturing Image
  83. if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE)
  84. {
  85. if (resultCode == RESULT_OK)
  86. {
  87. // successfully captured the image
  88. // display it in image view
  89. previewCapturedImage();
  90. } else if (resultCode == RESULT_CANCELED)
  91. {
  92. // user cancelled Image capture
  93. Toast.makeText(getApplicationContext(),"User cancelled image capture", Toast.LENGTH_SHORT).show();
  94. } else
  95. {
  96. // failed to capture image
  97. Toast.makeText(getApplicationContext(),"Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
  98. }
  99. }
  100. }
  101.  
  102. /**
  103. * Display image from a path to ImageView
  104. */
  105. private void previewCapturedImage()
  106. {
  107. try
  108. {
  109. imgPreview.setVisibility(View.VISIBLE);
  110. // bimatp factory
  111. BitmapFactory.Options options = new BitmapFactory.Options();
  112. // downsizing image as it throws OutOfMemory Exception for larger
  113. // images
  114. options.inSampleSize = 8;
  115. final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);
  116. imgPreview.setImageBitmap(bitmap);
  117. } catch (NullPointerException e)
  118. {
  119. e.printStackTrace();
  120. }
  121. }
  122.  
  123. /**
  124. * Creating file uri to store image/video
  125. */
  126. public Uri getOutputMediaFileUri(int type)
  127. {
  128. return Uri.fromFile(getOutputMediaFile(type));
  129. }
  130.  
  131. /**
  132. * returning image / video
  133. */
  134. private static File getOutputMediaFile(int type)
  135. {
  136. // External sdcard location
  137. String root = Environment.getExternalStorageDirectory().toString();
  138. //File myDir = new File(root + "/vanjasaved_images");
  139. // File mediaStorageDir = new File(root + "/.sidvanjasaved_images");
  140. File mediaStorageDir = new File(root + "/.external_sd");
  141.  
  142. if (!mediaStorageDir.exists())
  143. {
  144. if (!mediaStorageDir.mkdirs())
  145. {
  146. Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
  147. + IMAGE_DIRECTORY_NAME + " directory");
  148. return null;
  149. }
  150. }
  151. // Create a media file name
  152. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date());
  153. File mediaFile;
  154. if (type == MEDIA_TYPE_IMAGE)
  155. {
  156. mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "IMG_" + timeStamp + ".jpg");
  157. } else if (type == MEDIA_TYPE_VIDEO)
  158. {
  159. mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
  160. } else
  161. {
  162. return null;
  163. }
  164. return mediaFile;
  165. }
  166. }
  167.  
  168. TimerTask timerTask = new TimerTask () {
  169. @Override
  170. public void run () {
  171. // your code here... check if 5 image taken and cancel the timer
  172. }
  173. };
  174. new Timer().schedule(timerTask, 2*60*1000);
  175.  
  176. /**
  177. * flag to check if you have already lunched the handler
  178. */
  179. private static boolean isHandlerLunched = false;
  180.  
  181. /**
  182. * Delay max to take 5 pictures
  183. *
  184. * @value in milliseconds
  185. */
  186. private static final int DELAY_MAX = 60*1000*2;
  187.  
  188. /**
  189. * ArrayList<String> containing the URLS of the pictures
  190. */
  191. private static ArrayList<String> pictureUrls = new ArrayList<String>();
  192.  
  193. /**
  194. * open the camera activity for result
  195. */
  196. private void captureImage()
  197. {
  198. // if it is the first click to take the first picture
  199. if (!isHandlerLunched){
  200. // initialize the list again
  201. pictureUrls = new ArrayList<String>();
  202. // lunch the handler to delay
  203. Handler handler = new Handler();
  204. handler.postDelayed(new Runnable(){
  205. public void run() {
  206. //TODO the delay is finished , you do your staff of checking
  207. // I propose you to add the URLs of each picture in an ArrayList<String>,
  208. // and here you check the size of this list
  209. if (pictureUrls.size()<5) {
  210. // do staff
  211. } else {
  212. // do staff
  213. }
  214. }
  215. }, DELAY_MAX );
  216. }
  217. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  218. fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
  219. intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
  220. // start the image capture Intent
  221. startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement