Cordova相机插件为相机创建半屏,另一半用于选择菜单

时间:2016-06-05 20:02:36

标签: cordova cordova-plugins

我正在尝试使用cordova相机插件实现Instagram,如相机界面(半屏是相机和其他haalf是选择菜单)。任何人都可以帮我吗?无法找到一个好的来源

1 个答案:

答案 0 :(得分:1)

您需要实现混合视图, check this article

我是通过CordovaWebView实现了一个JAVA SurfaceView(在其中实现摄像头)。

为了给你指路:

在插件的JavaScript中有类似的内容:

SurfaceViewAdd:function(){
    cordova.exec(
        function(){//success function},function(){//error function},'MYPlugin',action,JSON[])
},

在您的cordova项目中安装插件后,您可以调用此JavaScript函数来调用JAVA本机代码。

在JAVA方面,MainActivity:

public class CordovaApp extends CordovaActivity
{


    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        super.init();
        super.loadUrl(Config.getStartUrl());

        MYPlugin.setCwv(this.appView);
);

....plus all function you might need (to release the camera onPause event for example)..

}

你的插件类:

public class MYPlugin extends CordovaPlugin {


    public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
///Manage the input of your plugin from Javascript
//call functions inside your plugin class to implement a SurfaceView on top of the CordovaWebView
//Send back a successful callback to your JavaScript once the webview is implemented
        }

... ///all you need to implement the surfaceView and the cameraView inside the surfaceView ...

        public static void setCwv(CordovaWebView cwvInput) {
        cwv = cwvInput;
        }
}

你需要一个插件XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
    id="com.you.myplugin"
    version="1.0.0">
    <name>MyPlugin</name>
    <description> ... </description>
    <license> ... </license>
    <author> ... </author>

    <engines>
        <engine name="cordova" version=">=3.0.0" />
    </engines>

    <js-module src="www/js/my_plugin.js" name="MyPlugin">
        <clobbers target="MyPlugin" />
    </js-module>

    <platform name="android">
        <config-file target="res/xml/config.xml" parent="widget">
            <preference name="orientation" value="portrait"/>
            <feature name="MyPlugin" >
                <param name="android-package" value="com.you.myplugin.MyPlugin"/>
                <param name="onload" value="true"/>
            </feature>
        </config-file>

        <config-file target="AndroidManifest.xml" parent="/*">
            <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
            <uses-permission android:name="android.permission.CAMERA" />
            <uses-feature android:name="android.hardware.camera.autofocus" />
            <uses-feature android:name="android.hardware.camera" />
        </config-file>

        <source-file src="src/android/MyPlugin.java" target-dir="src/com/you/myplugin" />

    </platform>
</plugin>

花点时间阅读文章并祝你好运。