Guest User

Untitled

a guest
May 5th, 2012
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. CameraAppActivity.java
  2. package my.apps.cameraApp;
  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. import utils.fileWork;
  10.  
  11. import my.apps.cameraApp.R.id;
  12. import android.app.Activity;
  13. import android.content.Context;
  14. import android.content.pm.PackageManager;
  15. import android.hardware.Camera;
  16. import android.hardware.Camera.PictureCallback;
  17. import android.os.Bundle;
  18. import android.os.Environment;
  19. import android.util.Log;
  20. import android.view.View;
  21. import android.widget.Button;
  22. import android.widget.FrameLayout;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25.  
  26.  
  27. public class CameraAppActivity extends Activity {
  28.  
  29.  
  30. private Camera mCamera;
  31. private CameraPreview mPreview;
  32.  
  33. public static final int MEDIA_TYPE_IMAGE = 1;
  34. fileWork fw = new fileWork();
  35.  
  36. /** Called when the activity is first created. */
  37. @Override
  38. public void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.main);
  41. // Create an instance of Camera
  42. mCamera = getCameraInstance();
  43.  
  44. // Create our Preview view and set it as the content of our activity.
  45. mPreview = new CameraPreview(this, mCamera);
  46.  
  47.  
  48. FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
  49.  
  50. preview.addView(mPreview);
  51.  
  52.  
  53. //Adding listener
  54. Button captureButton = (Button) findViewById(id.button_capture);
  55. captureButton.setOnClickListener(
  56. new View.OnClickListener() {
  57.  
  58. @Override
  59. public void onClick(View v) {
  60. mCamera.takePicture(null, null, mPicture);
  61.  
  62.  
  63.  
  64. // mCamera.stopPreview();HERE IS WHERE I GET THE ERROR
  65. // mCamera.startPreview();
  66.  
  67.  
  68.  
  69. }
  70. });
  71.  
  72.  
  73.  
  74. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  75. Environment.DIRECTORY_PICTURES), "MyCameraApp");
  76. TextView nr = (TextView) findViewById(id.textView2);
  77. int len = mediaStorageDir.list().length;
  78. nr.setText( "Frames captured: " + Integer.toString(len));
  79.  
  80.  
  81. }
  82.  
  83.  
  84.  
  85. // @Override
  86. /* public void onBackPressed() {
  87. // do something on back.
  88. mCamera.stopPreview();
  89. mCamera.release();
  90. // showMessage(this, "BAMBAM!");
  91. return;
  92. }*/
  93.  
  94.  
  95. PictureCallback mPicture = new PictureCallback() {
  96.  
  97. @Override
  98. public void onPictureTaken(byte[] data, Camera camera) {
  99. File pictureFile = getOutputMediaFile();
  100. if (pictureFile == null){
  101. return;
  102. }
  103.  
  104. try {
  105. FileOutputStream fos = new FileOutputStream(pictureFile);
  106. fos.write(data);
  107. fos.close();
  108. } catch (FileNotFoundException e) {
  109.  
  110. } catch (IOException e) {
  111.  
  112. }
  113.  
  114. }
  115.  
  116. };
  117.  
  118.  
  119. public void showMessage(Context context, CharSequence text)
  120. {
  121.  
  122. int duration = Toast.LENGTH_SHORT;
  123.  
  124. Toast toast = Toast.makeText(context, text, duration);
  125. toast.show();
  126.  
  127. }
  128. /** Create a File for saving the image */
  129. private static File getOutputMediaFile(){
  130. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  131. Environment.DIRECTORY_PICTURES), "MyCameraApp");
  132.  
  133.  
  134. if (! mediaStorageDir.exists()){
  135. if (! mediaStorageDir.mkdirs()){
  136. Log.d("MyCameraApp", "failed to create directory");
  137. return null;
  138. }
  139. }
  140.  
  141. // Create a media file name
  142. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  143. File mediaFile;
  144. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  145. "IMG_"+ timeStamp + ".jpg");
  146.  
  147. return mediaFile;
  148. }
  149.  
  150.  
  151.  
  152. /** Check if this device has a camera */
  153. private boolean checkCameraHardware(Context context) {
  154. if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
  155. // this device has a camera
  156. return true;
  157. } else {
  158. // no camera on this device
  159. return false;
  160. }
  161. }
  162.  
  163. /** A safe way to get an instance of the Camera object. */
  164. public static Camera getCameraInstance(){
  165. Camera c = null;
  166. try {
  167. c = Camera.open(1); // attempt to get a Camera instance
  168. }
  169. catch (Exception e){
  170. // Camera is not available (in use or does not exist)
  171. }
  172. return c; // returns null if camera is unavailable
  173. }
  174. private void releaseCamera(){
  175. if (mCamera != null){
  176. mCamera.release(); // release the camera for other applications
  177. mCamera = null;
  178. }
  179. }
  180.  
  181. protected void onPause() {
  182. super.onPause();
  183.  
  184. // releaseCamera(); // release the camera immediately on pause event
  185. }
  186.  
  187.  
  188. }
  189.  
  190.  
  191.  
  192. CameraPreview.java
  193.  
  194.  
  195. package my.apps.cameraApp;
  196.  
  197.  
  198. import java.io.IOException;
  199.  
  200. import android.content.Context;
  201. import android.hardware.Camera;
  202. import android.util.Log;
  203. import android.view.SurfaceHolder;
  204. import android.view.SurfaceView;
  205.  
  206. /** A basic Camera preview class */
  207. public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
  208.  
  209. private SurfaceHolder mSurfaceHolder;
  210. private Camera mCamera;
  211.  
  212. //Constructor that obtains context and camera
  213. public CameraPreview(Context context, Camera camera) {
  214. super(context);
  215. this.mCamera = camera;
  216.  
  217. this.mSurfaceHolder = this.getHolder();
  218. this.mSurfaceHolder.addCallback(this); // we get notified when underlying surface is created and destroyed
  219. this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //this is a deprecated method, is not requierd after 3.0
  220. }
  221.  
  222. @Override
  223. public void surfaceCreated(SurfaceHolder surfaceHolder) {
  224. try {
  225. mCamera.setPreviewDisplay(surfaceHolder);
  226. mCamera.startPreview();
  227. } catch (IOException e) {
  228. // left blank for now
  229. }
  230.  
  231. }
  232.  
  233. @Override
  234. public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
  235. mCamera.stopPreview();
  236. mCamera.release();
  237. }
  238.  
  239. @Override
  240. public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
  241. int width, int height) {
  242. // start preview with new settings
  243. try {
  244. mCamera.setPreviewDisplay(surfaceHolder);
  245. mCamera.startPreview();
  246. } catch (Exception e) {
  247. // intentionally left blank for a test
  248. }
  249. }
  250. public SurfaceHolder getSurfaceHolder()
  251. {
  252. return this.mSurfaceHolder;
  253. }
  254.  
  255. }
  256.  
  257.  
  258. The layout, main.xml
  259.  
  260.  
  261. <?xml version="1.0" encoding="utf-8"?>
  262. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  263. android:orientation="vertical"
  264. android:layout_width="fill_parent"
  265. android:layout_height="fill_parent"
  266. >
  267.  
  268. <FrameLayout
  269. android:id="@+id/camera_preview"
  270. android:layout_width="138dp"
  271. android:layout_height="126dp" >
  272.  
  273. </FrameLayout>
  274.  
  275.  
  276. <TextView
  277. android:id="@+id/textView1"
  278. android:layout_width="wrap_content"
  279. android:layout_height="wrap_content"
  280. android:text="Video surveillance"
  281. android:textAppearance="?android:attr/textAppearanceSmall" />
  282.  
  283. <TextView
  284. android:id="@+id/textView2"
  285. android:layout_width="wrap_content"
  286. android:layout_height="wrap_content"
  287. android:text="Small Text"
  288. android:textAppearance="?android:attr/textAppearanceSmall" />
  289.  
  290.  
  291. <Button
  292. android:id="@+id/button_capture"
  293. android:layout_width="wrap_content"
  294. android:layout_height="wrap_content"
  295. android:text="Capture" />
  296.  
  297. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment