Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mycamera2;
- public class MainActivity extends ActionBarActivity
- {
- private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
- private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
- public static final int MEDIA_TYPE_IMAGE = 1;
- public static final int MEDIA_TYPE_VIDEO = 2;
- // directory name to store captured images and videos
- private static final String IMAGE_DIRECTORY_NAME = "Car Camera";
- private Uri fileUri; // file url to store image/video
- private ImageView imgPreview;
- private Button btnCapturePicture;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imgPreview = (ImageView) findViewById(R.id.imgPreview);
- btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
- btnCapturePicture.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- // capture picture
- captureImage();
- }
- });
- // Checking camera availability
- if (!isDeviceSupportCamera())
- {
- Toast.makeText(getApplicationContext(),"Sorry! Your device doesn't support camera",Toast.LENGTH_LONG).show();
- // will close the app if the device does't have camera
- finish();
- }
- }
- private boolean isDeviceSupportCamera()
- {
- if (getApplicationContext().getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA))
- {
- // this device has a camera
- return true;
- } else
- {
- // no camera on this device
- return false;
- }
- }
- private void captureImage()
- {
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
- intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
- // start the image capture Intent
- startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
- }
- @Override
- protected void onSaveInstanceState(Bundle outState)
- {
- super.onSaveInstanceState(outState);
- outState.putParcelable("file_uri", fileUri);
- }
- @Override
- protected void onRestoreInstanceState(Bundle savedInstanceState)
- {
- super.onRestoreInstanceState(savedInstanceState);
- // get the file url
- fileUri = savedInstanceState.getParcelable("file_uri");
- }
- /**
- * Receiving activity result method will be called after closing the camera
- * */
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- // if the result is capturing Image
- if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE)
- {
- if (resultCode == RESULT_OK)
- {
- // successfully captured the image
- // display it in image view
- previewCapturedImage();
- } else if (resultCode == RESULT_CANCELED)
- {
- // user cancelled Image capture
- Toast.makeText(getApplicationContext(),"User cancelled image capture", Toast.LENGTH_SHORT).show();
- } else
- {
- // failed to capture image
- Toast.makeText(getApplicationContext(),"Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
- }
- }
- }
- /**
- * Display image from a path to ImageView
- */
- private void previewCapturedImage()
- {
- try
- {
- imgPreview.setVisibility(View.VISIBLE);
- // bimatp factory
- BitmapFactory.Options options = new BitmapFactory.Options();
- // downsizing image as it throws OutOfMemory Exception for larger
- // images
- options.inSampleSize = 8;
- final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);
- imgPreview.setImageBitmap(bitmap);
- } catch (NullPointerException e)
- {
- e.printStackTrace();
- }
- }
- /**
- * Creating file uri to store image/video
- */
- public Uri getOutputMediaFileUri(int type)
- {
- return Uri.fromFile(getOutputMediaFile(type));
- }
- /**
- * returning image / video
- */
- private static File getOutputMediaFile(int type)
- {
- // External sdcard location
- String root = Environment.getExternalStorageDirectory().toString();
- //File myDir = new File(root + "/vanjasaved_images");
- // File mediaStorageDir = new File(root + "/.sidvanjasaved_images");
- File mediaStorageDir = new File(root + "/.external_sd");
- if (!mediaStorageDir.exists())
- {
- if (!mediaStorageDir.mkdirs())
- {
- Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
- + IMAGE_DIRECTORY_NAME + " directory");
- return null;
- }
- }
- // Create a media file name
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date());
- File mediaFile;
- if (type == MEDIA_TYPE_IMAGE)
- {
- mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "IMG_" + timeStamp + ".jpg");
- } else if (type == MEDIA_TYPE_VIDEO)
- {
- mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
- } else
- {
- return null;
- }
- return mediaFile;
- }
- }
- TimerTask timerTask = new TimerTask () {
- @Override
- public void run () {
- // your code here... check if 5 image taken and cancel the timer
- }
- };
- new Timer().schedule(timerTask, 2*60*1000);
- /**
- * flag to check if you have already lunched the handler
- */
- private static boolean isHandlerLunched = false;
- /**
- * Delay max to take 5 pictures
- *
- * @value in milliseconds
- */
- private static final int DELAY_MAX = 60*1000*2;
- /**
- * ArrayList<String> containing the URLS of the pictures
- */
- private static ArrayList<String> pictureUrls = new ArrayList<String>();
- /**
- * open the camera activity for result
- */
- private void captureImage()
- {
- // if it is the first click to take the first picture
- if (!isHandlerLunched){
- // initialize the list again
- pictureUrls = new ArrayList<String>();
- // lunch the handler to delay
- Handler handler = new Handler();
- handler.postDelayed(new Runnable(){
- public void run() {
- //TODO the delay is finished , you do your staff of checking
- // I propose you to add the URLs of each picture in an ArrayList<String>,
- // and here you check the size of this list
- if (pictureUrls.size()<5) {
- // do staff
- } else {
- // do staff
- }
- }
- }, DELAY_MAX );
- }
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
- intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
- // start the image capture Intent
- startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement