Java android的Commonsguy摄像头库集成,显示空白预览

Java android的Commonsguy摄像头库集成,显示空白预览,java,android,commonsware-cwac,Java,Android,Commonsware Cwac,我正在尝试集成一个类似instagram的CamCoder,但是作为初学者,我正在尝试使用Commonsguy库中的以下代码来放置录像机预览 主要活动如下 @SuppressLint("SimpleDateFormat") public class RecorderActivity extends BaseActivity implements CamCoderView.Contract { //private CamCoderView ffc = null; //private

我正在尝试集成一个类似instagram的CamCoder,但是作为初学者,我正在尝试使用Commonsguy库中的以下代码来放置录像机预览

主要活动如下

@SuppressLint("SimpleDateFormat")
public class RecorderActivity extends BaseActivity implements CamCoderView.Contract
{
    //private CamCoderView ffc = null;
    //private CamCoderView std = null;
    private CamCoderView camcoder = null;

    private boolean hasTwoCameras = (Camera.getNumberOfCameras() > 1);
    private boolean singleShot = true;

    private ImageButton captureBtn;
    private ImageButton rotateBtn;  
    private ImageButton doneBtn;    

    private int cameraType = Camera.CameraInfo.CAMERA_FACING_BACK;

    private boolean isCapturePressed = false;

    private ArrayList<String> pathNames = new ArrayList<String>();
    private String randomPathName;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_record_layout);
        WindowManager.LayoutParams layout = getWindow().getAttributes();
        layout.screenBrightness = 1F;
        getWindow().setAttributes(layout);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        camcoder = CamCoderView.newInstance(false);
        getFragmentManager().beginTransaction()
                            .replace(R.id.container, camcoder ).commit();

        randomPathName =  new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

        captureBtn = (ImageButton) findViewById(R.id.captureBtn);
        doneBtn    = (ImageButton) findViewById(R.id.doneBtn);
        rotateBtn  = (ImageButton) findViewById(R.id.rotateBtn);

        if (hasTwoCameras)
            rotateBtn.setEnabled(true);

        rotateBtn.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                if (cameraType == Camera.CameraInfo.CAMERA_FACING_BACK)
                {        
                    cameraType = Camera.CameraInfo.CAMERA_FACING_FRONT;
                    camcoder = CamCoderView.newInstance(true);
                    getFragmentManager().beginTransaction()
                                      .replace(R.id.container, camcoder ).commit();
                }
                else
                {
                    cameraType = Camera.CameraInfo.CAMERA_FACING_BACK;
                    camcoder = CamCoderView.newInstance(false);
                    getFragmentManager().beginTransaction()
                                      .replace(R.id.container, camcoder ).commit();                 
                }
            }
        });

        doneBtn.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {               
            }
        });

        captureBtn.setOnTouchListener(new OnTouchListener() 
        {
            @SuppressLint("SdCardPath")
            @Override
            public boolean onTouch(View arg0, MotionEvent event) 
            {
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    if(isCapturePressed == false)
                    {
                        isCapturePressed = true;
                        String path = getOutputMediaFile().getAbsolutePath();
                        pathNames.add(path);
                        camcoder.startRecording(path);
                    }
                }
                else if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    isCapturePressed = false;
                    try {
                        camcoder.stopRecording();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return false;
            }
        });
    }

    @SuppressLint("DefaultLocale")
    @Override 
    public void onBackPressed()
    {
        super.onBackPressed();
        if (pathNames != null)
        {
            for (int i = 0 ; i < pathNames.size(); i++)
            {
                File file = new File(pathNames.get(i));
                if(file.exists())
                {
                    file.delete();
                }
            }
        }
        finish();
    }

    @SuppressLint("SimpleDateFormat")
    private File getOutputMediaFile() 
    {
        File filesDir = getDir("users", Context.MODE_PRIVATE); //Creating an internal dir;
        if(!filesDir.isDirectory())
        {
            if (!filesDir.mkdirs()) 
            {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        } 

        File mediaFile;
        mediaFile = new File(filesDir.getPath() + File.separator
                + "vid_" + randomPathName + "_" + String.valueOf(pathNames.size()) +".mp4");

        return mediaFile;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    public boolean isSingleShotMode() 
    {
        return(singleShot);
    }   

    @Override
    public void setSingleShotMode(boolean mode) 
    {
        singleShot = mode;
    }

    @Override
    public void callUIMethodForStore(Intent intent) {       
    }   
}
在我的HTC感觉上,它工作起来很有魅力,但是我的s3根本不工作


好,问题已修复,似乎

<style name="AppBaseTheme" parent="android:style/Theme.Light.NoTitleBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->

    <!-- <item name="android:background">@drawable/main_bg</item> -->
    <!-- <item name="android:windowBackground">@drawable/main_bg</item> -->
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
</style> 

真的
假的

应用程序上的设置主题导致了这个问题,我已经删除了它,现在它工作正常!!奇怪

“我哪里错了”——我没办法知道。您需要确定是否实际使用了此片段,并使用诸如层次视图之类的工具确定库中的
CameraView
是否实际在屏幕上。“应该做什么样的设置”--你需要问Instagram这个问题。@Commonware是的,视图打开了,只是没有显示预览,片段工作正常。设置实际上意味着,如何将视频预览设置为“方形”并初始化录像机:)试试演示应用程序。如果它们不起作用,那么我对你的设备的支持就有问题了,我需要知道更多。此库中不支持方形预览,我也看不到它的有效用例。我不知道“初始化记录器”是什么意思,但要开始和停止录制,您需要对片段调用
startRecording()
stopRecording()
。请参见第3b步。@Commonware是的,明白了,先生,我已经看过您的演示并查看了上面的代码,这是您的代码的一个副本,我做了一些调整,我改变的是,删除了操作栏并在主要活动上放置了按钮。它在我的HTC上工作得非常好,但是昨天我在s3上测试,仍然在s3上,只是没有显示摄像头预览,不管你的演示如何工作都很好。请检查图片,我无法进一步帮助您。您需要确定演示和代码之间有什么不同,然后慢慢修改其中一个,使其看起来像另一个,直到问题发生变化。
<style name="AppBaseTheme" parent="android:style/Theme.Light.NoTitleBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->

    <!-- <item name="android:background">@drawable/main_bg</item> -->
    <!-- <item name="android:windowBackground">@drawable/main_bg</item> -->
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
</style>