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
|
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private String TAG;
private String DEBUG_TAG;
final int MEDIA_TYPE_IMAGE = 100;
private FileOutputStream stream;
@Override
public void onCreate(Bundle savedInstanceState) {
boolean bool_check_cam = false;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
bool_check_cam = checkCameraHardware(this);
if (bool_check_cam == true) {
// Create an instance of Camera
mCamera = this.getCameraInstance();
// Create our Preview view and set it as the content of our
// activity.
// setCameraDisplayOrientation(this, mCamera);
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SavePicture();
// mCamera.startPreview();
}
});
}
}
private void SavePicture() {
try {
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH.mm.ss");
String fileName = "photo_" + timeStampFormat.format(new Date())
+ ".jpg";
// Metadata pour la photo
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().getTime());
values.put(Media.MIME_TYPE, "image/jpeg");
// Support de stockage
Uri taken = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
// Ouverture du flux pour la sauvegarde
stream = (FileOutputStream) getContentResolver().openOutputStream(
taken);
mCamera.takePicture(null, pictureCallback, pictureCallback);
} catch (Exception e) {
// TODO: handle exception
}
}
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
private Bitmap bMapRotate;
public void onPictureTaken(byte[] data, Camera camera) {
if (data != null) {
// Enregistrement de votre image
try {
if (stream != null) {
stream.write(data);
stream.flush();
stream.close();
}
} catch (Exception e) {
// TODO: handle exception
}
// On redémarre la prévisualisation
camera.startPreview();
}
}
};
public static void setCameraDisplayOrientation(Activity activity,
int cameraId, android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
private Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(CameraInfo.CAMERA_FACING_BACK);
setCameraDisplayOrientation(this, c);
/*
* c.setDisplayOrientation(90); Parameters p= c.getParameters();
* p.set("orientation", "portrait"); c.setParameters(p);
*/
// mCamera.setDisplayOrientation(90);
} catch (Exception e) {
// Camera is not available (in use or does not exist)
Log.d(TAG, "Error camera: " + e.getMessage());
}
return c;
}
} |
Partager