Example usage for android.media CamcorderProfile QUALITY_HIGH

List of usage examples for android.media CamcorderProfile QUALITY_HIGH

Introduction

In this page you can find the example usage for android.media CamcorderProfile QUALITY_HIGH.

Prototype

int QUALITY_HIGH

To view the source code for android.media CamcorderProfile QUALITY_HIGH.

Click Source Link

Document

Quality level corresponding to the highest available resolution.

Usage

From source file:Main.java

public static String convertOutputFormatToFileExt() {
    CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);

    if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.MPEG_4) {
        return ".mp4";
    }//from  w  w w . ja  va  2 s  .c o m
    return ".3gp";
}

From source file:com.example.android.mediarecorder.MainActivity.java

private boolean preparePreview() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        try {//  ww  w . j ava  2  s  . c  o m
            CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
            String id = manager.getCameraIdList()[0];
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);
            sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        } catch (CameraAccessException e) {
            Log.e("ERROR", "Cannot access Camera2 API");
            e.printStackTrace();
        }
    }

    Log.i(TAG, "Preparing Camera Preview");
    // BEGIN_INCLUDE (configure_preview)
    //        mCamera = CameraHelper.getDefaultCameraInstance();

    // We need to make sure that our preview and recording video size are supported by the
    // camera. Query camera to find all the sizes and choose the optimal size given the
    // dimensions of our preview surface.
    Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
    List<Camera.Size> mSupportedVideoSizes = parameters.getSupportedVideoSizes();
    Camera.Size optimalSize = CameraHelper.getOptimalVideoSize(mSupportedVideoSizes, mSupportedPreviewSizes,
            mPreview.getWidth(), mPreview.getHeight());

    // Use the same size for recording profile.
    CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    profile.videoFrameWidth = optimalSize.width;
    profile.videoFrameHeight = optimalSize.height;

    // likewise for the camera object itself.
    parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
    mCamera.setParameters(parameters);
    try {
        // Requires API level 11+, For backward compatibility use {@link setPreviewDisplay}
        // with {@link SurfaceView}
        //                mCamera.setPreviewTexture(mPreview.getSurfaceTexture());

        mHolder = mPreview.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //            mHolder.setKeepScreenOn(true);

        setCameraDisplayOrientation(0);
        mCamera.setPreviewDisplay(mPreview.getHolder());
        mCamera.startPreview();
        Log.i(TAG, "Setting Preview Display");
    } catch (IOException e) {
        Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
        return false;
    }
    // END_INCLUDE (configure_preview)
    return true;
}

From source file:com.example.android.mediarecorder.MainActivity.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private boolean prepareVideoRecorder() {
    if (!preparePreview()) {
        return false;
    }/*from ww w  . j av  a 2 s  .  c o  m*/
    CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);

    // BEGIN_INCLUDE (configure_media_recorder)
    mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    mMediaRecorder.setProfile(profile);

    // orientation
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    Log.i("TESTING", "Sensor Orientation: " + sensorOrientation);
    if (sensorOrientation == SENSOR_ORIENTATION_DEFAULT_DEGREES) {
        Log.i("TESTING", "Recorder: DEFAULT ROTATION: " + DEFAULT_ORIENTATIONS.get(rotation));
        mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
    } else {
        Log.i("TESTING", "Recorder: INVERSING ROTATION: " + INVERSE_ORIENTATIONS.get(rotation));
        mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
    }

    // Step 4: Set output file
    mOutputFile = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO);
    if (mOutputFile == null) {
        return false;
    }
    mMediaRecorder.setOutputFile(mOutputFile.getPath());
    // END_INCLUDE (configure_media_recorder)

    // Step 5: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}

From source file:com.almalence.plugins.capture.video.VideoCapturePlugin.java

private void prepareMediaRecorder() {
    mMediaRecorder = CameraController.getMediaRecorder();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ApplicationScreen.getMainContext());

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH && videoStabilization)
        CameraController.setVideoStabilization(true);

    CameraController.unlockCamera();/* w  w  w  .j av a 2  s.c  o m*/

    // Step 2: Set sources
    if (!CameraController.isUseCamera2()) {
        CameraController.configureMediaRecorder(mMediaRecorder);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    } else {
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    }

    if (ApplicationScreen.isMicrophonePermissionGranted())
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);

    int quality = Integer.parseInt(
            prefs.getString(CameraController.getCameraIndex() == 0 ? ApplicationScreen.sImageSizeVideoBackPref
                    : ApplicationScreen.sImageSizeVideoFrontPref, DEFAULT_VIDEO_QUALITY));

    if (maxQuality()) {
        quality = CamcorderProfile.QUALITY_HIGH;
    }

    boolean useProfile = true;
    if (!CamcorderProfile.hasProfile(CameraController.getCameraIndex(), quality))
        useProfile = false;

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    try {
        try {
            if (swChecked) {
                int qualityTimeLapse = quality;
                // if time lapse activated
                switch (quality) {
                case CamcorderProfile.QUALITY_QCIF:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_QCIF;
                    break;
                case CamcorderProfile.QUALITY_CIF:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_CIF;
                    break;
                case CamcorderProfile.QUALITY_2160P:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_2160P;
                    break;
                case CamcorderProfile.QUALITY_1080P:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_1080P;
                    break;
                case CamcorderProfile.QUALITY_720P:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_720P;
                    break;
                case CamcorderProfile.QUALITY_480P:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_480P;
                    break;
                case QUALITY_4K:
                    quality = QUALITY_4K;
                    break;
                case CamcorderProfile.QUALITY_HIGH:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_HIGH;
                    break;
                default:
                    break;
                }
                if (!CamcorderProfile.hasProfile(CameraController.getCameraIndex(), quality)) {
                    Toast.makeText(ApplicationScreen.instance, "Time lapse not supported", Toast.LENGTH_LONG)
                            .show();
                } else
                    quality = qualityTimeLapse;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Video", "Time lapse error catched" + e.getMessage());
            swChecked = false;
        }

        lastUseProfile = useProfile;
        if (useProfile) {
            CamcorderProfile pr = CamcorderProfile.get(CameraController.getCameraIndex(), quality);
            if (ApplicationScreen.isMicrophonePermissionGranted()) {
                mMediaRecorder.setProfile(pr);
            } else {
                // If we don't have access to microphone, then configure only video settings of MediaREcorder.
                mMediaRecorder.setOutputFormat(pr.fileFormat);
                mMediaRecorder.setVideoEncoder(pr.videoCodec);
                mMediaRecorder.setVideoSize(pr.videoFrameWidth, pr.videoFrameHeight);
                mMediaRecorder.setVideoFrameRate(pr.videoFrameRate);
                mMediaRecorder.setVideoEncodingBitRate(pr.videoBitRate);
            }
            lastCamcorderProfile = pr;
        } else {
            boolean useProf = false;
            lastUseProf = useProf;
            CameraController.Size sz = null;
            switch (quality) {
            case CamcorderProfile.QUALITY_QCIF:
                sz = new CameraController.Size(176, 144);
                break;
            case CamcorderProfile.QUALITY_CIF:
                sz = new CameraController.Size(352, 288);
                break;
            case CamcorderProfile.QUALITY_480P:
                sz = new CameraController.Size(640, 480);
                break;
            case CamcorderProfile.QUALITY_720P:
                sz = new CameraController.Size(1280, 720);
                break;
            case CamcorderProfile.QUALITY_1080P:
                sz = new CameraController.Size(1920, 1080);
                break;
            case CamcorderProfile.QUALITY_2160P: {
                if (CamcorderProfile.hasProfile(CameraController.getCameraIndex(),
                        CamcorderProfile.QUALITY_2160P))
                    sz = new CameraController.Size(3840, 2160);
                else {
                    CamcorderProfile prof = CamcorderProfile.get(CameraController.getCameraIndex(),
                            CamcorderProfile.QUALITY_HIGH);
                    prof.videoFrameWidth = 3840;
                    prof.videoFrameHeight = 2160;
                    prof.videoBitRate = (int) (prof.videoBitRate * 2.8); // need a higher bitrate for the better quality - this is roughly based on the bitrate used by an S5's native camera app at 4K (47.6 Mbps, compared to 16.9 Mbps which is what's returned by the QUALITY_HIGH profile)
                    if (ApplicationScreen.isMicrophonePermissionGranted()) {
                        mMediaRecorder.setProfile(prof);
                    } else {
                        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                        mMediaRecorder.setVideoSize(prof.videoFrameWidth, prof.videoFrameHeight);
                        mMediaRecorder.setVideoFrameRate(30);
                        mMediaRecorder.setVideoEncodingBitRate((int) (prof.videoBitRate * 2.8)); // need a higher bitrate for the better quality
                    }
                    lastCamcorderProfile = prof;
                    useProf = true;
                    lastUseProf = useProf;
                }

            }
                break;
            case QUALITY_4K: {
                if (CamcorderProfile.hasProfile(CameraController.getCameraIndex(),
                        CamcorderProfile.QUALITY_1080P)) {
                    CamcorderProfile prof = CamcorderProfile.get(CamcorderProfile.QUALITY_1080P);
                    prof.videoFrameHeight = 2160;
                    prof.videoFrameWidth = 4096;
                    if (ApplicationScreen.isMicrophonePermissionGranted()) {
                        mMediaRecorder.setProfile(prof);
                    } else {
                        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                        mMediaRecorder.setVideoSize(prof.videoFrameWidth, prof.videoFrameHeight);
                        mMediaRecorder.setVideoFrameRate(30);
                        mMediaRecorder.setVideoEncodingBitRate(prof.videoBitRate * 4); // 2160p has 4x more pixels then 1080p.
                    }
                    lastCamcorderProfile = prof;
                    useProf = true;
                    lastUseProf = useProf;
                } else
                    sz = new CameraController.Size(4096, 2160);
            }
                break;
            default:
                break;
            }

            if (!useProf) {
                mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                mMediaRecorder.setVideoSize(sz.getWidth(), sz.getHeight());
                mMediaRecorder.setVideoFrameRate(30);

                // Other parameters just copy from CamcorderProfile.QUALITY_1080P
                CamcorderProfile prof = CamcorderProfile.get(CamcorderProfile.QUALITY_1080P);
                mMediaRecorder.setVideoEncodingBitRate(prof.videoBitRate * 4); // 2160p has 4x more pixels then 1080p.

                if (ApplicationScreen.isMicrophonePermissionGranted()) {
                    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                    mMediaRecorder.setAudioChannels(prof.audioChannels);
                    mMediaRecorder.setAudioEncodingBitRate(prof.audioBitRate);
                    mMediaRecorder.setAudioSamplingRate(prof.audioSampleRate);
                }

                lastSz = sz;
            } else
                lastUseProfile = true;
        }

        if (swChecked) {
            double val1 = Double.valueOf(stringInterval[interval]);
            int val2 = measurementVal;
            switch (val2) {
            case 0:
                val2 = 1;
                break;
            case 1:
                val2 = 60;
                break;
            case 2:
                val2 = 3600;
                break;
            default:
                break;
            }
            captureRate = 1 / (val1 * val2);
            mMediaRecorder.setCaptureRate(captureRate);
        }
    } catch (Exception e) {
        e.printStackTrace();

        releaseMediaRecorder(); // release the MediaRecorder object
        return;
    }

    // Step 4: Set output file
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        DocumentFile file = getOutputMediaDocumentFile();
        try {
            documentFileSavedFd = ApplicationScreen.instance.getContentResolver()
                    .openFileDescriptor(file.getUri(), "w");
            FileDescriptor fileDescriptor = documentFileSavedFd.getFileDescriptor();
            mMediaRecorder.setOutputFile(fileDescriptor);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
        }
    } else {
        mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
    }

    // Step 5: Set the preview output
    if (!CameraController.isUseCamera2()) {
        mMediaRecorder.setPreviewDisplay(ApplicationScreen.getPreviewSurfaceHolder().getSurface());
    }

    mMediaRecorder.setOrientationHint(CameraController.isFrontCamera()
            ? (ApplicationScreen.getWantLandscapePhoto()
                    ? ApplicationScreen.getGUIManager().getImageDataOrientation()
                    : (ApplicationScreen.getGUIManager().getImageDataOrientation() + 180) % 360)
            : ApplicationScreen.getGUIManager().getImageDataOrientation());

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (Exception e) {
        Log.d("Video", "Exception preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();

        CameraController.lockCamera(); // take camera access back from MediaRecorder
        return;
    }
}