Search in sources :

Example 1 with Callback

use of android.view.SurfaceHolder.Callback in project Osmand by osmandapp.

the class AudioVideoNotesPlugin method takePhotoWithCamera.

private void takePhotoWithCamera(final double lat, final double lon, final MapActivity mapActivity) {
    try {
        lastTakingPhoto = getBaseFileName(lat, lon, app, IMG_EXTENSION);
        final Camera.Size mPreviewSize;
        Parameters parameters = cam.getParameters();
        List<Camera.Size> psps = parameters.getSupportedPictureSizes();
        int camPicSizeIndex = AV_CAMERA_PICTURE_SIZE.get();
        // camera picture size
        log.debug("takePhotoWithCamera() index=" + camPicSizeIndex);
        if (camPicSizeIndex == AV_PHOTO_SIZE_DEFAULT) {
            camPicSizeIndex = cameraPictureSizeDefault;
            log.debug("takePhotoWithCamera() Default value of picture size. Set index to cameraPictureSizeDefault. Now index=" + camPicSizeIndex);
        }
        final Camera.Size selectedCamPicSize = psps.get(camPicSizeIndex);
        if (mSupportedPreviewSizes != null) {
            int width = selectedCamPicSize.width;
            int height = selectedCamPicSize.height;
            mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
        } else {
            mPreviewSize = null;
        }
        final SurfaceView view;
        if (mPreviewSize != null) {
            view = recordingMenu.prepareSurfaceView(mPreviewSize.width, mPreviewSize.height);
        } else {
            view = recordingMenu.prepareSurfaceView();
        }
        view.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        view.getHolder().addCallback(new Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    // load sound befor shot
                    if (AV_PHOTO_PLAY_SOUND.get()) {
                        if (sp == null)
                            sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
                        if (shotId == 0) {
                            try {
                                AssetFileDescriptor assetFileDescriptor = app.getAssets().openFd("sounds/camera_click.ogg");
                                shotId = sp.load(assetFileDescriptor, 1);
                                assetFileDescriptor.close();
                            } catch (Exception e) {
                                log.error("cannot get shotId for sounds/camera_click.ogg");
                            }
                        }
                    }
                    Parameters parameters = cam.getParameters();
                    parameters.setPictureSize(selectedCamPicSize.width, selectedCamPicSize.height);
                    log.debug("takePhotoWithCamera() set Picture size: width=" + selectedCamPicSize.width + " height=" + selectedCamPicSize.height);
                    // camera focus type
                    autofocus = true;
                    parameters.removeGpsData();
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                        parameters.setGpsLatitude(lat);
                        parameters.setGpsLongitude(lon);
                    }
                    switch(AV_CAMERA_FOCUS_TYPE.get()) {
                        case AV_CAMERA_FOCUS_HIPERFOCAL:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_FIXED);
                            autofocus = false;
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_FIXED");
                            break;
                        case AV_CAMERA_FOCUS_EDOF:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_EDOF);
                            autofocus = false;
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_EDOF");
                            break;
                        case AV_CAMERA_FOCUS_INFINITY:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_INFINITY);
                            autofocus = false;
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_INFINITY");
                            break;
                        case AV_CAMERA_FOCUS_MACRO:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_MACRO);
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_MACRO");
                            break;
                        case AV_CAMERA_FOCUS_CONTINUOUS:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_CONTINUOUS_PICTURE");
                            break;
                        default:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_AUTO");
                            break;
                    }
                    if (parameters.getSupportedWhiteBalance() != null && parameters.getSupportedWhiteBalance().contains(Parameters.WHITE_BALANCE_AUTO)) {
                        parameters.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
                    }
                    if (parameters.getSupportedFlashModes() != null && parameters.getSupportedFlashModes().contains(Parameters.FLASH_MODE_AUTO)) {
                    // parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
                    }
                    int cameraOrientation = getCamOrientation(mapActivity, Camera.CameraInfo.CAMERA_FACING_BACK);
                    cam.setDisplayOrientation(cameraOrientation);
                    parameters.set("rotation", cameraOrientation);
                    if (mPreviewSize != null) {
                        parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
                    }
                    cam.setParameters(parameters);
                    cam.setPreviewDisplay(holder);
                    cam.startPreview();
                    internalShoot();
                } catch (Exception e) {
                    logErr(e);
                    closeRecordingMenu();
                    closeCamera();
                    finishRecording();
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }
        });
        recordingMenu.show();
    } catch (RuntimeException e) {
        logErr(e);
        closeCamera();
    }
}
Also used : Parameters(android.hardware.Camera.Parameters) AssetFileDescriptor(android.content.res.AssetFileDescriptor) SoundPool(android.media.SoundPool) GeoParsedPoint(net.osmand.util.GeoPointParserUtil.GeoParsedPoint) IOException(java.io.IOException) SurfaceHolder(android.view.SurfaceHolder) PictureCallback(android.hardware.Camera.PictureCallback) Callback(android.view.SurfaceHolder.Callback) Camera(android.hardware.Camera) SurfaceView(android.view.SurfaceView)

Example 2 with Callback

use of android.view.SurfaceHolder.Callback in project libstreaming by fyhertz.

the class VideoStream method setSurfaceView.

/**
 * Sets a Surface to show a preview of recorded media (video).
 * You can call this method at any time and changes will take effect next time you call {@link #start()}.
 */
public synchronized void setSurfaceView(SurfaceView view) {
    mSurfaceView = view;
    if (mSurfaceHolderCallback != null && mSurfaceView != null && mSurfaceView.getHolder() != null) {
        mSurfaceView.getHolder().removeCallback(mSurfaceHolderCallback);
    }
    if (mSurfaceView != null && mSurfaceView.getHolder() != null) {
        mSurfaceHolderCallback = new Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mSurfaceReady = false;
                stopPreview();
                Log.d(TAG, "Surface destroyed !");
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mSurfaceReady = true;
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                Log.d(TAG, "Surface Changed !");
            }
        };
        mSurfaceView.getHolder().addCallback(mSurfaceHolderCallback);
        mSurfaceReady = true;
    }
}
Also used : SurfaceHolder(android.view.SurfaceHolder) Callback(android.view.SurfaceHolder.Callback) SuppressLint(android.annotation.SuppressLint)

Example 3 with Callback

use of android.view.SurfaceHolder.Callback in project Osmand by osmandapp.

the class AudioVideoNotesPlugin method recordVideoCamera.

public void recordVideoCamera(final double lat, final double lon, final MapActivity mapActivity) {
    final CamcorderProfile p = CamcorderProfile.get(AV_VIDEO_QUALITY.get());
    final Camera.Size mPreviewSize = getPreviewSize();
    final SurfaceView view;
    if (mPreviewSize != null) {
        view = recordingMenu.prepareSurfaceView(mPreviewSize.width, mPreviewSize.height);
    } else {
        view = recordingMenu.prepareSurfaceView();
    }
    view.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    view.getHolder().addCallback(new Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            MediaRecorder mr = new MediaRecorder();
            try {
                startCamera(mPreviewSize, holder);
                cam.unlock();
                mr.setCamera(cam);
            } catch (Exception e) {
                logErr(e);
                closeRecordingMenu();
                closeCamera();
                finishRecording();
                return;
            }
            final File f = getBaseFileName(lat, lon, app, MPEG4_EXTENSION);
            initMediaRecorder(mr, p, f);
            try {
                if (AV_RECORDER_SPLIT.get()) {
                    cleanupSpace();
                }
                runMediaRecorder(mapActivity, mr, f);
            } catch (Exception e) {
                logErr(e);
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }
    });
    recordingMenu.show();
}
Also used : SurfaceHolder(android.view.SurfaceHolder) PictureCallback(android.hardware.Camera.PictureCallback) Callback(android.view.SurfaceHolder.Callback) CamcorderProfile(android.media.CamcorderProfile) Camera(android.hardware.Camera) MediaRecorder(android.media.MediaRecorder) File(java.io.File) SurfaceView(android.view.SurfaceView) IOException(java.io.IOException) GeoParsedPoint(net.osmand.util.GeoPointParserUtil.GeoParsedPoint)

Aggregations

SurfaceHolder (android.view.SurfaceHolder)3 Callback (android.view.SurfaceHolder.Callback)3 Camera (android.hardware.Camera)2 PictureCallback (android.hardware.Camera.PictureCallback)2 SurfaceView (android.view.SurfaceView)2 IOException (java.io.IOException)2 GeoParsedPoint (net.osmand.util.GeoPointParserUtil.GeoParsedPoint)2 SuppressLint (android.annotation.SuppressLint)1 AssetFileDescriptor (android.content.res.AssetFileDescriptor)1 Parameters (android.hardware.Camera.Parameters)1 CamcorderProfile (android.media.CamcorderProfile)1 MediaRecorder (android.media.MediaRecorder)1 SoundPool (android.media.SoundPool)1 File (java.io.File)1