如何解决一些Android设备有前置摄像头视频录制神器问题? [英] How to solve artifact issue with front camera video recording on some Android devices?
问题描述
我想从Android设备的前置摄像头编程录制视频。虽然大多数的测试设备我有,包括的Nexus 4,Nexus 7和三星Galaxy S4,正确地记录视频,有些则没有。
I am trying to programmatically record video from the front camera of an Android device. While most test devices I have, including the Nexus 4, Nexus 7, and the Samsung Galaxy S4, record the video correctly, some do not.
例如,这里有一个三星Galaxy S3录制的视频片段的截图:
For example, here's a screenshot of a segment of video recorded by a Samsung Galaxy S3:
有怪异的文物和颜色,以及视频似乎并不为中心(它正指向一个人的脸,你可以看到的只是在出手勉强可见)。
There are weird artifacts and colors, and the video does not seem to be centered (it is pointed at a person's face, which you can see is just barely visible in the shot).
下面是我的code:
//
// 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像素,并且屏幕的范围之外。我不希望显示preVIEW。
I should point out that the surfaceView is 1 by 1 px and is outside of the screen's bounds. I do not wish to display a preview.
我如何确保该录像是一个前置摄像头?
How do I make sure that the video recording is good on all devices with a front facing camera?
推荐答案
我终于能拿到三星Galaxy S3设备上的我的手和运行一些测试。这个问题是由以下行引起的:
I was finally able to get my hands on a Samsung Galaxy S3 device and run some tests. The issue was caused by the following lines:
CAMERA.setPreviewDisplay(CAMERA_SURFACE_VIEW.getHolder());
CAMERA.startPreview();
以下的答案我在计算器上发现导致我的解决办法: http://stackoverflow.com/a/14319270/379245
如果在 SurfaceView
preVIEW是之间共享摄像机
和 MediaRecorder
,它显然可以导致问题,如一个我经历过。从相机
对象中删除preVIEW显示的设置,最终解决了该问题。
If the SurfaceView
preview is shared between Camera
and MediaRecorder
, it can apparently cause issues such as the one I experienced. Removing the setting of the preview display from the Camera
object ultimately fixed the issue.
这篇关于如何解决一些Android设备有前置摄像头视频录制神器问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!