Example usage for android.util Range Range

List of usage examples for android.util Range Range

Introduction

In this page you can find the example usage for android.util Range Range.

Prototype

public Range(final T lower, final T upper) 

Source Link

Document

Create a new immutable range.

Usage

From source file:freed.cam.apis.camera2.modules.VideoModuleApi2.java

@TargetApi(VERSION_CODES.LOLLIPOP)
private void startPreviewVideo() {
    recordingFile = new File(cameraUiWrapper.getActivityInterface().getStorageHandler()
            .getNewFilePath(appSettingsManager.GetWriteExternal(), ".mp4"));
    mediaRecorder = new MediaRecorder();
    mediaRecorder.reset();/*from w w  w.jav a2 s . c  o m*/
    mediaRecorder.setMaxFileSize(3037822976L); //~2.8 gigabyte
    mediaRecorder.setMaxDuration(7200000); //2hours
    mediaRecorder.setOnErrorListener(new OnErrorListener() {
        @Override
        public void onError(MediaRecorder mr, int what, int extra) {
            Log.d(TAG, "error MediaRecorder:" + what + "extra:" + extra);
            cameraUiWrapper.GetModuleHandler()
                    .onRecorderstateChanged(I_RecorderStateChanged.STATUS_RECORDING_STOP);
            changeCaptureState(ModuleHandlerAbstract.CaptureStates.video_recording_stop);
        }
    });

    mediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
        @Override
        public void onInfo(MediaRecorder mr, int what, int extra) {
            if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                recordnextFile(mr);
            } else if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
                recordnextFile(mr);
            }
        }
    });

    if (cameraUiWrapper.GetAppSettingsManager().getApiString(AppSettingsManager.SETTING_LOCATION)
            .equals(KEYS.ON)) {
        Location location = cameraUiWrapper.getActivityInterface().getLocationHandler().getCurrentLocation();
        if (location != null)
            mediaRecorder.setLocation((float) location.getLatitude(), (float) location.getLongitude());
    }
    switch (currentVideoProfile.Mode) {

    case Normal:
    case Highspeed:
        if (currentVideoProfile.isAudioActive)
            mediaRecorder.setAudioSource(AudioSource.CAMCORDER);
        break;
    case Timelapse:
        break;
    }
    mediaRecorder.setVideoSource(VideoSource.SURFACE);

    mediaRecorder.setOutputFormat(OutputFormat.MPEG_4);
    setRecorderFilePath();

    mediaRecorder.setVideoEncodingBitRate(currentVideoProfile.videoBitRate);

    try {
        cameraHolder.SetParameterRepeating(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE,
                new Range<>(currentVideoProfile.videoFrameRate, currentVideoProfile.videoFrameRate));
    } catch (Exception e) {
        e.printStackTrace();
    }

    //  if(currentVideoProfile.Mode == VideoMediaProfile.VideoMode.SlowMO)
    //     int SlowFactor = currentVideoProfile.videoFrameRate /30;

    if (currentVideoProfile.videoFrameRate == 120 && currentVideoProfile.videoFrameWidth == 1920)
        mediaRecorder.setVideoFrameRate(60);
    else
        mediaRecorder.setVideoFrameRate(currentVideoProfile.videoFrameRate);

    /*setCaptureRate
            
    Added in API level 11
    void setCaptureRate (double fps)
    Set video frame capture rate. This can be used to set a different video frame capture rate than the recorded video's playback rate.
    !!!!!! This method also sets the recording mode to time lapse.!!!!!
    In time lapse video recording, only video is recorded.
    Audio related parameters are ignored when a time lapse recording session starts, if an application sets them.*/
    //mediaRecorder.setCaptureRate((double)currentVideoProfile.videoFrameRate);
    mediaRecorder.setVideoSize(currentVideoProfile.videoFrameWidth, currentVideoProfile.videoFrameHeight);
    try {
        mediaRecorder.setVideoEncoder(currentVideoProfile.videoCodec);
    } catch (IllegalArgumentException ex) {
        mediaRecorder.reset();
        cameraUiWrapper.GetCameraHolder().SendUIMessage("VideoCodec not Supported");
    }

    switch (currentVideoProfile.Mode) {
    case Normal:
    case Highspeed:
        if (currentVideoProfile.isAudioActive) {
            try {
                mediaRecorder.setAudioEncoder(currentVideoProfile.audioCodec);
            } catch (IllegalArgumentException ex) {
                mediaRecorder.reset();
                cameraUiWrapper.GetCameraHolder().SendUIMessage("AudioCodec not Supported");
            }
            mediaRecorder.setAudioChannels(currentVideoProfile.audioChannels);
            mediaRecorder.setAudioEncodingBitRate(currentVideoProfile.audioBitRate);
            mediaRecorder.setAudioSamplingRate(currentVideoProfile.audioSampleRate);
        }
        break;
    case Timelapse:
        float frame = 30;
        if (!appSettingsManager.getApiString(AppSettingsManager.TIMELAPSEFRAME).equals(""))
            frame = Float.parseFloat(
                    appSettingsManager.getApiString(AppSettingsManager.TIMELAPSEFRAME).replace(",", "."));
        else
            appSettingsManager.setApiString(AppSettingsManager.TIMELAPSEFRAME, "" + frame);
        mediaRecorder.setCaptureRate(frame);
        break;
    }
    try {
        mediaRecorder.prepare();
    } catch (IOException ex) {
        ex.printStackTrace();
        cameraUiWrapper.GetModuleHandler().onRecorderstateChanged(I_RecorderStateChanged.STATUS_RECORDING_STOP);
        changeCaptureState(ModuleHandlerAbstract.CaptureStates.video_recording_stop);
        return;
    }
    recorderSurface = mediaRecorder.getSurface();
    cameraHolder.CaptureSessionH.AddSurface(recorderSurface, true);

    if (currentVideoProfile.Mode != VideoMediaProfile.VideoMode.Highspeed)
        cameraHolder.CaptureSessionH.CreateCaptureSession(previewrdy);
    else
        cameraHolder.CaptureSessionH.CreateHighSpeedCaptureSession(previewrdy);
}

From source file:com.android.camera2.its.ItsSerializer.java

@SuppressWarnings("unchecked")
public static CaptureRequest.Builder deserialize(CaptureRequest.Builder mdDefault, JSONObject jsonReq)
        throws ItsException {
    try {//from  ww w  .  ja  va  2s . c om
        Logt.i(TAG, "Parsing JSON capture request ...");

        // Iterate over the CaptureRequest reflected fields.
        CaptureRequest.Builder md = mdDefault;
        Field[] allFields = CaptureRequest.class.getDeclaredFields();
        for (Field field : allFields) {
            if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())
                    && field.getType() == CaptureRequest.Key.class
                    && field.getGenericType() instanceof ParameterizedType) {
                ParameterizedType paramType = (ParameterizedType) field.getGenericType();
                Type[] argTypes = paramType.getActualTypeArguments();
                if (argTypes.length > 0) {
                    CaptureRequest.Key key = (CaptureRequest.Key) field.get(md);
                    String keyName = key.getName();
                    Type keyType = argTypes[0];

                    // For each reflected CaptureRequest entry, look inside the JSON object
                    // to see if it is being set. If it is found, remove the key from the
                    // JSON object. After this process, there should be no keys left in the
                    // JSON (otherwise an invalid key was specified).

                    if (jsonReq.has(keyName) && !jsonReq.isNull(keyName)) {
                        if (keyType instanceof GenericArrayType) {
                            Type elmtType = ((GenericArrayType) keyType).getGenericComponentType();
                            JSONArray ja = jsonReq.getJSONArray(keyName);
                            Object val[] = new Object[ja.length()];
                            for (int i = 0; i < ja.length(); i++) {
                                if (elmtType == int.class) {
                                    Array.set(val, i, ja.getInt(i));
                                } else if (elmtType == byte.class) {
                                    Array.set(val, i, (byte) ja.getInt(i));
                                } else if (elmtType == float.class) {
                                    Array.set(val, i, (float) ja.getDouble(i));
                                } else if (elmtType == long.class) {
                                    Array.set(val, i, ja.getLong(i));
                                } else if (elmtType == double.class) {
                                    Array.set(val, i, ja.getDouble(i));
                                } else if (elmtType == boolean.class) {
                                    Array.set(val, i, ja.getBoolean(i));
                                } else if (elmtType == String.class) {
                                    Array.set(val, i, ja.getString(i));
                                } else if (elmtType == Size.class) {
                                    JSONObject obj = ja.getJSONObject(i);
                                    Array.set(val, i, new Size(obj.getInt("width"), obj.getInt("height")));
                                } else if (elmtType == Rect.class) {
                                    JSONObject obj = ja.getJSONObject(i);
                                    Array.set(val, i, new Rect(obj.getInt("left"), obj.getInt("top"),
                                            obj.getInt("bottom"), obj.getInt("right")));
                                } else if (elmtType == Rational.class) {
                                    JSONObject obj = ja.getJSONObject(i);
                                    Array.set(val, i,
                                            new Rational(obj.getInt("numerator"), obj.getInt("denominator")));
                                } else if (elmtType == RggbChannelVector.class) {
                                    JSONArray arr = ja.getJSONArray(i);
                                    Array.set(val, i,
                                            new RggbChannelVector((float) arr.getDouble(0),
                                                    (float) arr.getDouble(1), (float) arr.getDouble(2),
                                                    (float) arr.getDouble(3)));
                                } else if (elmtType == ColorSpaceTransform.class) {
                                    JSONArray arr = ja.getJSONArray(i);
                                    Rational xform[] = new Rational[9];
                                    for (int j = 0; j < 9; j++) {
                                        xform[j] = new Rational(arr.getJSONObject(j).getInt("numerator"),
                                                arr.getJSONObject(j).getInt("denominator"));
                                    }
                                    Array.set(val, i, new ColorSpaceTransform(xform));
                                } else if (elmtType == MeteringRectangle.class) {
                                    JSONObject obj = ja.getJSONObject(i);
                                    Array.set(val, i, new MeteringRectangle(obj.getInt("x"), obj.getInt("y"),
                                            obj.getInt("width"), obj.getInt("height"), obj.getInt("weight")));
                                } else {
                                    throw new ItsException("Failed to parse key from JSON: " + keyName);
                                }
                            }
                            if (val != null) {
                                Logt.i(TAG, "Set: " + keyName + " -> " + Arrays.toString(val));
                                md.set(key, val);
                                jsonReq.remove(keyName);
                            }
                        } else {
                            Object val = null;
                            if (keyType == Integer.class) {
                                val = jsonReq.getInt(keyName);
                            } else if (keyType == Byte.class) {
                                val = (byte) jsonReq.getInt(keyName);
                            } else if (keyType == Double.class) {
                                val = jsonReq.getDouble(keyName);
                            } else if (keyType == Long.class) {
                                val = jsonReq.getLong(keyName);
                            } else if (keyType == Float.class) {
                                val = (float) jsonReq.getDouble(keyName);
                            } else if (keyType == Boolean.class) {
                                val = jsonReq.getBoolean(keyName);
                            } else if (keyType == String.class) {
                                val = jsonReq.getString(keyName);
                            } else if (keyType == Size.class) {
                                JSONObject obj = jsonReq.getJSONObject(keyName);
                                val = new Size(obj.getInt("width"), obj.getInt("height"));
                            } else if (keyType == Rect.class) {
                                JSONObject obj = jsonReq.getJSONObject(keyName);
                                val = new Rect(obj.getInt("left"), obj.getInt("top"), obj.getInt("right"),
                                        obj.getInt("bottom"));
                            } else if (keyType == Rational.class) {
                                JSONObject obj = jsonReq.getJSONObject(keyName);
                                val = new Rational(obj.getInt("numerator"), obj.getInt("denominator"));
                            } else if (keyType == RggbChannelVector.class) {
                                JSONObject obj = jsonReq.optJSONObject(keyName);
                                JSONArray arr = jsonReq.optJSONArray(keyName);
                                if (arr != null) {
                                    val = new RggbChannelVector((float) arr.getDouble(0),
                                            (float) arr.getDouble(1), (float) arr.getDouble(2),
                                            (float) arr.getDouble(3));
                                } else if (obj != null) {
                                    val = new RggbChannelVector((float) obj.getDouble("red"),
                                            (float) obj.getDouble("greenEven"),
                                            (float) obj.getDouble("greenOdd"), (float) obj.getDouble("blue"));
                                } else {
                                    throw new ItsException("Invalid RggbChannelVector object");
                                }
                            } else if (keyType == ColorSpaceTransform.class) {
                                JSONArray arr = jsonReq.getJSONArray(keyName);
                                Rational a[] = new Rational[9];
                                for (int i = 0; i < 9; i++) {
                                    a[i] = new Rational(arr.getJSONObject(i).getInt("numerator"),
                                            arr.getJSONObject(i).getInt("denominator"));
                                }
                                val = new ColorSpaceTransform(a);
                            } else if (keyType instanceof ParameterizedType
                                    && ((ParameterizedType) keyType).getRawType() == Range.class
                                    && ((ParameterizedType) keyType).getActualTypeArguments().length == 1
                                    && ((ParameterizedType) keyType)
                                            .getActualTypeArguments()[0] == Integer.class) {
                                JSONArray arr = jsonReq.getJSONArray(keyName);
                                val = new Range<Integer>(arr.getInt(0), arr.getInt(1));
                            } else {
                                throw new ItsException(
                                        "Failed to parse key from JSON: " + keyName + ", " + keyType);
                            }
                            if (val != null) {
                                Logt.i(TAG, "Set: " + keyName + " -> " + val);
                                md.set(key, val);
                                jsonReq.remove(keyName);
                            }
                        }
                    }
                }
            }
        }

        // Ensure that there were no invalid keys in the JSON request object.
        if (jsonReq.length() != 0) {
            throw new ItsException("Invalid JSON key(s): " + jsonReq.toString());
        }

        Logt.i(TAG, "Parsing JSON capture request completed");
        return md;
    } catch (java.lang.IllegalAccessException e) {
        throw new ItsException("Access error: ", e);
    } catch (org.json.JSONException e) {
        throw new ItsException("JSON error: ", e);
    }
}