相机预览无法正常工作(CWAC-Camera库)

时间:2015-04-07 20:22:42

标签: android android-camera commonsware-cwac cwac-camera

我是Android新手的东西,想要使用相机的东西,所以我正在尝试使用CWAC相机库。我使用简单的相机碎片,这将显示全屏相机预览。但相机预览是看不见的。任何人都可以建议什么我该怎么做才能纠正它。这是我的代码:

FullCameraFragment.java

package com.mission.fourinc.cwac;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.commonsware.cwac.camera.CameraFragment;
import com.commonsware.cwac.camera.CameraView;
import com.commonsware.cwac.camera.SimpleCameraHost;

/**
 * Created by sanjaytalreja on 4/7/15.
 */
public class FullCameraFragment extends CameraFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View cameraView=super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_cameramain, container, false);
        ((ViewGroup)rootView.findViewById(R.id.camera)).addView(cameraView);
        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHost(new SimpleCameraHost(getActivity()));
    }
}

cameramain.java

    package com.mission.fourinc.cwac;

    import android.support.v4.app.FragmentActivity;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.os.Build;

    import com.commonsware.cwac.camera.CameraFragment;


    public class cameramain extends FragmentActivity {

        FullCameraFragment fcm=null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_cameramain);
            fcm=(FullCameraFragment)getFragmentManager().findFragmentById(R.id.camera_preview);

            }
        }

fragment_cameramain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    tools:context=".cameramain$FullCameraFragment">

    <FrameLayout
        android:id="@+id/camera"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_below="@+id/camera"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="180dp" />

</RelativeLayout>

activity_cameramain.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".cameramain" tools:ignore="MergeRootFrame"
    >
<fragment
        android:id="@+id/camera_preview"
        android:name="com.mission.fourinc.cwac.FullCameraFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </FrameLayout>

1 个答案:

答案 0 :(得分:0)

在其他可能的问题中,您将本机API Level 11片段与Android Support包后向移植的片段混合在一起。那不行。

具体而言,FullCameraFragment继承自com.commonsware.cwac.camera.CameraFragmentandroid.app.Fragment本身继承自cameramain。您的FragmentActivity继承自android.app.Fragment,但不适用于android.support.v4.app.Fragment,而是cameramain。这不起作用。

最简单的解决方案是让Activity继承自{{1}},并使用原生API Level 11+片段。