This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 25th, 2012  |  syntax: None  |  size: 5.37 KB  |  views: 208  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.BufferedOutputStream;  
  2. import java.io.File;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.  
  7. import android.content.Context;  
  8. import android.graphics.Bitmap;  
  9. import android.graphics.BitmapFactory;  
  10. import android.graphics.Canvas;  
  11. import android.graphics.PixelFormat;  
  12. import android.hardware.Camera;  
  13. import android.util.Log;  
  14. import android.view.KeyEvent;  
  15. import android.view.SurfaceHolder;  
  16. import android.view.SurfaceView;  
  17.  
  18.  
  19. public class PreView extends SurfaceView implements SurfaceHolder.Callback {  
  20.  
  21.     private SurfaceHolder holder = null;  
  22.     private Camera mCamera = null;  
  23.     private Bitmap mBitmap = null;  
  24.  
  25.     public PreView(Context context) {  
  26.         super(context);  
  27.         Log.i("TAG", "PreView()");  
  28.         // TODO Auto-generated constructor stub  
  29.         holder = this.getHolder();  
  30.         holder.addCallback(this);  
  31.         holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
  32.     }  
  33.  
  34.     public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  35.             int height) {  
  36.         Log.i("TAG", "surfaceChanged");  
  37.         // TODO Auto-generated method stub  
  38.         Camera.Parameters parameters = mCamera.getParameters();  
  39.         parameters.setPictureFormat(PixelFormat.JPEG);
  40.         parameters.setPreviewSize(320, 480);
  41. //      parameters.setPictureSize(320, 480);
  42.         mCamera.setParameters(parameters);  
  43.         mCamera.startPreview();
  44.     }  
  45.  
  46.     public void surfaceCreated(SurfaceHolder holder) {  
  47.         Log.i("TAG", "surfaceCreated");  
  48.         // TODO Auto-generated method stub  
  49.         mCamera = Camera.open();
  50.         try {  
  51.             mCamera.setPreviewDisplay(holder);
  52.         } catch (IOException e) {  
  53.             mCamera.release();
  54.             mCamera = null;  
  55.         }  
  56.     }  
  57.  
  58.     public void surfaceDestroyed(SurfaceHolder holder) {  
  59.         Log.i("TAG", "surfaceDestroyed");  
  60.         // TODO Auto-generated method stub  
  61.         mCamera.stopPreview();
  62.         mCamera = null;  
  63.     }  
  64.  
  65.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  66.         if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){  
  67.             if(mCamera !=null){  
  68.                 mCamera.takePicture(null, null,pic);  
  69.             }  
  70.         }  
  71.         return super.onKeyDown(keyCode, event);  
  72.     }  
  73.    
  74.     public Camera.PictureCallback pic = new Camera.PictureCallback(){  
  75.  
  76.         public void onPictureTaken(byte[] data, Camera camera) {  
  77.             // TODO Auto-generated method stub  
  78.             mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
  79.             File f = new File(android.os.Environment.getExternalStorageDirectory()+"/camera1.jpg");  
  80.             try {  
  81.                 BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));  
  82.                 mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);  
  83.                 os.flush();  
  84.                 os.close();  
  85.                 Canvas canvas = holder.lockCanvas();  
  86.                 canvas.drawBitmap(mBitmap, 0, 0, null);  
  87.                 holder.unlockCanvasAndPost(canvas);  
  88.             } catch (FileNotFoundException e) {  
  89.                 // TODO Auto-generated catch block  
  90.                 e.printStackTrace();  
  91.             } catch (IOException e) {  
  92.                 // TODO Auto-generated catch block  
  93.                 e.printStackTrace();  
  94.             }  
  95.         }  
  96.          
  97.     };  
  98. }  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. ///**************************************************CameraApplication
  105.  
  106. import android.app.Activity;  
  107. import android.os.Bundle;  
  108. import android.view.KeyEvent;  
  109. import android.view.Window;  
  110.  
  111. public class CameraApplication extends Activity {  
  112.     /** Called when the activity is first created. */  
  113.     private PreView pv = null;  
  114.     @Override  
  115.     public void onCreate(Bundle savedInstanceState) {  
  116.         super.onCreate(savedInstanceState);  
  117.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  118.         pv = new PreView(this);  
  119.         setContentView(pv);  
  120.     }  
  121.  
  122.     @Override  
  123.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  124.         // TODO Auto-generated method stub  
  125.         return pv.onKeyDown(keyCode, event);  
  126.     }  
  127.      
  128. }  
  129.  
  130.  
  131.  
  132.  
  133. ///****************************************************AndroidManifest XML:
  134.  
  135. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  136.     package="com.camapp"
  137.     android:versionCode="1"
  138.     android:versionName="1.0" >
  139.  
  140.     <uses-sdk
  141.         android:minSdkVersion="8"
  142.         android:targetSdkVersion="15" />
  143.  
  144.     <uses-permission android:name="android.permission.CAMERA" />  
  145.  <uses-feature android:name="android.hardware.camera" />  
  146.  <uses-feature android:name="android.hardware.camera.autofocus" />  
  147.  
  148.     <application
  149.         android:icon="@drawable/ic_launcher"
  150.         android:label="@string/app_name"
  151.         android:theme="@style/AppTheme" >
  152.         <activity
  153.             android:name=".CameraApplication"
  154.             android:label="@string/title_activity_starting" >
  155.             <intent-filter>
  156.                 <action android:name="android.intent.action.MAIN" />
  157.  
  158.                 <category android:name="android.intent.category.LAUNCHER" />
  159.             </intent-filter>
  160.         </activity>
  161.     </application>
  162.  
  163. </manifest>
clone this paste RAW Paste Data