@flyouting
2014-03-19T16:07:30.000000Z
字数 2890
阅读 5114
这是基于Android Studio及Fragment的相机开发的第四章,如果你还没准备好,先去github上拉一下我的一个示例工程。本章主要包含“NativeCameraFragment.”
作者注: 这个例子目前在“测试版”, 我将根据别人的建议更新改进它。完整的相机的例子是很复杂的,如果你想要一个完整的工程实现示例用在自己的项目中,可以考虑这个例子:CWAC。这里的目标只是演示基本主体。
除了Android的其他相机应用程序,您也可以把相机融合在你的应用程序本身中。这样做需要直接使用Android的相机,并把结果显示在“预览”面或SurfaceView。虽然这不是完美的(退出Fragment返回会崩溃),演示应用程序应该只是研究相机的一个起点。
当Fragment的视图被创建时,首先是我们尝试绑定相机,获取相机预览。你可以在Google官方文档中阅读这个主题相关的资料。现在这里有个实现例子:
private boolean safeCameraOpenInView(View view) {
boolean qOpened = false;
releaseCameraAndPreview();
mCamera = getCameraInstance();
qOpened = (mCamera != null);
mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera);
FrameLayout preview = (FrameLayout) view.findViewById(R.id.camera_preview);
preview.addView(mPreview);
return qOpened;
}
这段的功能是获取相机的实例,并附加到我们的CameraPreview上。
这个CameraPreview继承SurfaceView和SurfaceHolder.callback。预览会显示相机的输入流,让你知道你所获取的图片是什么样的。它也负责管理SurfaceHolder本身的各种生命周期。
/**
* Surface on which the camera projects it's capture results.
*/
class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw
// the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your
// activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events
// here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Camera.PictureCallback是Android中的一个方法,用于当相机拍了一张照片时获得输出的结果。这里实现如下:
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Toast.makeText(getActivity(), "Image retrieval failed.", Toast.LENGTH_SHORT).show();
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
翻译:@flyouting
时间:2014/03/19
源地址