Last active
December 4, 2017 17:30
-
-
Save marchbold/ce93ed72a5fb60db045e6168362d5508 to your computer and use it in GitHub Desktop.
Camera v4 ANE Preview Frame Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* __ __ __ | |
* ____/ /_ ____/ /______ _ ___ / /_ | |
* / __ / / ___/ __/ ___/ / __ `/ __/ | |
* / /_/ / (__ ) / / / / / /_/ / / | |
* \__,_/_/____/_/ /_/ /_/\__, /_/ | |
* / / | |
* \/ | |
* http://distriqt.com | |
* | |
* @author "Michael Archbold (ma@distriqt.com)" | |
* @copyright http://distriqt.com/copyright/license.txt | |
*/ | |
package com.distriqt.camera.starling.ui | |
{ | |
import com.distriqt.extension.camera.device.CameraDevice; | |
import com.distriqt.extension.camera.events.CameraEvent; | |
import flash.display.BitmapData; | |
import flash.display3D.textures.Texture; | |
import flash.geom.Matrix; | |
import flash.geom.Rectangle; | |
import flash.utils.ByteArray; | |
import starling.display.Image; | |
import starling.display.Sprite; | |
import starling.textures.Texture; | |
/** | |
* | |
*/ | |
public class CameraPreview extends Sprite | |
{ | |
//////////////////////////////////////////////////////// | |
// CONSTANTS | |
// | |
//////////////////////////////////////////////////////// | |
// VARIABLES | |
// | |
private var _device : CameraDevice; | |
private var _preview : Image; | |
private var _previewTexture : starling.textures.Texture; | |
private var _previewData : ByteArray; | |
private var _previewBitmapData : BitmapData; | |
private var _previewBitmapRect : Rectangle; | |
private var _lastFrameProcessed : Number = -1; | |
//////////////////////////////////////////////////////// | |
// FUNCTIONALITY | |
// | |
/** | |
* Constructor | |
*/ | |
public function CameraPreview() | |
{ | |
super(); | |
_previewData = new ByteArray(); | |
_previewBitmapData = new BitmapData( 1, 1, false ); | |
_previewBitmapRect = new Rectangle(0,0,1,1); | |
_previewTexture = starling.textures.Texture.fromBitmapData( _previewBitmapData ); | |
_preview = new Image( _previewTexture ); | |
} | |
public function useDevice( device:CameraDevice ):void | |
{ | |
if (_device != null) removeDevice(); | |
_device = device; | |
_device.addEventListener( CameraEvent.VIDEO_FRAME, camera_videoFrameHandler ); | |
addChild( _preview ); | |
} | |
public function removeDevice():void | |
{ | |
if (_device != null) | |
{ | |
_device.removeEventListener( CameraEvent.VIDEO_FRAME, camera_videoFrameHandler ); | |
_device = null; | |
} | |
if (contains(_preview)) removeChild(_preview); | |
} | |
//////////////////////////////////////////////////////// | |
// INTERNALS | |
// | |
//////////////////////////////////////////////////////// | |
// EVENT HANDLERS | |
// | |
private function camera_videoFrameHandler( event:CameraEvent ):void | |
{ | |
var frame:Number = _device.receivedFrames; | |
if (frame != _lastFrameProcessed) | |
{ | |
if (-1 != _device.getFrameBuffer( _previewData )) | |
{ | |
try | |
{ | |
// | |
// Check we have an appropriately sized bitmapdata and texture | |
// Recreate if not | |
if (_previewBitmapData.width != _device.width || _previewBitmapData.height != _device.height) | |
{ | |
trace( "resizing to: (" + _device.width +", "+ _device.height +")" ); | |
_previewBitmapData.dispose(); | |
_previewTexture.dispose(); | |
_previewBitmapData = new BitmapData( _device.width, _device.height, false ); | |
_previewTexture = starling.textures.Texture.fromBitmapData( _previewBitmapData ); | |
_preview.texture = _previewTexture; | |
_preview.readjustSize(); | |
_previewBitmapRect = new Rectangle( 0, 0, _previewBitmapData.width, _previewBitmapData.height ); | |
var s:Number = 1.0; | |
var t:Matrix = new Matrix(); | |
if (_device.info.orientation == 90 || _device.info.orientation == 270) | |
{ | |
// s = stage.stageWidth / _previewTexture.height; | |
t.translate( - _previewTexture.width * 0.5, - _previewTexture.height * 0.5 ); | |
t.rotate( DEG2RAD(_device.info.orientation) ); | |
t.scale( s, s ); | |
t.translate( _previewTexture.height * s * 0.5, _previewTexture.width * s * 0.5 ); | |
} | |
else | |
{ | |
// s = stage.stageWidth / _previewTexture.width; | |
t.translate( - _previewTexture.width * 0.5, - _previewTexture.height * 0.5 ); | |
t.rotate( DEG2RAD(_device.info.orientation) ); | |
t.scale( s, s ); | |
t.translate( _previewTexture.width * s * 0.5, _previewTexture.height * s * 0.5 ); | |
} | |
// Something weird happens here with large bitmaps and it turns white? If you comment out this line it works but isn't rotated... | |
_preview.transformationMatrix = t; | |
} | |
// | |
// Update the bitmapdata and texture | |
_previewBitmapData.setPixels( _previewBitmapRect, _previewData ); | |
// For Starling 1.x | |
// flash.display3D.textures.Texture(_previewTexture.base).uploadFromBitmapData( _previewBitmapData ); | |
// For Starling 2.x | |
flash.display3D.textures.RectangleTexture(_previewTexture.base).uploadFromBitmapData( _previewBitmapData ); | |
} | |
catch (e:Error) | |
{ | |
trace( e ); | |
} | |
finally | |
{ | |
_previewData.clear(); | |
_lastFrameProcessed = frame; | |
} | |
} | |
} | |
} | |
private function DEG2RAD( degrees:Number ):Number | |
{ | |
return degrees * Math.PI / 180; | |
} | |
} | |
} | |
// com.distriqt.Camera |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello can i talk with you ?