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
| package com.aceart.FormationCamera;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.app.Activity;
import android.content.ContentValues;
import android.hardware.Camera;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Video.Media;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
public abstract class FormationCameraActivity extends Activity implements SurfaceHolder.Callback{
private Camera camera;
private SurfaceView surfaceCamera;
private Boolean isPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
isPreview = false;
setContentView(R.layout.main);
surfaceCamera = (SurfaceView) findViewById(R.id.surfaceViewCamera);
InitializeCamera();
Camera.PictureCallback pictureCallback = new Camera.PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera){
if(data != null){
try{ //ERREUR ICI SUR STREAM
if(stream != null){
stream.write(data);
stream.flush();
stream.close();
}
}catch (Exception e){}
camera.startPreview();
}
}
};
//ERREUR SUR ONCLICKLISTENER ET SURFACE
surface.Camera.setOnClickListener(new OnClickListener() {
public void onClick(View v){if(camera != null){SavePicture();}}});
}
private void InitializeCamera() {
surfaceCamera.getHolder().addCallback(this);
surfaceCamera.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceDestroyed(SurfaceHolder holder){
if(camera != null){
camera.stopPreview();
isPreview = false;
camera.release();
}
}
public void surfaceCreated(SurfaceHolder holder){
if(camera == null)
camera = Camera.open();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
if(isPreview){camera.stopPreview();}
Camera.Parameters parameters = camera.getParameters();
try{camera.setPreviewDisplay(surfaceCamera.getHolder());}catch (IOException e){}
camera.startPreview();
isPreview = true;
}
@Override
public void onResume(){
super.onResume();
camera = Camera.open();
}
@Override
public void onPause(){
super.onPause();
if(camera != null){
camera.release();
camera = null;
}
}
private void SavePicture(){
try{
SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
String fileName = "photo_" + timeStampFormat.format(new Date(0)) + ".jpg";
ContentValues values = new ContentValues();
values.put(Media.TITLE, fileName);
values.put(Media.DISPLAY_NAME, fileName);
values.put(Media.DESCRIPTION, "Image prise par FormationCamera");
values.put(Media.DATE_TAKEN, new Date(0).getTime());
values.put(Media.MIME_TYPE, "image/jpeg");
Uri taken = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
//ERREUR STREAM
stream = (FileOutputStream) getContentResolver().openOutputStream(taken);
//ERREUR PICTURECALLBACK
camera.takePicture(null, pictureCallback, pictureCallback);
} catch(Exception e){
}
}
} |
Partager