Android Open Source - pic-taker Camera Fragment






From Project

Back to project page pic-taker.

License

The source code is released under:

MIT License

If you think the Android project pic-taker 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.sambuo.pictaker;
//from   ww w  .j  a  v  a 2  s. c o m
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.SaveCallback;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.os.Bundle;
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.view.SurfaceHolder.Callback;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class CameraFragment extends Fragment {

  public static final String TAG = "CameraFragment";

  private Camera camera;
  private SurfaceView surfaceView;
  private ParseFile photoFile;
  private ImageButton photoButton;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_camera, parent, false);

    photoButton = (ImageButton) v.findViewById(R.id.camera_photo_button);

    if (camera == null) {
      try {
        //TODO: Open in another thread
        camera = Camera.open();
        photoButton.setEnabled(true);
      } catch (Exception e) {
        Log.e(TAG, "No camera with exception: " + e.getMessage());
        photoButton.setEnabled(false);
        Toast.makeText(getActivity(), "No camera detected", Toast.LENGTH_LONG).show();
      }
    }

    photoButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        if (camera == null)
          return;
        camera.takePicture(new Camera.ShutterCallback() {

          @Override
          public void onShutter() {
            // nothing to do
          }

        }, null, new Camera.PictureCallback() {

          @Override
          public void onPictureTaken(byte[] data, Camera camera) {
            saveScaledPhoto(data);
          }

        });

      }
    });

    surfaceView = (SurfaceView) v.findViewById(R.id.camera_surface_view);
    SurfaceHolder holder = surfaceView.getHolder();
    holder.addCallback(new Callback() {

      public void surfaceCreated(SurfaceHolder holder) {
        try {
          if (camera != null) {
            camera.setDisplayOrientation(90);
            camera.setPreviewDisplay(holder);
            camera.startPreview();
          }
        } catch (IOException e) {
          Log.e(TAG, "Error setting up preview", e);
        }
      }

      public void surfaceChanged(SurfaceHolder holder, int format,
          int width, int height) {
        // nothing to do here
      }

      public void surfaceDestroyed(SurfaceHolder holder) {
        // nothing here
      }

    });

    return v;
  }

  /*
   * ParseQueryAdapter loads ParseFiles into a ParseImageView at whatever size
   * they are saved. Since we never need a full-size image in our app, we'll
   * save a scaled one right away.
   */
  private void saveScaledPhoto(byte[] data) {

    // Resize photo from camera byte array
    Bitmap mealImage = BitmapFactory.decodeByteArray(data, 0, data.length);
    Bitmap mealImageScaled = Bitmap.createScaledBitmap(mealImage, 200, 200
        * mealImage.getHeight() / mealImage.getWidth(), false);

    // Override Android default landscape orientation and save portrait
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Bitmap rotatedScaledMealImage = Bitmap.createBitmap(mealImageScaled, 0,
        0, mealImageScaled.getWidth(), mealImageScaled.getHeight(),
        matrix, true);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    rotatedScaledMealImage.compress(Bitmap.CompressFormat.JPEG, 100, bos);

    byte[] scaledData = bos.toByteArray();

    // Save the scaled image to Parse
    photoFile = new ParseFile("user_photo.jpg", scaledData);
    photoFile.saveInBackground(new SaveCallback() {

      public void done(ParseException e) {
        if (e != null) {
          Toast.makeText(getActivity(), "Error saving: " + e.getMessage(), Toast.LENGTH_LONG).show();
        } else {
          savePhotoAndReturn(photoFile);
        }
      }
    });
  }

  private void savePhotoAndReturn(ParseFile photoFile) {
    UserPicture userPicture = new UserPicture();
    userPicture.setPhotoFile(photoFile);
    userPicture.saveInBackground(new SaveCallback() {

      @Override
      public void done(ParseException e) {
        if (e == null) {
          FragmentManager fm = getActivity().getFragmentManager();
          fm.popBackStack("CameraFragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
        } else {
          Toast.makeText(getActivity().getApplicationContext(), "Error saving: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
      }

    });
  }

  @Override
  public void onResume() {
    super.onResume();
    if (camera == null) {
      try {
        camera = Camera.open();
        photoButton.setEnabled(true);
      } catch (Exception e) {
        Log.i(TAG, "No camera: " + e.getMessage());
        photoButton.setEnabled(false);
        Toast.makeText(getActivity(), "No camera detected", Toast.LENGTH_LONG).show();
      }
    }
  }

  @Override
  public void onPause() {
    if (camera != null) {
      camera.stopPreview();
      camera.release();
    }
    super.onPause();
  }

}




Java Source Code List

com.sambuo.pictaker.CameraFragment.java
com.sambuo.pictaker.CameraPreview.java
com.sambuo.pictaker.HomeFragment.java
com.sambuo.pictaker.ImageAdapter.java
com.sambuo.pictaker.MainActivity.java
com.sambuo.pictaker.UserPicture.java
com.sambuo.pictaker.ViewPictureActivity.java