小编J.M*_*.M.的帖子

使用Android camera2进行全屏预览

我正在使用新的camera2 API构建自定义相机.我的代码是基于谷歌提供的代码示例在这里.

我无法找到一种方法来全屏显示相机.在代码示例中,他们使用比率优化来适应所有屏幕,但它只占屏幕高度的3/4左右.

这是我的代码AutoFitTextureView:

public class AutoFitTextureView extends TextureView {

private int mRatioWidth = 0;
private int mRatioHeight = 0;

public AutoFitTextureView(Context context) {
    this(context, null);
}

public AutoFitTextureView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

/**
 * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
 * calculated from the parameters. Note …
Run Code Online (Sandbox Code Playgroud)

camera android camera2 android-camera2

11
推荐指数
2
解决办法
8717
查看次数

Android Camera2 API切换后置摄像头

我正在创建一个使用新camera2 API捕获视频的自定义相机.

我的代码强力地由谷歌提供的代码启发在这里.我的相机预览有一个按钮,可以从后摄像头切换到前摄像头,然后从前摄像头切换到后摄像头.默认情况下,使用后置摄像头启动"摄像头预览"活动.

出于某种原因,当我第一次点击"开关/交换相机"按钮,它会带来前面的相机,但是每次我再次点击时,开关/交换都不再起作用了:预览(在前置摄像头上)稍微褪色,就像发生了什么事情一样,但它仍然保留在当前选择的(前置)摄像头上.

这是我的代码:

在a中RecordVideoFragment,在onViewCreated:

//  Switch camera button
        switchCameraButton = (ImageButton) view.findViewById(R.id.button_switch_camera);
        // Listener for Switch cameras button
        switchCameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchCameras();
            }
        });
Run Code Online (Sandbox Code Playgroud)

这是switchCameras()功能:

private void  switchCameras() {
        mCameraOpenCloseLock.release();
        mCameraDevice.close();

        CameraManager mCameraManager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
        try {
            String mCameraId = mCameraManager.getCameraIdList()[0];
            if (mCameraId.equals("0")) {   // If currently on FRONT camera (0 = CameraCharacteristics.LENS_FACING_FRONT)
                mCameraId = "1";           // switch to BACK camera …
Run Code Online (Sandbox Code Playgroud)

camera android camera2 android-camera2

10
推荐指数
2
解决办法
1万
查看次数

使用Android camera2 API打开/关闭闪光灯无法正常工作

我正在创建一个带有自定义相机的Android应用程序,我正在切换到新的camera2 API.我有一个按钮,可以在后置摄像头开启时打开和关闭闪光灯(不停止摄像头,就像任何经典的相机应用程序一样).

当我点击闪存图标时,没有任何反应,这就是logcat返回的内容:

D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它不起作用.这是代码:

我有一个RecordVideoActivity使用RecordVideoFragment.这是片段的XML部分,其中包含flash按钮代码:

<ImageButton
    android:id="@+id/button_flash"
    android:src="@drawable/ic_flash_off"
    android:layout_alignParentLeft="true"
    style="@style/actions_icons_camera"
    android:onClick="actionFlash"/>
Run Code Online (Sandbox Code Playgroud)

和Java代码:

ImageButton flashButton;
private boolean hasFlash;
private boolean isFlashOn = false;
Run Code Online (Sandbox Code Playgroud)

随着onViewCreated:

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    ...
    [some code]
    ...
    // Flash on/off button
    flashButton = (ImageButton) view.findViewById(R.id.button_flash);
    // Listener for Flash on/off button
    flashButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            actionFlash();
        }
    });
Run Code Online (Sandbox Code Playgroud)

这是actionFlash()函数定义:

private …
Run Code Online (Sandbox Code Playgroud)

camera android camera2 android-camera2

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×3

android-camera2 ×3

camera ×3

camera2 ×3