PurePDF ImageElement throwing errors - actionscript-3

Hey so I am trying to create an air app that generates a PDF for the user to save but am running into issues with PurePDF. Whenever I run the ImageElement.getInstance() method I am returned a runtime error:
Error: Error #2030: End of file was encountered. at flash.utils::ByteArray/readUnsignedByte()
I am still just in the testing stage and am not evening doing anything crazy. This is what my code looks like:
var bd:BitmapData = new BitmapData( 1024,768 );
bd.draw(pdfClip); //A simple movieclip on the stage containing an image
var bytes:ByteArray = bd.getPixels(new Rectangle(1024,768));
var image:ImageElement = ImageElement.getInstance( bytes );
I would be grateful if anyone that has used purePDF can offer any advice, the documentation is extremely limiting.

You should use the "getBitmapDataInstance" instead, because the "getInstance" method is expecting a png encoded bytearray.
ImageElement.getBitmapDataInstance( bitmap );
see also this example:
https://code.google.com/p/purepdf/source/browse/examples/src/ImageBitmapData.as

Related

bitmapdata.draw failing silently

I am trying to read an image using Loader (variable name is faceimage) and trying to pass the bitmapdata of that image to a function called detect. However, it is failing in the line where I am trying to get the Bitmap.
bmpTarget = new Bitmap( new BitmapData( faceImage.width, faceImage.height, false ) )
bmpTarget.bitmapData.draw( faceImage ); // Fails, no errors shown
detector.detect( bmpTarget.bitmapData );
I narrowed down to this line by putting trace statements above and below the failing line. The faceImage contains valid data which I verified by displaying contents on screen. I also tried
bmpTarget = Bitmap(BitmapData(faceImage.content))
but in vain. Am I doing something wrong here?
It could be a sandbox/crossdomain issue. Certain crossdomain settings prohibit drawing the content of a loaded image to a bitmapData. You can get around it by loading the raw image data with URLLoader and then using loadBytes on Loader.
As noted from:
Why do Loader objects kill bitmapdata draw();?
You likely need a LoaderContext.
loader.load("http://www.example.com/myimage.jpg", new LoaderContext(true));
Otherwise, you can load images from other sites but not access the actual bitmapData, which a draw() requires.
I wonder if you're trying to access the bitmapdata before it's been loaded?
Perhaps try using a complete listener...
var _urlRequest:URLRequest = new URLRequest("urlToImage");
var faceImage:Loader = new Loader;
faceImage.load(_urlRequest);
faceImage.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void{ trace(e) });
faceImage.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded, false, 0, true);
function imageLoaded(e:Event):void {
faceImage.contentLoaderInfo.removeEventListener(Event.COMPLETE, imageLoaded);
var bmpTarget:Bitmap = e.target.content;
detector.detect( bmpTarget.bitmapData );
}

Trying to connect to AMFPHP - NetConnection.connect() returns TypeError: Error #1009

UPDATE: Now I've moved the AMFConnection var declaration to outside the functions in Main, and commented out some trace() commands, and now it gives new errors:
Error #2044: Unhandled NetStatusEvent:. level=error, code=NetConnection.Call.BadVersion
at AMFConnection/init()[/Users/Jan/Downloads/amfphp1/AMFConnection.as:32]
at AMFConnection()[/Users/Jan/Downloads/amfphp1/AMFConnection.as:23]
at Main/testConnection()[/Users/Jan/Downloads/amfphp1/Main.as:14]
at Main()[/Users/Jan/Downloads/amfphp1/Main.as:10]
All of these essentially point to AMFConnection's NetConnection initialisation: _netConnection = new NetConnection(); (where _netConnection is declared at the beginning of the class)
I'm trying to connect to AMFPHP on a server (with Flash AS3), and the swf borks when it reaches the .connect() stage. To make things easier (?) and more reusable (?), I've put all the NetConnection mechanics into a separate class, AMFConnection, which I call from the Main document class like this (details changed):
public function testConnection(e:*=null):void {
var conn:AMFConnection = new AMFConnection();
conn.table = "some_table";
conn.selections = "*";
conn.conditionals = "WHERE something = 'something'";
conn.service = "QueryAMF";
conn.method = "makeQuery";
conn.displayText = txt;
conn.gogogo("http://www.someplace.com/Amfphp");
}
AMFConnection actually starts the connection and calls the AMFPHP service with the function gogogo(), and here's where the connect() NetConnection function just won't work. Here's the main section of the AMFConncection class
private var _netConnection:NetConnection;
private var _responder:Responder;
function AMFConnection()
{
init();
}
private function init(e:* = null)
{
_netConnection = new NetConnection();
_responder = new Responder(uponResult);
}
public function gogogo(url:String):void {
trace(url);
_netConnection.connect(url);
_netConnection.call(String(service+"/"+method), new Responder(onResult, null), table, selections, conditionals);
}
A quick debug session reveals the below errors:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at AMFConnection/gogogo()[AMFConnection.as:44]
at Main/testConnection()[Main.as:20]
at Main()[Main.as:8]
Where: Main.as:20 = conn.gogogo(...), and AMFConnection.as:44 = _netConnection.connect(url);
It also fails to display the stage, instead showing the loading dots. Now, eventually I'm going to move this application to the same server as the AMFPHP service, but even when I try it there with a relative url, instead of an absolute one, it still breaks down at connect(). I've tried changing the publish settings from local only to network only, to no avail.
Any clues? Know a better way to connect to AMFPHP locally?
Cheers in advance!
JB
P.S. Post updated, see top of page.
first, i prefer to use a php file which contains my sql and params. but hey...
The most obvious reason why you might get this error would be a fault in the url i guess. I believe that the standaard gateway.php is written without a capital G. and does not situate itself in the core folder but in the amfphp folder. but then again I don't know what you have altered.
Your _netConnection must be null, and you call connect() method on null reference, so you finish with NullPointerException. Show us how you initialize _netConnnection :).
Ok, I basically remade the thing, and after a couple of hours, it decided to work. I'm not sure how, but... eh.
Thanks all for your help

How can I be receving an error on frame 1 and line 23 when there is no code on that line?

I'm getting an error when I click a button which brings me to frame 2. The error is:
Warning: No libraries were linked as Runtime Shared Libraries (RSLs) because of your publish settings: AIR Android
[SWF] Lemonade-boy.swf - 862607 bytes after decompression
TypeError: Error #1034: Type Coercion failed: cannot convert 0 to flash.display.DisplayObject.
at fl.motion::AnimatorBase/play()
at fl.motion::AnimatorBase$/processCurrentFrame()
at fl.motion::AnimatorBase/handleEnterFrame()
at flash.display::MovieClip/nextFrame()
at Lemonade_fla::MainTimeline/start_now()[Lemonade_fla.MainTimeline::frame1:23]
The Code for Frame 1 is (If I can count correctly that is only 18 lines... not 23+):
//import flash.events.Event;
stop();
//var bg1_menue:Sound = new Sound();
//bg1_menue.load(new URLRequest("/bg1_menu/bg1_menu.mp3"));
//bg1_menue.play();
var cash1:Number = 5.00;
var maximum_storage:Number = 500;
var lemons1:Number = 10;
//var storage1:Number = lemons1;
var cups:Number = 10;
var straws:Number = 100;
var ice:Number = 5;
var drinks:Number = 0;
startbt.addEventListener(MouseEvent.CLICK, start_now);
function start_now(event:MouseEvent):void{
nextFrame();
}
CAN SOMEONE PLEASE EXPLAIN HOW TO FIX THIS ERROR? Thank you!
is this all the code you have? the first frame has everything listed above and there is a second frame(which should exist) which has no code in it?
EDIT: The code above works perfectly fine, you're error is coming from somewhere else.
On the side, you shouldn't create a new thread for the same error:
Tracing flash to a line which doesn't exist, flash CS5/AS3
Error #1034 while running flash application
just put the entire question in one thread, or people will be discouraged from answering.

flex: Create a screenshot of UI element

I want to make a screenshot of custom as3 ui element (which was extended from Canvas), and then upload the image on the server.
Could you help me with a small example of this?
I am trying to do the following:
// This is UI component extended from Canvas
var chessBoard:ChessBoard = new ChessBoard();
// I displayed pieces on the board, but didn't add it to the stage (I just need a
// a screenshot, not need them on the canvas)
chessBoard.displayPosition(DataStorage.getInstance().getStoredPosition(0));
var img:ImageSnapshot = ImageSnapshot.captureImage(chessBoard, 300, new PNGEncoder());
var obj:Object = new Object();
obj.complexity = complexity.value;
obj.img = img;
httpService.send(obj);
and getting this error:
ArgumentError: Error #2015: Invalid
BitmapData. at
flash.display::BitmapData() at
mx.graphics::ImageSnapshot$/captureBitmapData()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\graphics\ImageSnapshot.as:186]
at
mx.graphics::ImageSnapshot$/captureImage()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\graphics\ImageSnapshot.as:282]
at
ui::SendForm/send()[/Users/oleg/Documents/chess_problems/problemme/src/ui/SendForm.mxml:53]
at
> ui::SendForm/___SendForm_Button1_click()[/Users/oleg/Documents/chess_problems/problemme/src/ui/SendForm.mxml:16]
This error was caused by missing width and height of the Canvas. I set them and it helped to create bitmap data this way:
var bitmapData:BitmapData = new BitmapData(chessBoard.width,chessBoard.height);
var encoder:PNGEncoder = new PNGEncoder();
var data:ByteArray = encoder.encode(bitmapData);
I wonder how do I send the image to the server via httpService? Is there a way. Also is data = image file?
Here is a post on someone who has done this exact thing in AS3:
Dynamically Create an Image in Flash and Save it to the Desktop or Server
It is possible, and although I have never done it, this may be useful to you: http://www.flash-db.com/Tutorials/snapshot/

AS3: Can't Deserialize Object from ByteArray .. Error #2006

I am trying to serialize & deserialize Vector. using ByteArray
Here is my code:
public static function serializeToString(value:Object):String{
if(value==null){
throw new Error("null isn't a legal serialization candidate");
}
var bytes:ByteArray = new ByteArray();
bytes.writeObject(value);
bytes.position = 0;
return Base64.encodeByteArray( bytes ).toString();
}
public static function readObjectFromStringBytes(value:String):Object{
var result:ByteArray= Base64.decodeToByteArray(value);
result.position=0;
return result.readObject();
}
No matter what I do, I keep having this error:
RangeError: Error #2006: The supplied index is out of bounds.
at flash.utils::ByteArray/readObject()
I am dipertly looking for a solution ..
Thanks in advance
Sameer
I found the problem reason after days of frustration ..
The loader SWF published for player 9, the content SWF published for player 10,
I simply republished the loader for player 10, and the Error #2006 disappeared from the loaded SWF
What I wonder about, is that the content uses Vector. type, which is player 10 feature,
how come it played correctly inside an older loader ?! Why there is no descriptive regarding different player versions ??
Try getting rid of the .position = 0 lines. They aren't necessary and the second one may be what is causing your issue. Also note that toString() is unecessary, as encodeByteArray already returns a String.
In case you want to send or receive the serialized object graph to server-side code without converting to a string first, see this link.