brownemint / Ti-Android-CameraView

MIT License
23 stars 15 forks source link

Android Camera Module

Description

Titanium View with the background image sourced directly from the Android camera.

This module is not actively maintained. If you like using it and want to contribute that would be great, and very much appreciated. If you are looking for more features, check out flashcam from Ricardo Pereira. Ricardo has cloned version 0.6.4 and will be adding methods for flash, torch, and possibly much more.

Accessing the custom-android-camera Module

To access this module from JavaScript, you would do the following:

var androidcamera = require("pw.custom.androidcamera");

The androidcamera variable is a reference to the Module object.

Reference

The cameraview takes the same properties and methods as the standard titanium view.

createCameraView (method)

Create a new instance of the camera view. The method takes a dictionary of the following arguments:

arguments

takePicture (method)

Capture the image currently on screen and save to the filesystem

picturetaken (event)_

Returns a dictionary containing the following:

Resolution Constants

Usage

The following permissions need to be added to the manifest tag in tiapp.xml

<!-- Camera Permissions -->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>

Then, to use the module in app, do something like the following:

var win = Ti.UI.createWindow({
    navBarHidden: true,
    fullscreen: true,
    backgroundColor:'white'
});
win.orientationModes = [Ti.UI.PORTRAIT];
win.open();

function showCamera() {
    var androidcamera = require("pw.custom.androidcamera");
    var camera = androidcamera.createCameraView({
        save_location: "my_app",
        useFrontCamera: true,
        pictureTimeout: 200,
        resolutionNamed: androidcamera.RESOLUTION_480
    });

    var btSnap = Ti.UI.createButton({
        title: "Capture",
        bottom: "10dp",
        height: "80dp",
        width: "80dp",
        zIndex: 2
    });

    btSnap.addEventListener("click", function(){
        camera.snapPicture();
    });

    camera.addEventListener("picture_taken", function(evt){
        alert("Image saved to "+evt.path);
    });

    win.addEventListener("close", function(){
        camera = null;
    });

    win.add(camera);
    win.add(btSnap);
}

if( Ti.Media.isCameraSupported ) {
    if (Ti.Media.hasCameraPermissions()) {
        showCamera();
    } else { 
        Ti.Media.requestCameraPermissions(function(e) {
            if (e.success === true) {
                showCamera();
            } else {
                alert("Access denied, error: " + e.error);
            }
        });
    }
} else {
    alert("No camera found!");
}

Changelog

Version 0.6.6: Updated to support Ti SDK 6.0.1.GA at a minimum

Version 0.6.3: Added the ability to change the desired resolution for the camera (and match it with the preview) when the view is created.

Version 0.6.1: Added ability to restart preview after x number of milliseconds after a picture is taken.

Version 0.5: Removed potential crash when the app is resumed while the camera view is open. Added ability to pick camera when creating view (useFrontCamera).

Author

Michael Browne Email @brownemint

Notes