diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 3971059b..50635591 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -7,6 +7,7 @@
android:required="true"/>
+
supportedPreviewSizes;
- private SurfaceView surfaceView;
- private Camera.Size previewSize;
+ private static Context context;
+ private static SurfaceHolder surfaceHolder;
+ private static Camera camera;
+ private static List supportedPreviewSizes;
+ private static SurfaceView surfaceView;
+ private static Camera.Size previewSize;
- public Preview(Context context) {
- super(context);
+ public Preview(Context cxt) {
+ super(cxt);
+ context = cxt;
}
- public Preview(Context context, SurfaceView sv) {
- super(context);
+ public Preview(Context cxt, SurfaceView sv) {
+ super(cxt);
+ context = cxt;
surfaceView = sv;
surfaceHolder = surfaceView.getHolder();
@@ -50,6 +65,71 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback {
}
}
+ public void takePicture() {
+ camera.takePicture(null, null, takePictureCallback);
+ }
+
+ private Camera.PictureCallback takePictureCallback = new Camera.PictureCallback() {
+ @Override
+ public void onPictureTaken(byte[] data, Camera camera) {
+ final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
+ if (pictureFile == null) {
+ return;
+ }
+
+ try {
+ final FileOutputStream fos = new FileOutputStream(pictureFile);
+ final ExifInterface exif = new ExifInterface(pictureFile.toString());
+ Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
+
+ if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")) {
+ realImage = rotateImage(realImage, 90);
+ } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")) {
+ realImage = rotateImage(realImage, 270);
+ } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")) {
+ realImage = rotateImage(realImage, 180);
+ } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")) {
+ realImage = rotateImage(realImage, 90);
+ }
+
+ realImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
+
+ fos.close();
+
+ final String[] picturePath = {pictureFile.getAbsolutePath()};
+ MediaScannerConnection.scanFile(context, picturePath, null, null);
+ } catch (FileNotFoundException e) {
+ Log.d(TAG, "onPictureTaken file not found: " + e.getMessage());
+ } catch (IOException e) {
+ Log.e(TAG, "onPictureTaken ioexception " + e.getMessage());
+ }
+ }
+ };
+
+ private static File getOutputMediaFile(int type) {
+ final String appName = context.getResources().getString(R.string.app_name);
+ final File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), appName);
+
+ if (!mediaStorageDir.exists()) {
+ if (!mediaStorageDir.mkdirs()) {
+ return null;
+ }
+ }
+
+ final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
+ if (type == MEDIA_TYPE_IMAGE) {
+ return new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
+ }
+
+ return null;
+ }
+
+ public static Bitmap rotateImage(Bitmap source, float angle) {
+ final Matrix matrix = new Matrix();
+ matrix.postRotate(angle);
+ return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
+ }
+
public void releaseCamera() {
if (camera != null) {
camera.stopPreview();
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 4734f3cd..19303894 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -11,6 +11,7 @@
android:layout_height="wrap_content"/>