mirror of
				https://github.com/SimpleMobileTools/Simple-Camera.git
				synced 2025-06-27 09:02:59 +02:00 
			
		
		
		
	implement capturing images
This commit is contained in:
		| @@ -7,6 +7,7 @@ | |||||||
|         android:required="true"/> |         android:required="true"/> | ||||||
|  |  | ||||||
|     <uses-permission android:name="android.permission.CAMERA"/> |     <uses-permission android:name="android.permission.CAMERA"/> | ||||||
|  |     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | ||||||
|  |  | ||||||
|     <application |     <application | ||||||
|         android:allowBackup="true" |         android:allowBackup="true" | ||||||
|   | |||||||
| @@ -12,11 +12,13 @@ import android.widget.RelativeLayout; | |||||||
|  |  | ||||||
| import butterknife.Bind; | import butterknife.Bind; | ||||||
| import butterknife.ButterKnife; | import butterknife.ButterKnife; | ||||||
|  | import butterknife.OnClick; | ||||||
|  |  | ||||||
| public class MainActivity extends AppCompatActivity { | public class MainActivity extends AppCompatActivity { | ||||||
|     @Bind(R.id.viewHolder) RelativeLayout viewHolder; |     @Bind(R.id.viewHolder) RelativeLayout viewHolder; | ||||||
|  |  | ||||||
|     private static final String TAG = Preview.class.getSimpleName(); |     private static final String TAG = Preview.class.getSimpleName(); | ||||||
|  |     private static final int REQUEST_IMAGE_CAPTURE = 1; | ||||||
|     private Preview preview; |     private Preview preview; | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -33,6 +35,11 @@ public class MainActivity extends AppCompatActivity { | |||||||
|         viewHolder.addView(preview); |         viewHolder.addView(preview); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @OnClick(R.id.shutter) | ||||||
|  |     public void takePicture() { | ||||||
|  |         preview.takePicture(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     protected void onResume() { |     protected void onResume() { | ||||||
|         super.onResume(); |         super.onResume(); | ||||||
|   | |||||||
| @@ -1,30 +1,45 @@ | |||||||
| package com.simplemobiletools.camera; | package com.simplemobiletools.camera; | ||||||
|  |  | ||||||
| import android.content.Context; | import android.content.Context; | ||||||
|  | import android.graphics.Bitmap; | ||||||
|  | import android.graphics.BitmapFactory; | ||||||
|  | import android.graphics.Matrix; | ||||||
| import android.hardware.Camera; | import android.hardware.Camera; | ||||||
|  | import android.media.ExifInterface; | ||||||
|  | import android.media.MediaScannerConnection; | ||||||
|  | import android.os.Environment; | ||||||
| import android.util.Log; | import android.util.Log; | ||||||
| import android.view.SurfaceHolder; | import android.view.SurfaceHolder; | ||||||
| import android.view.SurfaceView; | import android.view.SurfaceView; | ||||||
| import android.view.ViewGroup; | import android.view.ViewGroup; | ||||||
|  |  | ||||||
|  | import java.io.File; | ||||||
|  | import java.io.FileNotFoundException; | ||||||
|  | import java.io.FileOutputStream; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.text.SimpleDateFormat; | ||||||
|  | import java.util.Date; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| public class Preview extends ViewGroup implements SurfaceHolder.Callback { | public class Preview extends ViewGroup implements SurfaceHolder.Callback { | ||||||
|     private static final String TAG = Preview.class.getSimpleName(); |     private static final String TAG = Preview.class.getSimpleName(); | ||||||
|  |     private static final int MEDIA_TYPE_IMAGE = 1; | ||||||
|  |  | ||||||
|     private SurfaceHolder surfaceHolder; |     private static Context context; | ||||||
|     private Camera camera; |     private static SurfaceHolder surfaceHolder; | ||||||
|     private List<Camera.Size> supportedPreviewSizes; |     private static Camera camera; | ||||||
|     private SurfaceView surfaceView; |     private static List<Camera.Size> supportedPreviewSizes; | ||||||
|     private Camera.Size previewSize; |     private static SurfaceView surfaceView; | ||||||
|  |     private static Camera.Size previewSize; | ||||||
|  |  | ||||||
|     public Preview(Context context) { |     public Preview(Context cxt) { | ||||||
|         super(context); |         super(cxt); | ||||||
|  |         context = cxt; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public Preview(Context context, SurfaceView sv) { |     public Preview(Context cxt, SurfaceView sv) { | ||||||
|         super(context); |         super(cxt); | ||||||
|  |         context = cxt; | ||||||
|  |  | ||||||
|         surfaceView = sv; |         surfaceView = sv; | ||||||
|         surfaceHolder = surfaceView.getHolder(); |         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() { |     public void releaseCamera() { | ||||||
|         if (camera != null) { |         if (camera != null) { | ||||||
|             camera.stopPreview(); |             camera.stopPreview(); | ||||||
|   | |||||||
| @@ -11,6 +11,7 @@ | |||||||
|         android:layout_height="wrap_content"/> |         android:layout_height="wrap_content"/> | ||||||
|  |  | ||||||
|     <ImageView |     <ImageView | ||||||
|  |         android:id="@+id/shutter" | ||||||
|         android:layout_width="wrap_content" |         android:layout_width="wrap_content" | ||||||
|         android:layout_height="wrap_content" |         android:layout_height="wrap_content" | ||||||
|         android:layout_alignParentRight="true" |         android:layout_alignParentRight="true" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user