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
|
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import android.app.Activity;
import android.content.ContentValues;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class CapturePhoto extends Activity implements SurfaceHolder.Callback, PictureCallback, ShutterCallback {
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Camera camera;
private final String TAG = "Svetlin Surface Renderer";
private Canvas canvas;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
surfaceView = (SurfaceView)findViewById(R.id.surface_view);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button prendrePhoto = (Button) findViewById(R.id.btn_prendrePhoto);
prendrePhoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
camera.takePicture(CapturePhoto.this, null, CapturePhoto.this);
}
});
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (camera != null) {
camera.stopPreview();
Camera.Parameters p = this.camera.getParameters();
p.setPreviewSize(width, height);
this.camera.setParameters(p);
try {
this.camera.setPreviewDisplay(holder);
this.camera.startPreview();
tryDrawing(holder);
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Erreur E/S lors du setPreviewDisplay sur l'objet Camera", e);
}
// TEST
{
tryDrawing(holder);
}
}
}
private void tryDrawing(SurfaceHolder holder) {
Log.i(TAG, "Trying to draw...");
canvas =holder.lockCanvas();
if (canvas == null) {
Log.e(TAG, "Cannot draw onto the canvas as it's null");
} else {
drawMyStuff(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
private void drawMyStuff(final Canvas canvas) {
Log.i(TAG, "Drawing...");
// canvas.drawRGB(255, 128, 128);
// Rectangle
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(10);
float leftx = 20;
float topy = 20;
float rightx = 200;
float bottomy = 200;
canvas.drawRect(leftx, topy, rightx, bottomy, paint);
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
camera.stopPreview();
camera.release();
}
}
public void onPictureTaken(byte[] data, Camera arg1) {
ContentValues values = new ContentValues();
values.put(Media.TITLE, "Mon image");
values.put(Media.DESCRIPTION, "Image prise par le téléphone");
Uri uri = getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream outputStream;
try {
outputStream = getContentResolver().openOutputStream(uri);
outputStream.write(data);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
Log.e(getClass().getSimpleName(), "Fichier non trouvé à l'écriture de l'image", e);
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Erreur E/S a l'enregistrement de l'image", e);
}
camera.startPreview();
}
public void onShutter() {
Log.d(getClass().getSimpleName(), "Clic clac ! ");
}
} |
Partager