Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 摄像机2作为录像机,在android中不显示要录制的视图_Java_Android_Video_Video Capture_Android Camera2 - Fatal编程技术网

Java 摄像机2作为录像机,在android中不显示要录制的视图

Java 摄像机2作为录像机,在android中不显示要录制的视图,java,android,video,video-capture,android-camera2,Java,Android,Video,Video Capture,Android Camera2,我在Android应用程序中使用Camera 2作为我的自定义录像机。 我已经在我的应用程序中使用了这个。起初,我能够录制视频,但过了一段时间,录制视频的视图就不显示了。相反,它显示黑色。我不知道问题出在哪里。。。 请告诉我关于这个问题的任何建议或解决办法 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android

我在Android应用程序中使用Camera 2作为我的自定义录像机。 我已经在我的应用程序中使用了这个。起初,我能够录制视频,但过了一段时间,录制视频的视图就不显示了。相反,它显示黑色。我不知道问题出在哪里。。。 请告诉我关于这个问题的任何建议或解决办法

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="70dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp">
        <com.ex.repo.new_video_recording.AutoFitTextureView
            android:id="@+id/texture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />
    </LinearLayout>
</LinearLayout>

<RelativeLayout
    android:id="@+id/videoCameraId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:weightSum="2">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_weight="1"
        android:padding="8dp">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/video"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_centerInParent="true"
                android:src="@drawable/youtube"
                android:tint="@color/colorPrimaryDark" />
        </RelativeLayout>

    </LinearLayout>

    <!--  <LinearLayout
          android:id="@+id/linearlayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:padding="5dp"
          android:visibility="gone">

          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

              <Button
                  android:id="@+id/info"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:layout_gravity="end"
                  android:background="#FF30731C"
                  android:contentDescription="@string/description_info"
                  android:gravity="center_vertical"
                  android:padding="12dp"
                  android:src="@drawable/ic_action_play"
                  android:text="Upload"
                  android:textColor="#000"
                  android:visibility="visible" />
          </RelativeLayout>
      </LinearLayout>-->
</RelativeLayout>

下面是我的活动

private static final int SENSOR_ORIENTATION_DEFAULT_DEGREES = 90;
    private static final int SENSOR_ORIENTATION_INVERSE_DEGREES = 270;
    private static final SparseIntArray DEFAULT_ORIENTATIONS = new SparseIntArray();
    private static final SparseIntArray INVERSE_ORIENTATIONS = new SparseIntArray();

    private static final String TAG = "Camera2VideoFragment";

    static {
        DEFAULT_ORIENTATIONS.append(Surface.ROTATION_0, 90);
        DEFAULT_ORIENTATIONS.append(Surface.ROTATION_90, 0);
        DEFAULT_ORIENTATIONS.append(Surface.ROTATION_180, 270);
        DEFAULT_ORIENTATIONS.append(Surface.ROTATION_270, 180);
    }

    static {
        INVERSE_ORIENTATIONS.append(Surface.ROTATION_0, 270);
        INVERSE_ORIENTATIONS.append(Surface.ROTATION_90, 180);
        INVERSE_ORIENTATIONS.append(Surface.ROTATION_180, 90);
        INVERSE_ORIENTATIONS.append(Surface.ROTATION_270, 0);
    }

    /**
     * An {@link AutoFitTextureView} for camera preview.
     */
    private AutoFitTextureView mTextureView;

    /**
     * Button to record video
     */
    private ImageView mButtonVideo;

    private TextView uploadText;

    /**
     * A reference to the opened {@link CameraDevice}.
     */
    private CameraDevice mCameraDevice;

    /**
     * A reference to the current {@link CameraCaptureSession} for
     * preview.
     */
    private CameraCaptureSession mPreviewSession;

    /**
     * {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a
     * {@link TextureView}.
     */
    private TextureView.SurfaceTextureListener mSurfaceTextureListener
            = new TextureView.SurfaceTextureListener() {

        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
                                              int width, int height) {
            System.out.println("====surfacetexture available=====");
            openCamera(width, height);
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
                                                int width, int height) {
            configureTransform(width, height);
        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
            System.out.println("====surfacetexture destroyed=====");
            return true;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        }

    };

    /**
     * The {@link Size} of camera preview.
     */
    private Size mPreviewSize;

    /**
     * The {@link Size} of video recording.
     */
    private Size mVideoSize;

    /**
     * MediaRecorder
     */
    private MediaRecorder mMediaRecorder;

    /**
     * Whether the app is recording video now
     */
    private boolean mIsRecordingVideo;
    private Semaphore mCameraOpenCloseLock = new Semaphore(1);
    /**
     * An additional thread for running tasks that shouldn't block the UI.
     */

    private HandlerThread mBackgroundThread;
    /**
     * A {@link Handler} for running tasks in the background.
     */
    private Handler mBackgroundHandler;

    /**
     * A {@link Handler} for running tasks in the background.
     *//**//**
     * A {@link Semaphore} to prevent the app from exiting before closing the camera.
     *//**/
    /**
     * {@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its status.
     */
    private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {

        @Override
        public void onOpened(@NonNull CameraDevice cameraDevice) {
            System.out.println("====surfacetexture camera opened=====");
            mCameraDevice = cameraDevice;
            startPreview();
            mCameraOpenCloseLock.release();
            if (null != mTextureView) {
                configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
            }
        }

        @Override
        public void onDisconnected(@NonNull CameraDevice cameraDevice) {
            mCameraOpenCloseLock.release();
            cameraDevice.close();
            mCameraDevice = null;
        }

        @Override
        public void onError(@NonNull CameraDevice cameraDevice, int error) {
            mCameraOpenCloseLock.release();
            cameraDevice.close();
            mCameraDevice = null;
            CameraDevice.StateCallback activity = this;
            if (null != activity) {
                finish();
            }
        }
    };
    private Integer mSensorOrientation;
    private String mNextVideoAbsolutePath;
    private CaptureRequest.Builder mPreviewBuilder;

    public static VideoRecordingFragment newInstance() {
        return new VideoRecordingFragment();
    }

    /**
     * In this sample, we choose a video size with 3x4 aspect ratio. Also, we don't use sizes
     * larger than 1080p, since MediaRecorder cannot handle such a high-resolution video.
     *
     * @param choices The list of available sizes
     * @return The video size
     */
    private static Size chooseVideoSize(Size[] choices) {
        for (Size size : choices) {
            if (size.getWidth() == size.getHeight() * 4 / 3 && size.getWidth() <= 1080) {
                return size;
            }
        }
        Log.e(TAG, "Couldn't find any suitable video size");
        return choices[choices.length - 1];
    }

    /**
     * Given {@code choices} of {@code Size}s supported by a camera, chooses the smallest one whose
     * width and height are at least as large as the respective requested values, and whose aspect
     * ratio matches with the specified value.
     *
     * @param // The list of sizes that the camera supports for the intended output class
     *           /@param       The minimum desired height
     * @param /  /The aspect ratio
     * @return The optimal {@code Size}, or an arbitrary one if none were big enough
     */
    private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
        // Collect the supported resolutions that are at least as big as the preview Surface
        List<Size> bigEnough = new ArrayList<>();
        int w = aspectRatio.getWidth();
        int h = aspectRatio.getHeight();
        for (Size option : choices) {
            if (option.getHeight() == option.getWidth() * h / w &&
                    option.getWidth() >= width && option.getHeight() >= height) {
                bigEnough.add(option);
            }
        }

        // Pick the smallest of those, assuming we found any
        if (bigEnough.size() > 0) {
            return Collections.min(bigEnough, new VideoRecordingFragment.CompareSizesByArea());
        } else {
            return choices[0];
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_video_recording);
        setContentView(R.layout.fragment_video_recording);
        // VideoRecordingFragment frag = new VideoRecordingFragment();
        System.out.println("================video recording activity===");
        mTextureView = (AutoFitTextureView) findViewById(R.id.texture);
        mButtonVideo = (ImageView) findViewById(R.id.video);
        mButtonVideo.setOnClickListener(this);

        new SimpleTooltip.Builder(this)
                .anchorView(findViewById(R.id.videoCameraId))
                .text("tap to record")
                .gravity(Gravity.TOP)
                .backgroundColor(getResources().getColor(R.color.colorAccent))
                .animated(true)
                .dismissOnOutsideTouch(true)
                .dismissOnInsideTouch(true)
                .transparentOverlay(false)
                .build()
                .show();
        //   Bundle extras = getIntent().getExtras();
        //  Double lat = extras.getDouble("LATITUDE");
        //   Double lang = extras.getDouble("LONGITUDE");

        //    Bundle bundle = new Bundle();
        //    bundle.putDouble("lat", lat);
        //    bundle.putDouble("lang", lang);
        //     Utilities.loadFragment(this, new VideoRecordingFragment(), true, null, "fragment");
        //   Utilities.loadFragment(this, VideoRecordingFragment(), true, null, "sfdfg");
            /*FragmentManager fragmentManager = ((FragmentActivity) this).getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.addToBackStack(null);
            frag.setArguments(bundle);
            fragmentTransaction.replace(R.id.container, frag);
            fragmentTransaction.commit();*/

//            getFragmentManager().beginTransaction()
//                    .replace(R.id.container, VideoRecordingFragment.newInstance())
//                    .commit();
    }

    @Override
    public void onResume() {
        super.onResume();
        startBackgroundThread();
        if (mTextureView.isAvailable()) {
            System.out.println("=====texture view is available==");
            openCamera(mTextureView.getWidth(), mTextureView.getHeight());
        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }

    @Override
    public void onPause() {
        closeCamera();
        stopBackgroundThread();
        super.onPause();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.video: {
                if (mIsRecordingVideo) {
                    System.out.println("====stopped Recording======");
                    stopRecordingVideo();
                } else {
                    System.out.println("====started Recording======");
                    startRecordingVideo();
                }
                break;
            }
        }
    }

    /**
     * Starts a background thread and its {@link Handler}.
     */
    private void startBackgroundThread() {
        mBackgroundThread = new HandlerThread("CameraBackground");
        mBackgroundThread.start();
        mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
    }

    /**
     * Stops the background thread and its {@link Handler}.
     */
    private void stopBackgroundThread() {
        mBackgroundThread.quitSafely();
        try {
            mBackgroundThread.join();
            mBackgroundThread = null;
            mBackgroundHandler = null;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * Tries to open a {@link CameraDevice}. The result is listened by `mStateCallback`.
     */
    @SuppressWarnings("MissingPermission")
    private void openCamera(int width, int height) {
        final Activity activity = this;
        if (null == activity || activity.isFinishing()) {
            return;
        }
        CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
        try {
            Log.d(TAG, "tryAcquire");
            if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
                throw new RuntimeException("Time out waiting to lock camera opening.");
            }
            String cameraId = manager.getCameraIdList()[0];

            // Choose the sizes for camera preview and video recording
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
            StreamConfigurationMap map = characteristics
                    .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
            if (map == null) {
                throw new RuntimeException("Cannot get available preview/video sizes");
            }
            mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
            mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                    width, height, mVideoSize);

            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            } else {
                mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
            }
            configureTransform(width, height);
            mMediaRecorder = new MediaRecorder();
            manager.openCamera(cameraId, mStateCallback, null);
        } catch (CameraAccessException e) {
            Toast.makeText(activity, "Cannot access the camera.", Toast.LENGTH_SHORT).show();
            activity.finish();
        } catch (NullPointerException e) {
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while trying to lock camera opening.");
        }
    }
    private void closeCamera() {
        try {
            mCameraOpenCloseLock.acquire();
            closePreviewSession();
            if (null != mCameraDevice) {
                mCameraDevice.close();
                mCameraDevice = null;
            }
            if (null != mMediaRecorder) {
                mMediaRecorder.release();
                mMediaRecorder = null;
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while trying to lock camera closing.");
        } finally {
            mCameraOpenCloseLock.release();
        }
    }

    /**
     * Start the camera preview.
     */
    private void startPreview() {
        if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
            return;
        }
        try {
            closePreviewSession();
            SurfaceTexture texture = mTextureView.getSurfaceTexture();
            assert texture != null;
            texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

            Surface previewSurface = new Surface(texture);
            mPreviewBuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Collections.singletonList(previewSurface),
                    new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(@NonNull CameraCaptureSession session) {
                            mPreviewSession = session;
                            updatePreview();
                        }
                        @Override
                        public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                            CameraCaptureSession.StateCallback activity = this;
                            if (null != activity) {
                                // Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }, mBackgroundHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * Update the camera preview. {@link #startPreview()} needs to be called in advance.
     */
    private void updatePreview() {
        if (null == mCameraDevice) {
            return;
        }
        try {
            setUpCaptureRequestBuilder(mPreviewBuilder);
            HandlerThread thread = new HandlerThread("CameraPreview");
            thread.start();
            mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, mBackgroundHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    private void setUpCaptureRequestBuilder(CaptureRequest.Builder builder) {
        builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
    }
    /**
     * Configures the necessary {@link Matrix} transformation to `mTextureView`.
     * This method should not to be called until the camera preview size is determined in
     * openCamera, or until the size of `mTextureView` is fixed.
     *
     * @param viewWidth  The width of `mTextureView`
     * @param viewHeight The height of `mTextureView`
     */
    private void configureTransform(int viewWidth, int viewHeight) {
        Activity activity = this;
        if (null == mTextureView || null == mPreviewSize || null == activity) {
            return;
        }
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        Matrix matrix = new Matrix();
        RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
        RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
        float centerX = viewRect.centerX();
        float centerY = viewRect.centerY();
        if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
            bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
            matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
            float scale = Math.max(
                    (float) viewHeight / mPreviewSize.getHeight(),
                    (float) viewWidth / mPreviewSize.getWidth());
            matrix.postScale(scale, scale, centerX, centerY);
            matrix.postRotate(90 * (rotation - 2), centerX, centerY);
        }
        mTextureView.setTransform(matrix);
    }

    private void setUpMediaRecorder() throws IOException {
        final Activity activity = this;
        if (null == activity) {
            return;
        }
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
            mNextVideoAbsolutePath = getVideoFilePath(this);
        }
        mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
        mMediaRecorder.setVideoEncodingBitRate(10000000);
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        switch (mSensorOrientation) {
            case SENSOR_ORIENTATION_DEFAULT_DEGREES:
                mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
                break;
            case SENSOR_ORIENTATION_INVERSE_DEGREES:
            mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
                break;
        }
        mMediaRecorder.prepare();
    }
    private String getVideoFilePath(Context context) {
        final File dir = context.getExternalFilesDir(null);
        return (dir == null ? "" : (dir.getAbsolutePath() + "/"))
                + System.currentTimeMillis() + ".mp4";
    }

    private void startRecordingVideo() {
        if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
            return;
        }
        try {
            closePreviewSession();
            setUpMediaRecorder();
            SurfaceTexture texture = mTextureView.getSurfaceTexture();
            assert texture != null;
            texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
            List<Surface> surfaces = new ArrayList<>();

            // Set up Surface for the camera preview
            Surface previewSurface = new Surface(texture);
            surfaces.add(previewSurface);
            mPreviewBuilder.addTarget(previewSurface);

            // Set up Surface for the MediaRecorder
            Surface recorderSurface = mMediaRecorder.getSurface();
            surfaces.add(recorderSurface);
            mPreviewBuilder.addTarget(recorderSurface);

            // Start a capture session
            // Once the session starts, we can update the UI and start recording
            mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                    mPreviewSession = cameraCaptureSession;
                    updatePreview();
                    VideoRecordingActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mButtonVideo.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.stoprec));
                            mIsRecordingVideo = true;
                            // Start recording
                            mMediaRecorder.start();
                        }
                    });
                }
                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                    CameraCaptureSession.StateCallback activity = this;
                    if (null != activity) {
                        // Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show();
                    }
                }
            }, mBackgroundHandler);
        } catch (CameraAccessException | IOException e) {
            e.printStackTrace();
        }

    }

    private void closePreviewSession() {
        if (mPreviewSession != null) {
            mPreviewSession.close();
            mPreviewSession = null;
        }
    }

    private void stopRecordingVideo() {
        // UI
        mIsRecordingVideo = false;
        mMediaRecorder.stop();
        mMediaRecorder.reset();

        Activity activity = this;
        if (null != activity) {
            setPreferenceString(Constants.PREF_VIDEO_PATH, mNextVideoAbsolutePath);
            Toast.makeText(activity, "Video saved: " + mNextVideoAbsolutePath,
                    Toast.LENGTH_SHORT).show();
            Log.d(TAG, "Video saved: " + mNextVideoAbsolutePath);
        }
        Intent intent = new Intent();
        intent.putExtra("VIDEOPATH", mNextVideoAbsolutePath);
        this.setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * Compares two {@code Size}s based on their areas.
     */
    static class CompareSizesByArea implements Comparator<Size> {

        @Override
        public int compare(Size lhs, Size rhs) {
            // We cast here to ensure the multiplications won't overflow
            return Long.signum((long) lhs.getWidth() * lhs.getHeight() -
                    (long) rhs.getWidth() * rhs.getHeight());
        }
    }
private static final int SENSOR\u ORIENTATION\u DEFAULT\u DEGREES=90;
专用静态最终int传感器方向反向度=270;
专用静态最终SparseIntArray默认_方向=新SparseIntArray();
专用静态最终SparseIntArray逆_方向=新SparseIntArray();
私有静态最终字符串标记=“Camera2VideoFragment”;
静止的{
默认方向。附加(Surface.ROTATION\u 0,90);
默认方向。附加(Surface.ROTATION\u 90,0);
默认方向。附加(Surface.ROTATION\u 180、270);
默认_方向。附加(Surface.ROTATION_270、180);
}
静止的{
反向方向追加(Surface.ROTATION\u0270);
反转方向。追加(Surface.ROTATION\u 90、180);
反向方向追加(Surface.ROTATION_180,90);
反转方向。追加(Surface.ROTATION_270,0);
}
/**
*用于相机预览的{@link AutoFitTextureView}。
*/
专用AutoFitTextureView mTextureView;
/**
*录制视频的按钮
*/
私有图像视图mbutonvideo;
私有文本查看上传文本;
/**
*对打开的{@link CameraDevice}的引用。
*/
私人摄像设备;
/**
*对当前{@link CameraCaptureSession}的引用
*预览。
*/
私人摄像机拍摄会议回顾;
/**
*{@link TextureView.SurfaceTextureListener}处理一个服务器上的多个生命周期事件
*{@link TextureView}。
*/
私有TextureView.SurfaceTextReListener mSurfaceTextureListener
=新的TextureView.SurfaceTextureRelistener(){
@凌驾
表面结构上的公共空隙可用(表面结构表面结构,
整数宽度,整数高度){
System.out.println(“===可用的surfacetexture==”;
开放式摄像机(宽度、高度);
}
@凌驾
表面结构上的公共空隙尺寸已更改(表面结构表面结构,
整数宽度,整数高度){
配置变换(宽度、高度);
}
@凌驾
公共布尔onSurfaceTextureDestroyed(SurfaceTexture SurfaceTexture){
System.out.println(“===表面结构已破坏===”;
返回true;
}
@凌驾
已更新SurfaceTexture上的公共空间(SurfaceTexture SurfaceTexture){
}
};
/**
*相机预览的{@link Size}。
*/
私人规模的mPreviewSize;
/**
*视频录制的{@link Size}。
*/
私人规模;
/**
*媒体记录器
*/
专用媒体记录器;
/**
*应用程序是否正在录制视频
*/
私用布尔误录视频;
专用信号量mcameraaopencloselock=新信号量(1);
/**
*用于运行不应阻塞UI的任务的附加线程。
*/
私有HandlerThread mBackgroundThread;
/**
*用于在后台运行任务的{@link Handler}。
*/
私人处理程序mBackgroundHandler;
/**
*用于在后台运行任务的{@link Handler}。
*//**//**
*一个{@link Semaphore}用于在关闭摄像头之前阻止应用程序退出。
*//**/
/**
*{@link CameraDevice.StateCallback}在{@link CameraDevice}更改其状态时调用。
*/
私有CameraDevice.StateCallback mStateCallback=新CameraDevice.StateCallback(){
@凌驾
打开的公共无效(@NonNull CameraDevice CameraDevice){
System.out.println(“===表面纹理摄影机打开===”;
mCameraDevice=cameraDevice;
startPreview();
mCameraOpenCloseLock.release();
if(null!=mTextureView){
configureTransform(mTextureView.getWidth(),mTextureView.getHeight());
}
}
@凌驾
公共无效onDisconnected(@NonNull CameraDevice CameraDevice){
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice=null;
}
@凌驾
public void onError(@NonNull CameraDevice CameraDevice,int error){
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice=null;
CameraDevice.StateCallback活动=此;
如果(空!=活动){
完成();
}
}
};
私有整数方向;
私有字符串mNextVideoAbsolutePath;
私人CaptureRequest.Builder mPreviewBuilder;
公共静态录像片段newInstance(){
返回新的VideoRecordingFragment();
}
/**
*在这个示例中,我们选择了一个宽高比为3x4的视频大小。此外,我们不使用尺寸
*大于1080p,因为MediaRecorder无法处理如此高分辨率的视频。
*
*@param选择可用大小的列表
*@返回视频大小
*/
专用静态大小选择器视频大小(大小[]选项){
用于(大小:选择){
如果(size.getWidth()==size.getHeight()*4/3&&size.getWidth()=width&&option.getHeight()>=height){
足够大。添加(选项);
}
}
//挑最小的,假设我们找到了
如果(足够大。大小()>0){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);`