Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.cameraddk;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import android.app.Activity;
- import android.hardware.Camera;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.hardware.Camera.PictureCallback;
- public class CameraActivity extends Activity implements OnClickListener
- {
- private static Camera mCamera;
- private CameraPreview mPreview;
- private static ImageView capturar;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.camera_foto);
- // Create an instance of Camera
- mCamera = getCameraInstance();
- // Create our Preview view and set it as the content of our activity.
- mPreview = new CameraPreview(CameraActivity.this, mCamera);
- FrameLayout preview = (FrameLayout) findViewById(R.id.frameCamera);
- preview.addView(mPreview);
- capturar = (ImageView) findViewById(R.id.btnCamerafotografar);
- capturar.setClickable(true);
- capturar.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if( v == capturar)
- {
- mCamera.takePicture(null, null, mPicture);
- mCamera.cl
- finish();
- }
- }
- public static Camera getCameraInstance(){
- try {
- mCamera = Camera.open(); // attempt to get a Camera instance
- }
- catch (Exception e){
- System.out.println("\n\n\ngetCameraInstance() NO ok");
- // Camera is not available (in use or does not exist)
- }
- return mCamera; // returns null if camera is unavailable
- }
- private PictureCallback mPicture = new PictureCallback() {
- @Override
- public void onPictureTaken(byte[] data, Camera camera) {
- File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
- if (pictureFile == null){
- //Log.d("CameraActivity", "Error creating media file, check storage permissions: " + e.getMessage());
- return;
- }
- try {
- FileOutputStream fos = new FileOutputStream(pictureFile);
- fos.write(data);
- fos.close();
- } catch (FileNotFoundException e) {
- Log.d("CameraActivity", "File not found: " + e.getMessage());
- } catch (IOException e) {
- Log.d("CameraActivity", "Error accessing file: " + e.getMessage());
- }
- }
- };
- public static final int MEDIA_TYPE_IMAGE = 1;
- public static final int MEDIA_TYPE_VIDEO = 2;
- private static Uri getOutputMediaFileUri(int type){
- return Uri.fromFile(getOutputMediaFile(type));
- }
- private static File getOutputMediaFile(int type){
- // To be safe, you should check that the SDCard is mounted
- // using Environment.getExternalStorageState() before doing this.
- File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "ArtespDDK/Media/Foto");
- // This location works best if you want the created images to be shared
- // between applications and persist after your app has been uninstalled.
- // Create the storage directory if it does not exist
- if (! mediaStorageDir.exists()){
- if (! mediaStorageDir.mkdirs()){
- Log.d("MyCameraApp", "failed to create directory");
- return null;
- }
- }
- // Create a media file name
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment