Android Open Source - Android-CriminalIntent Crime Camera Fragment






From Project

Back to project page Android-CriminalIntent.

License

The source code is released under:

MIT License

If you think the Android project Android-CriminalIntent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bignerdranch.android.criminalintent;
//from   ww w.  jav  a2s .  c  o  m


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import static android.view.View.OnClickListener;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 *
 */
public class CrimeCameraFragment extends Fragment {
    private static final String TAG = CrimeCameraFragment.class.getSimpleName();

    public static final String EXTRA_PHOTO_FILENAME =
            "com.bignerdranch.android.criminialintent.photo_filename";
    public static final String EXTRA_PHOTO_ORIENTATION =
            "com.bignerdranch.android.criminialintent.photo_orientation";

    private Camera mCamera;
    private SurfaceView mSurfaceView;
    private View mProgressContainer;

    public CrimeCameraFragment() {
        // Required empty public constructor
    }

    @Override
//    @SuppressWarnings("deprecation")
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_crime_camera, container, false);
        Button button = (Button) view.findViewById(R.id.crime_camera_takePictureButton);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCamera != null) {
                    mCamera.takePicture(mShutterCallback, null, mJpegCallback);
                }
            }
        });

        mProgressContainer = view.findViewById(R.id.crime_camera_progressContainer);
        mProgressContainer.setVisibility(View.INVISIBLE);

        mSurfaceView = (SurfaceView) view.findViewById(R.id.crime_camera_surfaceView);
        SurfaceHolder holder = mSurfaceView.getHolder();
        // setType() and SURFACE_TYPE_PUSH_BUFFERS are deprecated
        // but required for pre-Honeycomb devices
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                // Tell the camera to use this Surface as its preview area
                try {
                    if (mCamera != null) {
                        mCamera.setPreviewDisplay(holder);
                    }
                } catch (IOException ioe) {
                    Log.e(TAG, "Exception configuring preview display: " + ioe);
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                if (mCamera == null) return;

                // Surface has changed size; update the camera preview size
                Camera.Parameters parameters = mCamera.getParameters();
                Camera.Size size = getBestSupportedSize(parameters.getSupportedPreviewSizes(), width, height);
                parameters.setPreviewSize(size.width, size.height);
                size = getBestSupportedSize(parameters.getSupportedPictureSizes(), width, height);
                parameters.setPictureSize(size.width, size.height);
                mCamera.setParameters(parameters);

                try {
                    mCamera.startPreview();
                } catch (Exception e) {
                    Log.e(TAG, "Exception starting preview: " + e);
                    mCamera.release();
                    mCamera = null;
                }
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // We can no longer display on this surface, so stop the preview
                if (mCamera != null) {
                    mCamera.stopPreview();
                }
            }
        });

        return view;
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onResume() {
        super.onResume();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            mCamera = Camera.open(0); // request first camera available
        } else {
            mCamera = Camera.open();
        }
    }

    @Override
    public void onPause() {
        super.onPause();

        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

    private Camera.Size getBestSupportedSize(List<Camera.Size> sizeList, int width, int height) {
        Camera.Size bestSize = sizeList.get(0);
        int largestArea = bestSize.width * bestSize.height;
        for (Camera.Size s: sizeList) {
            int area = s.width * s.height;
            if (area > largestArea) {
                bestSize = s;
                largestArea = area;
            }
        }
        return bestSize;
    }

    private Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
        @Override
        public void onShutter() {
            mProgressContainer.setVisibility(View.VISIBLE);
        }
    };

    private Camera.PictureCallback mJpegCallback = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // Create a filename
            String filename = UUID.randomUUID().toString() + ".jpg";

            // Save jpeg data to storage
            boolean success = true;
            FileOutputStream os = null;

            try {
                os = getActivity().openFileOutput(filename, Context.MODE_PRIVATE);
                os.write(data);
            } catch (Exception e) {
                Log.e(TAG, "Error writing to file: " + filename, e);
                success = false;
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Error closing file: " + filename, e);
                    success = false;
                }
            }

            if (success) {
                // Set the photo filename on the result intent
                Intent intent = new Intent();
                intent.putExtra(EXTRA_PHOTO_FILENAME, filename);
                intent.putExtra(EXTRA_PHOTO_ORIENTATION, getActivity().getResources().getConfiguration().orientation);
                getActivity().setResult(Activity.RESULT_OK, intent);
            } else {
                getActivity().setResult(Activity.RESULT_CANCELED);
            }
            getActivity().finish();
        }
    };
}




Java Source Code List

com.bignerdranch.android.criminalintent.CrimeCameraActivity.java
com.bignerdranch.android.criminalintent.CrimeCameraFragment.java
com.bignerdranch.android.criminalintent.CrimeFragment.java
com.bignerdranch.android.criminalintent.CrimeLab.java
com.bignerdranch.android.criminalintent.CrimeListActivity.java
com.bignerdranch.android.criminalintent.CrimeListFragment.java
com.bignerdranch.android.criminalintent.CrimePagerActivity.java
com.bignerdranch.android.criminalintent.Crime.java
com.bignerdranch.android.criminalintent.CriminalIntentJSONSerializer.java
com.bignerdranch.android.criminalintent.DatePickerFragment.java
com.bignerdranch.android.criminalintent.ImageFragment.java
com.bignerdranch.android.criminalintent.Photo.java
com.bignerdranch.android.criminalintent.PictureUtils.java
com.bignerdranch.android.criminalintent.SingleFragmentActivity.java
com.bignerdranch.android.criminalintent.dummy.DummyContent.java