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