Android Open Source - My-Team--Your-Team Equipe Picture Callback






From Project

Back to project page My-Team--Your-Team.

License

The source code is released under:

MIT License

If you think the Android project My-Team--Your-Team 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.selesse.apps;
// ww  w .  ja v a2 s . c  o m
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.widget.Toast;

import com.selesse.apps.activity.CameraActivity;
import com.selesse.apps.model.SimpleCamera;
import com.selesse.apps.model.Team;

public class EquipePictureCallback implements PictureCallback {

  // team can be mine or yours
  private Team team;
  private Bitmap caption;
  private String path;

  public EquipePictureCallback(Team team, Bitmap caption) {
    this.team = team;
    this.caption = caption;
  }

  @TargetApi(9)
  @Override
  public void onPictureTaken(byte[] data, Camera camera) {

    int rotateDegrees = 90;

    List<SimpleCamera> cameraList = CameraActivity.getCameraList();

    for (SimpleCamera cam : cameraList) {
      if (cam.getFacing() == SimpleCamera.FACING.CAMERA_FACING_BACK) {
        rotateDegrees = 90;
        break;
      }
      if (cam.getFacing() == SimpleCamera.FACING.CAMERA_FACING_FRONT) {
        rotateDegrees = 270;
      }
    }

    FileOutputStream outStream = null;
    try {
      // decode the byte[] data into a bitmap
      Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, data.length);
      Matrix mat = new Matrix();

      // we do a different type of rotation depending on the camera type
      if (rotateDegrees == 270) {
        float[] mirrorX = { 1, 0, 0, 0, -1, 0, 0, 0, 1 };
        mat.setValues(mirrorX);
      }
      mat.postRotate(rotateDegrees);
      Bitmap pictureTaken = null;
      try {
        pictureTaken = Bitmap
            .createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), mat, true);
      }
      catch (OutOfMemoryError memErr) {
        CameraActivity.popup(R.string.out_of_memory_warning);
      }

      // if we've failed to take a picture, abort
      if (pictureTaken == null) {
          CameraActivity.popup(R.string.out_of_memory_warning);
          return;
      }

      // scale the caption to be appropriate size
      caption = Bitmap.createScaledBitmap(caption, (pictureTaken.getWidth() * 3) / 4,
          pictureTaken.getHeight() / 4, false);
      // set up overlay
      Canvas canvas = new Canvas(pictureTaken);

      // the image appears in the center of the screen (horizontally)
      // vertically, it's at the bottom - (4/3) of the image's height,
      // i.e. 1/3 of the image's height from the bottom
      canvas.drawBitmap(getCaption(), canvas.getWidth() / 2 - getCaption().getWidth() / 2,
          canvas.getHeight() - ((4 * getCaption().getHeight()) / 3), null);

      long currentTime = System.currentTimeMillis();

      File dir = new File(CameraActivity.picFolder + team.getDirectory());
      if (!dir.exists()) {
        if (!dir.mkdirs()) {
          CameraActivity.popup(R.string.unable_to_make_directory);
        }
      }

      setPath(String
          .format(CameraActivity.picFolder + team.getDirectory() + "/%d.jpg", currentTime));

      File imagePath = new File(getPath());

      outStream = new FileOutputStream(imagePath);
      // do not compress!
      pictureTaken.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
      outStream.close();
    }
    catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      camera.stopPreview();
      camera.startPreview();
    }
  }

  public String getPath() {
    return path;
  }

  public void setPath(String path) {
    this.path = path;
  }

  public Bitmap getCaption() {
    return caption;
  }

  public void setCaption(Bitmap caption) {
    this.caption = caption;
  }

}




Java Source Code List

com.selesse.apps.EquipePictureCallback.java
com.selesse.apps.Preview.java
com.selesse.apps.activity.CameraActivity.java
com.selesse.apps.model.SimpleCamera.java
com.selesse.apps.model.Team.java