CWAC Camera - Multiple camera views in one layout (and switching between them)

眉间皱痕 提交于 2019-12-25 07:29:47

问题


I am trying to build a camera feature similar to the new Front Back app - where you take two pictures in the same activity and display them each on half of the screen. So, the top half of the screen will be displaying the camera preview while the bottom half is black, you then take one picture, that picture is displayed in place (in the top half), and then the bottom half of the screen would then become the preview, and you can then take another picture which will then be displayed in the bottom half of the screen. I want to be able to switch back and forth between the standard camera view and the front facing camera at each step.

I've managed to get most of this functionality working - you can take the two pictures, but I am unable to switch between standard and front facing on the second image capture because I am forced to recreate a new fragment (or reuse the front facing one i've previously created), and I lose the image taken in the first step.

In case that isn't clear, here's how I did it: I create a fragment and inflate a layout with a Camera View in the top half of the screen, and a black filler view in the bottom half (I initially tried to inflate two camera views, but wasn't able to stop the preview in the top camera view and then restart it in the bottom one - if there's a way to do this that I am overlooking, please let me know). Once I capture the top image, I grab the whole byte array and then create a bitmap from a 1/4 offset of the total height to 3/4 of the total height. This allows me to display exactly what was in the preview (unnecessary details but I figure they may help someone out in the future). Since I wasn't able to use a second CameraView, i used a hacky way of just "bumping down" the same cameraview and reusing it - I removed the filler view from the layout and re-added it to the layout at position 0, underneath (lower z value) the now-displayed image. So, now the camera preview is displayed at the bottom, and I can repeat the process of capturing the image and displaying it in the bottom. However, if I want to switch to the front-facing camera at this point, I must completely switch fragments, and I lose the first image - the process starts over. I just realized that I could pass the image data from fragment to fragment (or pass a URI since the images are way over the 1 MB intent limit), but it seems like there should be an easier way to do this.

I also tried nesting fragments - making both the upper and lower halves its own camera fragment, each inflating its own cameraview at the proper time, but I was getting unknown camera errors (Error 2)

Is this a limitation of the library or am I overlooking a solution here?

Thanks in advance for all of your help.


回答1:


I just realized that I could pass the image data from fragment to fragment (or pass a URI since the images are way over the 1 MB intent limit), but it seems like there should be an easier way to do this.

Have the hosting activity hold onto the images.

Or, have a retained fragment hold onto the images, if you are supporting configuration changes.




回答2:


Not sure if you still looking for a single-fragment solution. But I manage to switch the front/back camera using following code without creating another fragment. So, you may modifying a bit and it should fit yours.

My solution is as simple as removing the current CameraView instance and add a new one. If you prefer add it to another frame, be my guest.

The CameraView is added dynamically into a Framelayout:

public class MyCameraFragment extends CameraFragment {

CameraView cameraView;

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    addCameraView(view);
}

private void addCameraView(View view) {
    FrameLayout frame = (FrameLayout)v.findViewById(R.id.cameraFrame);
    frame.removeAllViews();
    cameraView = new CameraView(getActivity());
    cameraView.setHost(cameraHost = new MyCameraHost(getActivity()));
    setCameraView(cameraView);
    frame.addView(cameraView);
}

private void doSwitchCamera() {
    // do some change to the settings.
    useFrontFacingCamera = !useFrontFacingCamera;
    if (null != cameraView) {
        cameraView.onPause();
    }
    addCameraView(getView());
    cameraView.onResume();
}

}

I'm not sure if super.onPause() and super.onResume() is necessary but they do call the onPause() and onResume() of CameraView, which I believe it is necessary.

Edited: after some code investigation and testing, I found that calling super.onPause/onResume is not necessary. Just call onPause and onResume on the cameraView is enough.



来源:https://stackoverflow.com/questions/23329421/cwac-camera-multiple-camera-views-in-one-layout-and-switching-between-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!