ddkclaudio

Untitled

Jan 14th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. package com.example.cameraddk;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9.  
  10. import android.app.Activity;
  11. import android.hardware.Camera;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.os.Environment;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.widget.FrameLayout;
  19. import android.widget.ImageView;
  20. import android.hardware.Camera.PictureCallback;
  21.  
  22. public class CameraActivity extends Activity implements OnClickListener
  23. {
  24.     private static Camera mCamera;
  25.     private        CameraPreview mPreview;
  26.     private static ImageView capturar;
  27.    
  28.     @Override
  29.     public void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.camera_foto);
  32.  
  33.        // Create an instance of Camera
  34.         mCamera = getCameraInstance();
  35.        
  36.         // Create our Preview view and set it as the content of our activity.
  37.         mPreview = new CameraPreview(CameraActivity.this, mCamera);
  38.         FrameLayout preview = (FrameLayout) findViewById(R.id.frameCamera);
  39.         preview.addView(mPreview);
  40.        
  41.         capturar            = (ImageView) findViewById(R.id.btnCamerafotografar);
  42.         capturar.setClickable(true);
  43.         capturar.setOnClickListener(this);
  44.        
  45.        
  46.     }
  47.  
  48.  
  49.  
  50.     @Override
  51.     public void onClick(View v) {
  52.  
  53.         if( v == capturar)
  54.         {
  55.             mCamera.takePicture(null, null, mPicture);
  56.             mCamera.cl
  57.             finish();
  58.         }  
  59.        
  60.     }
  61.     public static Camera getCameraInstance(){
  62.        
  63.         try {
  64.             mCamera = Camera.open(); // attempt to get a Camera instance
  65.         }
  66.         catch (Exception e){
  67.             System.out.println("\n\n\ngetCameraInstance() NO ok");
  68.             // Camera is not available (in use or does not exist)
  69.         }
  70.         return mCamera; // returns null if camera is unavailable
  71.     }
  72.    
  73.     private PictureCallback mPicture = new PictureCallback() {
  74.  
  75.         @Override
  76.         public void onPictureTaken(byte[] data, Camera camera) {
  77.  
  78.             File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
  79.             if (pictureFile == null){
  80.                 //Log.d("CameraActivity", "Error creating media file, check storage permissions: " + e.getMessage());
  81.                 return;
  82.             }
  83.  
  84.             try {
  85.                 FileOutputStream fos = new FileOutputStream(pictureFile);
  86.                 fos.write(data);
  87.                 fos.close();
  88.             } catch (FileNotFoundException e) {
  89.                 Log.d("CameraActivity", "File not found: " + e.getMessage());
  90.             } catch (IOException e) {
  91.                 Log.d("CameraActivity", "Error accessing file: " + e.getMessage());
  92.             }
  93.         }
  94.     };
  95.  
  96.  
  97.  
  98.    
  99.    
  100.    
  101.    
  102.    
  103.    
  104.    
  105.    
  106.    
  107.    
  108.    
  109.    
  110.    
  111.    
  112.    
  113.    
  114.    
  115.    
  116.    
  117.    
  118.    
  119.    
  120.    
  121.    
  122.    
  123.    
  124.    
  125.    
  126.    
  127.    
  128.    
  129.    
  130.    
  131.    
  132.    
  133.    
  134.    
  135.    
  136.    
  137.    
  138.    
  139.    
  140.     public static final int MEDIA_TYPE_IMAGE = 1;
  141.     public static final int MEDIA_TYPE_VIDEO = 2;
  142.  
  143.     private static Uri getOutputMediaFileUri(int type){
  144.           return Uri.fromFile(getOutputMediaFile(type));
  145.     }
  146.  
  147.     private static File getOutputMediaFile(int type){
  148.         // To be safe, you should check that the SDCard is mounted
  149.         // using Environment.getExternalStorageState() before doing this.
  150.  
  151.         File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "ArtespDDK/Media/Foto");
  152.         // This location works best if you want the created images to be shared
  153.         // between applications and persist after your app has been uninstalled.
  154.  
  155.         // Create the storage directory if it does not exist
  156.         if (! mediaStorageDir.exists()){
  157.             if (! mediaStorageDir.mkdirs()){
  158.                 Log.d("MyCameraApp", "failed to create directory");
  159.                 return null;
  160.             }
  161.         }
  162.  
  163.         // Create a media file name
  164.         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  165.         File mediaFile;
  166.         if (type == MEDIA_TYPE_IMAGE){
  167.             mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  168.             "IMG_"+ timeStamp + ".jpg");
  169.         } else if(type == MEDIA_TYPE_VIDEO) {
  170.             mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  171.             "VID_"+ timeStamp + ".mp4");
  172.         } else {
  173.             return null;
  174.         }
  175.  
  176.         return mediaFile;
  177.     }
  178.  
  179.    
  180. }
Advertisement
Add Comment
Please, Sign In to add comment