【发布时间】:2014-03-13 20:39:40
【问题描述】:
我正在尝试以编程方式从 Android 设备的前置摄像头录制视频。虽然我拥有的大多数测试设备(包括 Nexus 4、Nexus 7 和三星 Galaxy S4)都能正确录制视频,但有些则不能。
例如,下面是三星 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 x 1 像素,并且超出了屏幕的范围。我不想显示预览。
如何确保所有带有前置摄像头的设备都能正常录制视频?
【问题讨论】:
标签: android android-camera android-mediarecorder