mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-08 21:30:57 +00:00
# use focus of Camera
# play sound on shutter
This commit is contained in:
1 parent
b34f0df298
commit
86007bda44
1 file changed
+38
-17
+38
-17
@@ -4,44 +4,58 @@ import java.io.IOException;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Camera;
|
||||
import android.hardware.Camera.AutoFocusCallback;
|
||||
import android.hardware.Camera.PictureCallback;
|
||||
import android.hardware.Camera.ShutterCallback;
|
||||
import android.media.AudioManager;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
||||
private static final String TAG = "CameraPreview";
|
||||
|
||||
|
||||
private SurfaceHolder surfaceHolder;
|
||||
private Camera camera;
|
||||
private AudioManager audioManager;
|
||||
|
||||
public CameraView(Context context) {
|
||||
super(context);
|
||||
|
||||
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
||||
|
||||
// Install a SurfaceHolder.Callback so we get notified when the
|
||||
// underlying surface is created and destroyed.
|
||||
surfaceHolder = getHolder();
|
||||
surfaceHolder.addCallback(this);
|
||||
// deprecated setting, but required on Android versions prior to 3.0
|
||||
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
||||
}
|
||||
|
||||
public void snap(final PictureCallback pictureHandler) {
|
||||
camera.takePicture(null, null, new PictureCallback() {
|
||||
|
||||
camera.autoFocus(new AutoFocusCallback() {
|
||||
@Override
|
||||
public void onPictureTaken(byte[] data, Camera camera) {
|
||||
pictureHandler.onPictureTaken(data, camera);
|
||||
camera.startPreview();
|
||||
public void onAutoFocus(boolean success, Camera camera) {
|
||||
if (success) {
|
||||
camera.takePicture(shutterSoundCallback, null, new PictureCallback() {
|
||||
@Override
|
||||
public void onPictureTaken(byte[] data, Camera camera) {
|
||||
pictureHandler.onPictureTaken(data, camera);
|
||||
camera.startPreview();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
audioManager.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setCamera(Camera newCamera) {
|
||||
camera = newCamera;
|
||||
if (camera == null) {
|
||||
if (camera == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
camera.setPreviewDisplay(surfaceHolder);
|
||||
requestLayout();
|
||||
@@ -50,7 +64,7 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
||||
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
// The Surface has been created, acquire the camera and tell it where
|
||||
@@ -63,7 +77,7 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
||||
Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
// Surface will be destroyed when we return, so stop the preview.
|
||||
@@ -71,22 +85,22 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
||||
camera.stopPreview();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
|
||||
// If your preview can change or rotate, take care of those events here.
|
||||
// Make sure to stop the preview before resizing or reformatting it.
|
||||
|
||||
if (surfaceHolder.getSurface() == null){
|
||||
// preview surface does not exist
|
||||
return;
|
||||
if (surfaceHolder.getSurface() == null) {
|
||||
// preview surface does not exist
|
||||
return;
|
||||
}
|
||||
|
||||
// stop preview before making changes
|
||||
try {
|
||||
camera.stopPreview();
|
||||
} catch (Exception e){
|
||||
// ignore: tried to stop a non-existent preview
|
||||
} catch (Exception e) {
|
||||
// ignore: tried to stop a non-existent preview
|
||||
}
|
||||
|
||||
// set preview size and make any resize, rotate or
|
||||
@@ -96,8 +110,15 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
|
||||
try {
|
||||
camera.setPreviewDisplay(surfaceHolder);
|
||||
camera.startPreview();
|
||||
} catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private ShutterCallback shutterSoundCallback = new ShutterCallback() {
|
||||
@Override
|
||||
public void onShutter() {
|
||||
audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user