Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CameraAppActivity.java
- package my.apps.cameraApp;
- 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 utils.fileWork;
- import my.apps.cameraApp.R.id;
- import android.app.Activity;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.hardware.Camera;
- import android.hardware.Camera.PictureCallback;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- public class CameraAppActivity extends Activity {
- private Camera mCamera;
- private CameraPreview mPreview;
- public static final int MEDIA_TYPE_IMAGE = 1;
- fileWork fw = new fileWork();
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // Create an instance of Camera
- mCamera = getCameraInstance();
- // Create our Preview view and set it as the content of our activity.
- mPreview = new CameraPreview(this, mCamera);
- FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
- preview.addView(mPreview);
- //Adding listener
- Button captureButton = (Button) findViewById(id.button_capture);
- captureButton.setOnClickListener(
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mCamera.takePicture(null, null, mPicture);
- // mCamera.stopPreview();HERE IS WHERE I GET THE ERROR
- // mCamera.startPreview();
- }
- });
- File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
- Environment.DIRECTORY_PICTURES), "MyCameraApp");
- TextView nr = (TextView) findViewById(id.textView2);
- int len = mediaStorageDir.list().length;
- nr.setText( "Frames captured: " + Integer.toString(len));
- }
- // @Override
- /* public void onBackPressed() {
- // do something on back.
- mCamera.stopPreview();
- mCamera.release();
- // showMessage(this, "BAMBAM!");
- return;
- }*/
- PictureCallback mPicture = new PictureCallback() {
- @Override
- public void onPictureTaken(byte[] data, Camera camera) {
- File pictureFile = getOutputMediaFile();
- if (pictureFile == null){
- return;
- }
- try {
- FileOutputStream fos = new FileOutputStream(pictureFile);
- fos.write(data);
- fos.close();
- } catch (FileNotFoundException e) {
- } catch (IOException e) {
- }
- }
- };
- public void showMessage(Context context, CharSequence text)
- {
- int duration = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(context, text, duration);
- toast.show();
- }
- /** Create a File for saving the image */
- private static File getOutputMediaFile(){
- File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
- Environment.DIRECTORY_PICTURES), "MyCameraApp");
- 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;
- mediaFile = new File(mediaStorageDir.getPath() + File.separator +
- "IMG_"+ timeStamp + ".jpg");
- return mediaFile;
- }
- /** Check if this device has a camera */
- private boolean checkCameraHardware(Context context) {
- if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
- // this device has a camera
- return true;
- } else {
- // no camera on this device
- return false;
- }
- }
- /** A safe way to get an instance of the Camera object. */
- public static Camera getCameraInstance(){
- Camera c = null;
- try {
- c = Camera.open(1); // attempt to get a Camera instance
- }
- catch (Exception e){
- // Camera is not available (in use or does not exist)
- }
- return c; // returns null if camera is unavailable
- }
- private void releaseCamera(){
- if (mCamera != null){
- mCamera.release(); // release the camera for other applications
- mCamera = null;
- }
- }
- protected void onPause() {
- super.onPause();
- // releaseCamera(); // release the camera immediately on pause event
- }
- }
- CameraPreview.java
- package my.apps.cameraApp;
- import java.io.IOException;
- import android.content.Context;
- import android.hardware.Camera;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- /** A basic Camera preview class */
- public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
- private SurfaceHolder mSurfaceHolder;
- private Camera mCamera;
- //Constructor that obtains context and camera
- public CameraPreview(Context context, Camera camera) {
- super(context);
- this.mCamera = camera;
- this.mSurfaceHolder = this.getHolder();
- this.mSurfaceHolder.addCallback(this); // we get notified when underlying surface is created and destroyed
- this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //this is a deprecated method, is not requierd after 3.0
- }
- @Override
- public void surfaceCreated(SurfaceHolder surfaceHolder) {
- try {
- mCamera.setPreviewDisplay(surfaceHolder);
- mCamera.startPreview();
- } catch (IOException e) {
- // left blank for now
- }
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
- mCamera.stopPreview();
- mCamera.release();
- }
- @Override
- public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
- int width, int height) {
- // start preview with new settings
- try {
- mCamera.setPreviewDisplay(surfaceHolder);
- mCamera.startPreview();
- } catch (Exception e) {
- // intentionally left blank for a test
- }
- }
- public SurfaceHolder getSurfaceHolder()
- {
- return this.mSurfaceHolder;
- }
- }
- The layout, main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <FrameLayout
- android:id="@+id/camera_preview"
- android:layout_width="138dp"
- android:layout_height="126dp" >
- </FrameLayout>
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Video surveillance"
- android:textAppearance="?android:attr/textAppearanceSmall" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Small Text"
- android:textAppearance="?android:attr/textAppearanceSmall" />
- <Button
- android:id="@+id/button_capture"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Capture" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment