当前位置: 首页 > 知识库问答 >
问题:

一些Android设备前置摄像头录像神器问题怎么解决?

乐正远航
2023-03-14

举个例子,下面是一段由三星Galaxy S3录制的视频截图:

有怪异的神器和颜色,视频似乎并不居中(它是指着一个人的脸,你可以看到在镜头中只是勉强可见)。

这是我的代码:

//
// starting code
//

CAMERA = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
Camera.Parameters cameraParameters = CAMERA.getParameters();
if (cameraParameters.isZoomSupported()) {
    cameraParameters.setZoom(0);
}
CAMERA.setParameters(cameraParameters);
CAMERA.setDisplayOrientation(90);

CAMERA.setPreviewDisplay(CAMERA_SURFACE_VIEW.getHolder());
CAMERA.startPreview();
CAMERA.unlock();

CAMERA_MEDIA_RECORDER = new MediaRecorder();
CAMERA_MEDIA_RECORDER.setCamera(CAMERA);
try {
    CAMERA_MEDIA_RECORDER.setOrientationHint(270);
} catch (Exception e) {
    // this is to fix "setParameter failed" b/c of unsupported orientation hint
    CAMERA_MEDIA_RECORDER.setOrientationHint(90);
}
CAMERA_MEDIA_RECORDER.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
CAMERA_MEDIA_RECORDER.setVideoSource(MediaRecorder.VideoSource.CAMERA);

ArrayList<Integer> profileIds = new ArrayList<Integer>();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    profileIds.add(CamcorderProfile.QUALITY_480P);
    profileIds.add(CamcorderProfile.QUALITY_720P);
}

profileIds.add(CamcorderProfile.QUALITY_HIGH);
profileIds.add(CamcorderProfile.QUALITY_LOW);

CamcorderProfile camcorderProfile = null;

for (int profileId : profileIds) {
    try {
        camcorderProfile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT, profileId);
        break; // found our most desired profile, done
    } catch (IllegalArgumentException e) {
        // profile not found on device... It's okay, the next one might exist.
        continue;
    } catch (RuntimeException e) {
        continue;
    }
}

if (camcorderProfile == null) {
    return false;
}

CAMERA_MEDIA_RECORDER.setProfile(camcorderProfile);
CAMERA_MEDIA_RECORDER.setAudioEncodingBitRate(96000);

int audioSamplingRate = 44100;

for (int rate : new int[] {44100, 32000, 22050, 16000, 11025, 8000}) {
    if (AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT) > 0) {
        audioSamplingRate = rate;
        break;
    }
}

CAMERA_MEDIA_RECORDER.setAudioSamplingRate(audioSamplingRate);
CAMERA_MEDIA_RECORDER.setVideoEncodingBitRate(700000);

String extension = ".3gpp";

if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.MPEG_4) {
    extension = ".mp4";
}

File file = new File(Internal.getStoragePath(Internal.CURRENT_ACTIVITY) + "/webcam" + extension);
file.createNewFile();

CAMERA_MEDIA_RECORDER.setOutputFile(file.toString());
CAMERA_MEDIA_RECORDER.setPreviewDisplay(CAMERA_SURFACE_VIEW.getHolder().getSurface());

CAMERA_MEDIA_RECORDER.prepare();
CAMERA_MEDIA_RECORDER.start();

//
// stopping code
//

try {
    CAMERA_MEDIA_RECORDER.stop();
} catch (IllegalStateException e) {
    // do nothing
} catch (RuntimeException e) {
    // do nothing
}

CAMERA_MEDIA_RECORDER.reset();
CAMERA_MEDIA_RECORDER.release();

CAMERA.lock();
CAMERA.stopPreview();
CAMERA.release();

CAMERA_MEDIA_RECORDER = null;
CAMERA = null;
CAMERA_SURFACE_VIEW = null;

编辑:我应该指出surfaceView是1乘1像素,超出了屏幕的界限。我不希望显示预览。

如何确保所有带有前置摄像头的设备上的视频记录良好?

共有1个答案

邹禄
2023-03-14

“接受”的答案只是一个变通办法,并不能解决问题,因为当您释放预览并开始录制时,缩放或方面可能会发生变化,因为MediaCorder调整预览表面以适应实际需要,所以当您点击录制时,镜头的取景会发生变化,这是不可取的。正确的做法是将预览大小设置为与录制大小的纵横比一致,以便MediaCorder得到它所期望的表面。

 类似资料:
  • 我想写一个通过智能手机摄像头录制视频的应用程序。我找到了这个网站来寻求帮助:http://developer.android.com/guide/topics/media/camera.html#Custom-camera 我使用那个源代码开始。 当应用程序启动时,摄像头显示出来,当我按下“捕获”按钮时,这个错误就抛出了。现在我认为问题“mCamera=getCameraInstance();”在

  • 本文向大家介绍Android中判断是否有前置摄像头、后置摄像头的方法,包括了Android中判断是否有前置摄像头、后置摄像头的方法的使用技巧和注意事项,需要的朋友参考一下 通常我们进行摄像头操作,如扫描二维码需要判断是否有后置摄像头(Rear camera),比如Nexus 7 一代就没有后置摄像头,这样在尝试使用的时候,我们需要进行判断进行一些提示或者处理。 以下代码为一系列的方法,用来判断是否

  • 在为inApp相机实施camera2 API后,我注意到在三星设备上图像显得模糊。搜索之后,我找到了Sasmung Camera SDK(http://developer.samsung.com/galaxy#camera)。因此,在三星Galaxy S7上实施SDK后,图像现在很好,但在Galaxy S6上它们仍然模糊不清。有人在三星设备上遇到过这些问题吗? 编辑:补充@rcsumners的评论

  • 问题内容: 我正在使用来开发iPad2中的前置摄像头应用程序。 当我捕获图像时,它显示为从左向右翻转。 我该如何纠正? 问题答案: 您可以使用源图像翻转图像 编辑: 添加了快速代码