简体   繁体   English

相机预览不起作用(CWAC-相机库)

[英]camera preview not working(CWAC-Camera library)

I am new to android stuff and want to work with camera stuff so I am trying to hands on with CWAC camera library.I am using simple camerafragment which would show fullscreen camera preview.But camera preview is invisible.Can anyone suggest what should I do to rectify it.Here is my code: 我是android的新手,想使用相机的东西,所以我尝试使用CWAC相机库。我正在使用简单的camerafragment,它将显示全屏相机预览。但是相机预览是不可见的。谁能建议我该怎么办纠正它。这是我的代码:

FullCameraFragment.java 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 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 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 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>

Among other possible problems, you are mixing native API Level 11 fragments with Android Support package backported fragments. 除其他可能的问题外,您还将本机API级别11片段与Android支持程序包反向移植的片段混合在一起。 That will not work. 那不管用。

Specifically, FullCameraFragment inherits from com.commonsware.cwac.camera.CameraFragment , which itself inherits from android.app.Fragment . 具体来说, FullCameraFragment继承自com.commonsware.cwac.camera.CameraFragment ,而com.commonsware.cwac.camera.CameraFragment本身也继承自android.app.Fragment Your cameramain inherits from FragmentActivity , which does not work with android.app.Fragment , but rather android.support.v4.app.Fragment . cameramain从继承FragmentActivity ,不与工作android.app.Fragment ,而是android.support.v4.app.Fragment That does not work. 那行不通。

The simplest solution is to have cameramain inherit from Activity , and use native API Level 11+ fragments. 最简单的解决方案是让cameramain继承自Activity ,并使用本机API Level 11+片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM