您好我正在使用SurfaceView开发具有相机功能的应用程序。 我想在创建表面视图时拍照。
在我的片段onCreateView方法中,初始化了我需要的所有内容。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.game_one_start_fragment, container, false);
initWidgets(view);
return view;
}
在initWidgets()方法中初始化SurfaceView对象,如下所示
mSurfaceView = (SurfaceView) view.findViewById(R.id.game_action_surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
在OnResume()方法中获取surfaceHolder对象。
@Override
public void onResume() {
super.onResume();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
safeCameraOpen(Camera.CameraInfo.CAMERA_FACING_BACK);
}
safeCameraOpen()我已关注Android Developer link。使用与上述相同的代码。
surfaceHolder.callback使用如下
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera.startPreview();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try {
//to set the picture orientation
setCameraDisplayOrientation(getActivity(), findFrontFacingCameraID(), mCamera);
mCamera.startPreview();
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
Camera.Parameters parameters = mCamera.getParameters();
parameters.getSupportedPreviewSizes();
mCamera.setParameters(parameters);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
我想在加载片段时拍照。
我尝试过OnViewCreated(),onResume()和onStart()生命周期方法。 在其中我使用了以下行
mCamera.takePicture(null,null,mPictureCallback);
(在safeCameraOpen()方法中创建和初始化的mCamera对象)
如果我在提到的生命周期方法中尝试,我会收到以下崩溃错误。
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1266)
at android.hardware.Camera.takePicture(Camera.java:1211)
at com.gaming.fragment.GameOneStartFragment.snapPicture(GameOneStartFragment.java:76)
at com.gaming.fragment.GameOneStartFragment.surfaceCreated(GameOneStartFragment.java:284)
at android.view.SurfaceView.updateWindow(SurfaceView.java:572)
at android.view.SurfaceView.access$000(SurfaceView.java:86)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1897)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
但是,当我在按钮内使用Camera对象的takePicture()方法时,单击它会捕获图片。
如何在没有点击查看的情况下拍照?
答案 0 :(得分:0)
您不会调用Camera对象的startPreview方法。 我想你应该在拍照前打电话给startPreview。 在这种情况下,您没有设置预览显示:
try {
camera.setPreviewDisplay(surface.getHolder());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
检查此答案:How to take pictures from the camera without preview when my app starts?