1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
| package univ.rouen.spirit.activity;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import univ.rouen.spirit.activity.R;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore.Images.ImageColumns;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
/**
* The home user interface of the spirit application
* that displays a camera, if we want to take a picture
* from the spirit application and allows to browse a
* picture from the smartphone memory.
*
* @author Spirit Red Team
*
*/
public class CameraActivity extends Activity implements SurfaceHolder.Callback {
// FIELDS
/**
* The camera object that spirit application uses
* to take a picture.
*/
private Camera camera;
/**
* Indicates if or not the photo
* preview is running. The default
* value is false.
*/
private boolean isPreviewRunning = false;
/**
* A date format to include in the picture name.
*/
private SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
/**
* The surfaceView field defines the camera area
* in the smartphone interface.
*/
private SurfaceView surfaceView;
/**
* Allows you to control the SurfaceView settings
* and to monitor changes on it. That's why the
* CameraActivity class implements SurfaceHolder.CallBack
* interface in order to be notified about changes in the
* SurfaceView calling the <em>SurfaceHolder.CallBack</em>
* methods: surfaceChanged, surfaceCreated and surfaceDestroyed.
*/
private SurfaceHolder surfaceHolder;
/**
* The handler that held a auto focus
* request on the camera. This field is
* initialized by the requestAutoFocus method
* and read in the onAutoFocus method of the
* autoFocusCallBack field.
*/
private Handler mAutoFocusHandler;
/**
* Message sent by mAutoFocusHandler.
*/
private int mAutoFocusMessage;
/**
* Uri that identifies the file where
* picture is going to be saved.
*/
private Uri taken;
/**
* The OutputStream to the picture taken file.
*/
private OutputStream fileoutputStream;
//the bundle used to transmit data to the following activities, like the picture's path
private Bundle b;
private String imgPath;
/**
* The take picture button listener.
*/
private OnClickListener btListener = new OnClickListener() {
public void onClick(View v) {
takeThePicture();
}
};
/**
* The image browser listener.
*/
private OnClickListener imageBrowserListener = new OnClickListener() {
public void onClick(View v) {
b=new Bundle();
b.putString("FILE",imgPath);
Intent intent1 = new Intent(CameraActivity.this, ImageBrowserActivity.class);
intent1.putExtras(b);
startActivity(intent1);
}
};
/**
* The callback called when focus
* is requested.
*/
private final Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
if (mAutoFocusHandler != null) {
Message message = mAutoFocusHandler.obtainMessage(mAutoFocusMessage, success);
mAutoFocusHandler.sendMessageDelayed(message, 1500);
mAutoFocusHandler = null;
}
}
};
/**
* The callback called after the image
* is captured. It can be used to annotate
* the picture with the GPS coords.
*/
Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
// TODO
}
};
/**
* The callback called by the
* android.hardware.takePicture method when
* the byte data of the taken image is available.
* In this case it starts the picture preview.
*/
Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
camera.startPreview();
}
};
/**
* The callback called when the
* compressed image (JPEG) is available.
* In this case it saves the picture into
* the smartphone memory.
*/
Camera.PictureCallback mPictureCallbackJpeg = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
try {
Log.v(getClass().getSimpleName(), "onPictureTaken=" + data + " length = " + data.length);
fileoutputStream.write(data);
fileoutputStream.flush();
fileoutputStream.close();
Intent intent1 = new Intent(CameraActivity.this, CityNameActivity.class);
intent1.putExtra("univ.rouen.spirit.imageUri", taken.getPath());
startActivity(intent1);
} catch(Exception ex) {
//TODO
}
}
};
// EVENTS HANDLER METHODS
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Log.e(getClass().getSimpleName(), "onCreate");
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.camera_activity);
ImageButton b1 = (ImageButton) this.findViewById(R.id.takepicture);
b1.setOnClickListener(btListener);
surfaceView = (SurfaceView)findViewById(R.id.surface);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
ImageButton browse = (ImageButton) findViewById(R.id.browse);
browse.setOnClickListener(imageBrowserListener);
}
@Override
/**
* Switches from the CameraActivity to
* the LanguageChooserActivity
*/
public boolean onCreateOptionsMenu(Menu menu) {
Intent settingsActivity = new Intent(this, LanguageChooserActivity.class);
startActivity(settingsActivity);
return false;
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
/**
* Takes a picture pressing the center
* digital pad of the smartphone.
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
takeThePicture ();
return true;
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
return super.onKeyDown(keyCode, event);
}
return false;
}
@Override
protected void onResume() {
Log.e(getClass().getSimpleName(), "onResume");
super.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onStop() {
Log.e(getClass().getSimpleName(), "onStop");
super.onStop();
}
// TOOLS
/**
* Takes the picture and calls every callback
* saved in the camera object
*/
private void takeThePicture() {
try {
String filename = "spirit" + timeStampFormat.format(new Date());
imgPath = filename + ".jpg";
ContentValues values = new ContentValues();
values.put(Media.TITLE, imgPath);
values.put(Media.DISPLAY_NAME, imgPath);
values.put(Media.DESCRIPTION, "Image capture by camera for Spirit Application");
long dt = new Date().getTime();
values.put(ImageColumns.DATE_TAKEN, dt);
values.put(MediaColumns.MIME_TYPE, "image/jpeg");
taken = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
fileoutputStream = getContentResolver().openOutputStream(taken);
camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);
} catch(Exception ex ) {
ex.printStackTrace();
Log.e(getClass().getSimpleName(), ex.getMessage(), ex);
}
}
/**
* Request a focus
*
* @param handler the handler used to send the message
* @param message the message to be sent
*/
public void requestAutoFocus(Handler handler, int message) {
if (camera != null) {
mAutoFocusHandler = handler;
mAutoFocusMessage = message;
camera.autoFocus(autoFocusCallback);
}
}
/**
* Called when the SurfaceView is created.
* In this case it opens the camera.
*/
public void surfaceCreated(SurfaceHolder holder) {
Log.e(getClass().getSimpleName(), "surfaceCreated");
camera = Camera.open();
}
/**
* Called when the SurfaceView changes.
*/
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.e(getClass().getSimpleName(), "surfaceChanged");
if (isPreviewRunning) {
camera.stopPreview();
}
Camera.Parameters p = camera.getParameters();
p.setPreviewSize(w, h);
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
}
camera.startPreview();
isPreviewRunning = true;
}
/**
* Called when the surface is destroyed
*/
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(getClass().getSimpleName(), "surfaceDestroyed");
camera.stopPreview();
isPreviewRunning = false;
camera.release();
}
} |
Partager