I am making the following call in my blackberry application (API ver 4.5)...
public void annotate(String msg, EncodedImage ei)
{
Bitmap bitmap = ei.getBitmap();
Graphics g = new Graphics(bitmap);
g.drawText(msg,0,0);
}
And I keep getting an IllegalArgumentException when I instantiate the Graphics object. Looking at the documentation for Graphics is confusing as it leaves many things unstated.
What does it mean by 'default type of the device'?
How do you know if the type of 'bitmap' is not supported? Does this mean that there are different types of bitmaps? Can different encodedImages generate different types of bitmaps?
Is there another way to add my string to the associated encoded image?
public Graphics(Bitmap bitmap)
Constructs a Graphics object for drawing to a bitmap.
Parameters:
bitmap - Bitmap to draw into. Must be Bitmap.COLUMNWISE_MONOCHROME or the default type of the device.
Throws:
IllegalArgumentException - If the type of 'bitmap' is not supported, or the bitmap is readonly.
Are you sure that your Bitmap is mutable? You can't create Graphics objects from immutable Bitmaps. That is one cause of an IllegalArgumentException. You can set the decode mode for your EncodedImage (EncodeImage.setDecodeMode). There are different modes that allow you to specify whether the file is native or readonly...along with other modes that can be combined.
The size of the bitmap might be another IllegalArgumentException. Of course, this is relevant to the target device.
I'd imagine that the default type depends on the graphics chip and hardware. (If you have a monochrome screen, the default would probably be different than if you had a color one.)
Bitmap has a static method getDefaultType(), which "Queries the default Bitmap type for the device". There's also a non-static method getType(). It seems to be telling you the rule is that for the code above to work then either:
bitmap.getType() == Bitmap.getDefaultType()
...or...
bitmap.getType() == COLUMNWISE_MONOCHROME
And presumably neither of these conditions are true. You can do a sanity check on that, and maybe print out the result of getDefaultType() so you know what your target is.
Looks like you'll have to convert the bitmap or get it from somewhere else.
The Graphics object isn't normally constructed explicitly. Rather, you are given an instance of it in the paint() method, if you've overridden it.
I suspect what you want to do is create a subclass of BitmapField and override the paint() method to include your code for drawing text on the bitmap.
Related
I want to be able to encode video frames using Media Foundation IMFTransform for H264 Video Encoding. That's easily doable in Win32, where you can use MFTEnumEx to enumerate the transforms and find the H264 encoder.
However, on WinRT (Store Apps), I can't find a way to instantiate.
I've noticed there's a class CMSH264EncoderMFT, but there's no definition for the CLSID to use on CoCreateInstance.
With:
CoCreateInstance(CLSID_CMSH264EncoderMFT, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IUnknown), (void **)&pUnknown);
CLSID_CMSH264EncoderMFT is undefined for WinRT apps.
And tried:
ComPtr<CMSH264EncoderMFT> encoder = Make<CMSH264EncoderMFT>();
It says the class CMSH264EncoderMFT is incomplete, and says "use of undefined type 'CMSH264EncoderMFT'". Don't even know if the syntax for Make is correct or appropriate...
Does anyone have a clue on how to do this for WinRT?
Use MFCreateSinkWriterFromURL to create a file writer first. Then, use MFCreateMediaType to create an IMFMediaType. Set up its properties, one of which will be the output format: use SetGUID method on the media type with MF_MT_SUBTYPE guid and specify MFVideoFormat_H264 as the argument. Finally, use AddStream method on the sink writer to set the media type to it.
There's an example here (you'll need to modify it a bit when it sets MF_MT_SUBTYPE).
you cannot instantiate object via CMSH264EncoderMFT because it DOES NOT have some interfaces which MUST have object in WinRT for example IInspectable - Provides functionality required for all Windows Runtime classes. CMSH264EncoderMFT IS NOT WinRT class. You can try resolve your task by function MFCreateSinkWriterFromMediaSink - this function takes an object with interface IMFMediaSink. It is possible write code for object with IMFMediaSink interface and receive samples from IMFTransform::ProcessOutput. I just point your attention - you cannot instantiate in WindowsStore code objects which IS NOT Windows Runtime classes.
Regards,
Evgeny Pereguda
so I've recently had problems with using hitTestObject. I did a bit of research about hitTestPoint and that is too exact. And, using the bitmap is a bit too complicated for me.
Is there any other user-made collision detection functions? If so, how would I use it? I am trying to test if the object eChar collides with an objecy called bd1. I've found one, but that required my code to be in a class, which it isn't (it's programmed in the action window).
A very popular API is PixelPerfectCollisionDetection.
All you do is import it (import PixelPerfectCollisionDetection;) and check collisions like:
if(PixelPerfectCollisionDetection.isColliding(eChar, bd1, commonParent, pixelPrecise (set to True for Pixel detections), tolerance:Number = 0 (for testing if testing for same Alpha) )) which either returns true or false
I'm usin Flash Builder to create some actionscript code that uses SharedObjects.
First question: how can I delete my local SharedObject in Flash Builder? I am debugging my program and the SharedObject sems to persist between runs. I want to start fresh and clean with no SharedObject storing my data. How do I get rid of it?
Also, in my SharedObject, I used mySharedObject.data["mykey"] to store a Dictionary. This Dictionary will have String keys and values of MyCustomClass. The problem is that when I later try to loop over the values of this Dictionary, I get error #1034 cannot convert object to type MyCustomClass. It seems like I can put an item of type MyCustomClass into this dictionary, but I can't get the item back out as anything other than an object.
Any idea what is going wrong?
Those are essentially two questions, so should have been asked as two questions. Anyway, I'd answer them here but still prefer that you break them up in two parts (possibly leave a link to the other one here for reference sake):
Local shared object, are useful exactly for persistence across runs. And then there's SharedObject.clear() to clear the state as required.
For you second issue, Shared Object's serialize your object into AMF, so that it can be written to disk or sent over network using RTMP. Now, your custom class can't really be serialized in AMF. What actually happens is that the public properties (and dynamic ones, if the class is declared dynamic) are serialized into the structure. So, the public data is stored... but it's essentially a general Object.
To work around that, you can have a public static readFrom(object:Object):MyCustomClass type function, which would read the properties from the passed object to construct a new MyCustomClass representing that information.
There are ways to register your class with the player to be stored in SharedObject (see here)... but you need to make sure that the code that de-serializes that data is aware of the class as well.
To make a class available for conversion, in your global initialization use registerClassAlias() call with MyCustomClass and its fully qualified name as parameters. The manual. Say your custom class is foo.bar.TheClass, you write:
registerClassAlias('foo.bar.TheClass',foo.bar.TheClass);
In order to drop old SO use delete call against so.data["mykey"] and do so.flush(). Edit: SharedObject.clear() is way better.
1/ Being persistent is one of the particularity of a SharedObject. To cleanup all its content, you need to call the clear method.
var shareObject:SharedObject = SharedObject.getLocal('justatest');
shareObject.data.test = 'test';
trace(shareObject.data.test)
shareObject.clear();
trace(shareObject.data.test)
output
test
undefined
2/ To store complex data types in SO, you need to use flash.net.registerClassAlias (example here)
I have an application which pulls in Bitmap resources from a server - currently I use the Loader class to do this, then, once they're loaded, generate a BitmapData based on the loader dimensions and draw the instance of Loader directly to it (the BitmapData is used for Away3D textures as well as Bitmap instances, so I have no need for the Loader once fetched).
This has always worked for me, but recently I started getting 0x0 Loaders, and invalid BitmapData as a result.
I stopped doing this:
this.imageBitmap = new BitmapData(this.imageLoader.width, this.imageLoader.height, true, 0);
..and started doing this:
this.imageBitmap = new BitmapData(event.target.content.width, event.target.content.height, true, 0);
Where event is the Event.COMPLETE event fired by the loader. This fixed the dimension problem, but the BitmapData is just a plain white bitmap (and it's set to transparent by default, so this is being drawn into it). Frustratingly, this doesn't happen every time, if I refresh the application it works as it should around 25% of the time, otherwise it plays up like this.
I've got a tight deadline and I'm really screwing about this, if anyone could help or suggest a better way of doing it you'd really be saving my neck!
Sounds like you need to adjust the image decoding policy for the loader - to ensure it decodes the image before COMPLETE fires - then the width and height etc should be reliable.
To do it, just add a suitable LoaderContext object to the Loader.load method:
var loaderContext:LoaderContext = new LoaderContext();
//set decode policy
loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD;
//load image
loader.load(yourUrl, loaderContext);
The default decode policy is ImageDecodingPolicy.ON_DEMAND - which doesnt decode the image until it is actually required.
Lang docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/ImageDecodingPolicy.html
Fixed it, stupid oversight and bit of an obscure situation but I'll leave an answer in case anyone runs into something similar.
My loader is contained in an Asset class - when another object requires the internal bitmap, it queries this class - if the bitmap's present, it returns it, if not it loads it with a loader and registers a callback for a COMPLETE event which is fired when the Loader has loaded and transferred its contents to a BitmapData instance.
The stupid mistake I'd made was that, if several objects were querying the same (as-yet-unloaded) asset, it would start reloading the asset each time, creating a new Loader as it did so...so when the first Loader was complete, it would fire an event but no reference to it would exist, not only creating a memory leak but causing the Asset class to extract the BitmapData from the most-recently-created Loader, which was not complete! The asynchronous nature of Loader is the reason it worked sometimes, as on occasion the final Loader would be ready in time for BitmapData extraction.
Simple solution was to create an internal boolean, _isLoading, which is set to true the first time load() is called - any subsequent calls are ignored if it's true, but callbacks still registered, works a treat!
I have an object (MovieClip) on stage at some frame, and at the next frame, even though the object is visually removed, it is still there (it has an internal function that generates something on stage periodically, and the stage objects are being generated). I know I can simply stop the action while leaving the frame, but the object will eat up memory and maybe even CPU time for some background actions (and this is a AIR to iOS project so I care about memory and performance). How can I get rid of the object entirely? I expect everything to be removed when I navigate to another frame if it's a designer-placed object (if the object is not generated by ActionScript) and this is the case, but it only gets visually removed.
Thanks,
Can.
Just null your object reference and it's listeners.
...
myMovieClip.removeListeners();//class function
myMovieClip.parent.removeChild(myMovieClip);
myMovieclip = null;
...