Copy link to clipboard
Copied
Hi there,
i really need som help here. I cant seem understand how to resize an still image taken with the camera. Here are the code so far(also with the upload part). I just need a thumbnail to be uploaded to the server, not the HQ-image. Any ideas?
/*
Simple AIR for iOS Package for selecting a cameraroll photo or taking a photo and processing it.
Copyright 2012 FIZIX Digital Agency
For more information see the tutorial at:
http://www.fizixstudios.com/labs/do/view/id/air-ios-camera-and-uploading-photos
Notes:
This is a barebones script and is as generic as possible. The upload process is very basic,
the tutorial linked above gives information on how to post the image along with data to
your PHP script.
The PHP script will collect as $_FILES['Filedata'];
*/
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.media.Camera;
import flash.media.CameraUI;
import flash.media.CameraRoll;
import flash.media.MediaPromise;
import flash.media.MediaType;
import flash.events.MediaEvent;
import flash.events.Event;
import flash.events.ErrorEvent;
import flash.utils.IDataInput;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.utils.ByteArray;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.errors.EOFError;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
// Define properties
var cameraRoll:CameraRoll = new CameraRoll(); // For Camera Roll
var cameraUI:CameraUI = new CameraUI(); // For Taking a Photo
var dataSource:IDataInput; // Data Source
var tempDir; // Our temporary directory
CameraTest() ;
function CameraTest()
{
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
// Start the home screen
startHomeScreen();
}
// =================================================================================
// startHomeScreen
// =================================================================================
function startHomeScreen()
{
trace("Main Screen Initialized");
// Add main screen event listeners
if(Multitouch.supportsGestureEvents)
{
mainScreen.startCamera.addEventListener(TouchEvent.TOUCH_TAP, initCamera);
mainScreen.startCameraRoll.addEventListener(TouchEvent.TOUCH_TAP, initCameraRoll);
}
else
{
mainScreen.startCamera.addEventListener(MouseEvent.CLICK, initCamera);
mainScreen.startCameraRoll.addEventListener(MouseEvent.CLICK, initCameraRoll);
}
}
// =================================================================================
// initCamera
// =================================================================================
function initCamera(evt:Event):void
{
trace("Starting Camera");
if( CameraUI.isSupported )
{
cameraUI.addEventListener(MediaEvent.COMPLETE, imageSelected);
cameraUI.addEventListener(Event.CANCEL, browseCancelled);
cameraUI.addEventListener(ErrorEvent.ERROR, mediaError);
cameraUI.launch(MediaType.IMAGE);
}
else
{
mainScreen.feedbackText.text = "This device does not support Camera functions.";
}
}
// =================================================================================
// initCameraRoll
// =================================================================================
function initCameraRoll(evt:Event):void
{
trace("Opening Camera Roll");
if(CameraRoll.supportsBrowseForImage)
{
mainScreen.feedbackText.text = "Opening Camera Roll.";
// Add event listeners for camera roll events
cameraRoll.addEventListener(MediaEvent.SELECT, imageSelected);
cameraRoll.addEventListener(Event.CANCEL, browseCancelled);
cameraRoll.addEventListener(ErrorEvent.ERROR, mediaError);
// Open up the camera roll
cameraRoll.browseForImage();
}
else
{
mainScreen.feedbackText.text = "This device does not support CameraRoll functions.";
}
}
// =================================================================================
// imageSelected
// =================================================================================
function imageSelected(evt:MediaEvent):void
{
mainScreen.feedbackText.text = "Image Selected";
// Create a new imagePromise
var imagePromise:MediaPromise = evt.data;
// Open our data source
dataSource = imagePromise.open();
if(imagePromise.isAsync )
{
mainScreen.feedbackText.text += "Asynchronous Mode Media Promise.";
var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
eventSource.addEventListener( Event.COMPLETE, onMediaLoaded );
}
else
{
mainScreen.feedbackText.text += "Synchronous Mode Media Promise.";
readMediaData();
}
}
// =================================================================================
// browseCancelled
// =================================================================================
function browseCancelled(event:Event):void
{
mainScreen.feedbackText.text = "Browse CameraRoll Cancelled";
}
// =================================================================================
// mediaError
// =================================================================================
function mediaError(event:Event):void
{
mainScreen.feedbackText.text = "There was an error";
}
// =================================================================================
// onMediaLoaded
// =================================================================================
function onMediaLoaded( event:Event ):void
{
mainScreen.feedbackText.text += "Image Loaded.";
readMediaData();
}
// =================================================================================
// readMediaData
// =================================================================================
function readMediaData():void
{
mainScreen.feedbackText.text += "Reading Image Data.";
var imageBytes:ByteArray = new ByteArray();
dataSource.readBytes( imageBytes );
tempDir = File.createTempDirectory();
// Set the userURL
var serverURL:String = "http://www.hidden_in_this_example.com/upload.php";
// Get the date and create an image name
var now:Date = new Date();
var filename:String = "IMG" + now.fullYear + now.month + now.day + now.hours + now.minutes + now.seconds;
// Create the temp file
var temp:File = tempDir.resolvePath(filename);
// Create a new FileStream
var stream:FileStream = new FileStream();
stream.open(temp, FileMode.WRITE);
stream.writeBytes(imageBytes);
stream.close();
// Add event listeners for progress
temp.addEventListener(Event.COMPLETE, uploadComplete);
temp.addEventListener(IOErrorEvent.IO_ERROR, ioError);
// Try to upload the file
try
{
mainScreen.feedbackText.text += "Uploading File";
//temp.upload(new URLRequest(serverURL), "Filedata");
// We need to use URLVariables
var params:URLVariables = new URLVariables();
// Set the parameters that we will be posting alongside the image
params.userid = "1234567";
// Create a new URLRequest
var request:URLRequest = new URLRequest(serverURL);
// Set the request method to POST (as opposed to GET)
request.method = URLRequestMethod.POST;
// Put our parameters into request.data
request.data = params;
// Perform the upload
temp.upload(request, "Filedata");
}
catch( e:Error )
{
trace(e);
mainScreen.feedbackText.text += "Error Uploading File: " + e;
removeTempDir();
}
}
// =================================================================================
// removeTempDir
// =================================================================================
function removeTempDir():void
{
tempDir.deleteDirectory(true);
tempDir = null;
}
// ==================================================================================
// uploadComplete()
// ==================================================================================
function uploadComplete(event:Event):void
{
mainScreen.feedbackText.text += "Upload Complete";
}
// ==================================================================================
// ioError()
// ==================================================================================
function ioError(event:Event):void
{
mainScreen.feedbackText.text += "Unable to process photo";
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Thx Mocca,
but i had the impression that setMode just works on movies, not still images. Is that right?
Copy link to clipboard
Copied
1. Create a BitmapData of the correct size of the full image
2. Use BitmapData.setPixels to create pixel data from your byteArray
3. Make a new Bitmap, Bitmap.bitmapData = BitmapData
4. Create a matrix with the correct scaling factors for your thumbnail
5. Create a new BitmapData the size of your thumb
6. Use BitmapData.draw to draw your image data from the Bitmap to the new BitmapData with the scaling matrix
7. Use BitmapData.getPixels to create a bytearray from your thumb BitmapData
8. save it
You'll have to look up the AS3 reference to see how all these methods work.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now