Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1. package com.exercise.AndroidCamera;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.lang.reflect.Method;
  8.  
  9. import android.app.Activity;
  10. import android.content.ContentValues;
  11. import android.content.Intent;
  12. import android.content.pm.ActivityInfo;
  13. import android.content.res.Configuration;
  14. import android.graphics.PixelFormat;
  15. import android.hardware.Camera;
  16. import android.hardware.Camera.PictureCallback;
  17. import android.hardware.Camera.ShutterCallback;
  18. import android.net.Uri;
  19. import android.os.Build;
  20. import android.os.Bundle;
  21. import android.os.Environment;
  22. import android.provider.MediaStore;
  23. import android.provider.MediaStore.Images.Media;
  24. import android.util.Log;
  25. import android.view.LayoutInflater;
  26. import android.view.SurfaceHolder;
  27. import android.view.SurfaceView;
  28. import android.view.View;
  29. import android.view.ViewGroup.LayoutParams;
  30. import android.widget.Button;
  31. import android.widget.Toast;
  32.  
  33. public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
  34.  
  35.     Camera camera;
  36.     SurfaceView surfaceView;
  37.     SurfaceHolder surfaceHolder;
  38.     boolean previewing = false;
  39.     LayoutInflater controlInflater = null;
  40.    
  41.     final int RESULT_SAVEIMAGE = 0;
  42.    
  43.     /** Called when the activity is first created. */
  44.     @Override
  45.     public void onCreate(Bundle savedInstanceState) {
  46.         super.onCreate(savedInstanceState);
  47.         setContentView(R.layout.main);
  48.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  49.        
  50.         getWindow().setFormat(PixelFormat.UNKNOWN);
  51.         surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
  52.         surfaceHolder = surfaceView.getHolder();
  53.         surfaceHolder.addCallback(this);
  54.         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  55.        
  56.         controlInflater = LayoutInflater.from(getBaseContext());
  57.         View viewControl = controlInflater.inflate(R.layout.control, null);
  58.         LayoutParams layoutParamsControl
  59.             = new LayoutParams(LayoutParams.FILL_PARENT,
  60.             LayoutParams.FILL_PARENT);
  61.         this.addContentView(viewControl, layoutParamsControl);
  62.        
  63.         Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
  64.         buttonTakePicture.setOnClickListener(new Button.OnClickListener(){
  65.  
  66.             public void onClick(View arg0) {
  67.                 // TODO Auto-generated method stub
  68.                 camera.takePicture(myShutterCallback,
  69.                         myPictureCallback_RAW, myPictureCallback_JPG);
  70.                 Intent intent = new Intent(getBaseContext(), Punch.class);
  71.                 intent.putExtra("ImageReference", "");
  72.                 startActivity(intent);
  73.  
  74.             }});
  75.     }
  76.    
  77.     ShutterCallback myShutterCallback = new ShutterCallback(){
  78.  
  79.         public void onShutter() {
  80.             // TODO Auto-generated method stub
  81.            
  82.         }};
  83.        
  84.     PictureCallback myPictureCallback_RAW = new PictureCallback(){
  85.  
  86.         public void onPictureTaken(byte[] arg0, Camera arg1) {
  87.             // TODO Auto-generated method stub
  88.            
  89.         }};
  90.        
  91.     PictureCallback myPictureCallback_JPG = new PictureCallback(){
  92.  
  93.         public void onPictureTaken(byte[] arg0, Camera arg1) {
  94.             // TODO Auto-generated method stub
  95.             /*Bitmap bitmapPicture
  96.                 = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */
  97.             int imageNum = 0;
  98.             Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  99.             File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
  100.             imagesFolder.mkdirs(); // <----
  101.             String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
  102.             File output = new File(imagesFolder, fileName);
  103.             while (output.exists()){
  104.                 imageNum++;
  105.                 fileName = "image_" + String.valueOf(imageNum) + ".jpg";
  106.                 output = new File(imagesFolder, fileName);
  107.             }
  108.  
  109.             Uri uriSavedImage = Uri.fromFile(output);
  110.             imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
  111.  
  112.  
  113.             OutputStream imageFileOS;
  114.             try {
  115.                 imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
  116.                 imageFileOS.write(arg0);
  117.                 imageFileOS.flush();
  118.                 imageFileOS.close();
  119.                
  120.                 Toast.makeText(AndroidCamera.this,
  121.                         "Image saved",
  122.                         Toast.LENGTH_LONG).show();
  123.                
  124.             } catch (FileNotFoundException e) {
  125.                 // TODO Auto-generated catch block
  126.                 e.printStackTrace();
  127.             } catch (IOException e) {
  128.                 // TODO Auto-generated catch block
  129.                 e.printStackTrace();
  130.             }
  131.  
  132.             camera.startPreview();
  133.         }};
  134.  
  135.     public void surfaceChanged(SurfaceHolder holder, int format, int width,
  136.             int height) {
  137.         // TODO Auto-generated method stub
  138.         if(previewing){
  139.             camera.stopPreview();
  140.             previewing = false;
  141.         }
  142.        
  143.         if (camera != null){
  144.             try {
  145.                 camera.setPreviewDisplay(surfaceHolder);
  146.                 camera.startPreview();
  147.                 previewing = true;
  148.             } catch (IOException e) {
  149.                 // TODO Auto-generated catch block
  150.                 e.printStackTrace();
  151.             }
  152.         }
  153.     }
  154.  
  155.     public void surfaceCreated(SurfaceHolder holder) {
  156.         // TODO Auto-generated method stub
  157.        
  158.         camera = Camera.open();
  159.         try {
  160.                Camera.Parameters parameters = camera.getParameters();
  161.                if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
  162.                   // This is an undocumented although widely known feature
  163.                   parameters.set("orientation", "portrait");
  164.                   // For Android 2.2 and above
  165.                   camera.setDisplayOrientation(90);
  166.                   // Uncomment for Android 2.0 and above
  167.                   parameters.setRotation(90);
  168.                } else {
  169.                   // This is an undocumented although widely known feature
  170.                   parameters.set("orientation", "landscape");
  171.                   // For Android 2.2 and above
  172.                   camera.setDisplayOrientation(0);
  173.                   // Uncomment for Android 2.0 and above
  174.                   parameters.setRotation(0);
  175.                }
  176.               camera.setParameters(parameters);
  177.               camera.setPreviewDisplay(holder);
  178.           } catch (IOException exception) {
  179.              camera.release();
  180.            
  181.            }
  182.             camera.startPreview();
  183.            
  184.         }
  185.  
  186.  
  187.     public void surfaceDestroyed(SurfaceHolder holder) {
  188.         // TODO Auto-generated method stub
  189.         camera.stopPreview();
  190.         camera.release();
  191.         camera = null;
  192.         previewing = false;
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement