Flex, Problem when jpegencoding two bitmaps in sequence - apache-flex

I am creating this "drawing application", where the user can click "preview" and it will take what they made, draw a bitmap, and then I want to split that bitmap into left and right images. I create the first bitmap, encode it as a jpeg, and then use that to cut the left and right out using copypixels. I then reform the images together with a space inbetween in one canvas, and draw that canvas. Everything works fine up there.
When I go to encode the new bitmap, and then offer it to save out, the saved image is blank. I have tested everything up to that point and it all works fine. I see the images on screen, I can save out the first bitmap fine.. but the second one is always blank. Here is some sample code, possibly someone can help me out.
_mainBmd = new BitmapData(_jacketWidth, _jacketHeight);
_mainBmd.draw(_imageHolder);
startEncode(_mainBmd);
private function startEncode(imageBitmapData:BitmapData):void
{
var encoder:JPEGAsyncEncoder = new JPEGAsyncEncoder(100);
encoder.PixelsPerIteration = 150;
encoder.addEventListener(JPEGAsyncCompleteEvent.JPEGASYNC_COMPLETE, encodeDone);
encoder.encode(imageBitmapData);
}
private function encodeDone(event:JPEGAsyncCompleteEvent):void
{
_leftBmd = new BitmapData(sideWidth, sideHeight);
var lRect:Rectangle = new Rectangle(0,0, sideWidth, sideHeight);
var lPoint:Point = new Point(0,0);
_leftBmd.copyPixels(_mainBmd, lRect, lPoint);
_rightBmd = new BitmapData(sideWidth, sideHeight);
var bWidth:Number = 200;
var sWidth:Number = 111;
var rRectWidth:Number = (bWidth/2 + sWidth) * Constants.print_dpi;
var rRect:Rectangle = new Rectangle(rRectWidth, 0, sideWidth, sideHeight);
var rPoint:Point = new Point(0, 0);
_rightBmd.copyPixels(_mainBmd, rRect, rPoint);
var lbm:Bitmap = new Bitmap(_leftBmd);
var rbm:Bitmap = new Bitmap(_rightBmd);
//now combine the two images into one holder with a space in the middle
//left Image
var l_Image:Image = new Image();
l_Image.source = lbm;
//right image
var r_Image:Image = new Image();
r_Image.source = rbm;
var newRender:Canvas = new Canvas();
newRender.clipContent = false;
newRender.minHeight = 0;
newRender.minWidth = 0;
newRender.addChild(l_Image);
r_Image.x = 500;
newRender.addChild(r_Image);
fcBMD = new BitmapData(renderW, renderH);
fcBMD.draw(newRender);
startEncode2(fcBMD);
}
private function startEncode2(imageBitmapData:BitmapData):void
{
var encoder:JPEGAsyncEncoder = new JPEGAsyncEncoder(100);
encoder.PixelsPerIteration = 150;
encoder.addEventListener(JPEGAsyncCompleteEvent.JPEGASYNC_COMPLETE, encode2Done);
encoder.encode(imageBitmapData);
}
private function encode2Done(event:JPEGAsyncCompleteEvent):void
{
_data = event.ImageData;
}
private function onSaveRenderClick(e:MouseEvent):void //save button listener
{
var fileRef:FileReference = new FileReference();
fileRef.addEventListener(Event.SELECT, onSaveComplete);
fileRef.save(_data, 'testImage.jpg');
}

Is there a special reason why you go trough all the loops of creating a Canvas holder and adding Images?
Why not use copyPixels directly on the final BitmapData that you want to encode:
var backgroundColor:uint = 0xffffff;
fcBMD = new BitmapData(renderW, renderH, false, backgroundColor);
var lPoint:Point = new Point(0,0);
var lRect:Rectangle = new Rectangle(0,0, sideWidth, sideHeight);
fcBMD.copyPixels(_mainBmd, lRect, lPoint);
var rRect:Rectangle = new Rectangle(rRectWidth, 0, sideWidth, sideHeight);
var rPoint:Point = new Point(renderW - sideWidth, 0);
fcBMD.copyPixels(_mainBmd, rRect, rPoint);
startEncode2(fcBMD);

Related

Avatar image clickable and display on screen Xamarin.Forms

I want to make my avatar image which is in a circle clickable. After click it has to be displayed on screen like in FB. So how I can implement this in my code?
listView.Header = "";
listView.HeaderTemplate = new DataTemplate(() =>
{
var avatarLayout = new StackLayout()
{
HeightRequest = 350,
};
var grid = new Grid();
grid.HeightRequest = 400;
grid.BackgroundColor = Color.White;
grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions.Add(new RowDefinition());
var bgrImg = new Image();
bgrImg.Aspect = Aspect.Fill;
bgrImg.Source = ImageSource.FromFile("abstract_background.jpg");
var avImg = new CircleImage();
avImg.VerticalOptions = new LayoutOptions(LayoutAlignment.Center, false);
avImg.Source = ImageSource.FromFile("about_background.png");
grid.Children.Add(bgrImg, 0, 0);
grid.Children.Add(avImg);
Grid.SetRowSpan(avImg, 2);
avatarLayout.Children.Add(grid);
Example of my
avatar
You can make an element tappable by using a GestureRecognizer
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
// handle the tap
};
image.GestureRecognizers.Add(tapGestureRecognizer);

OSMF how to get screenshot

in normal screenshot like this:
bitmapData.draw(video, new Matrix());
but how to get video object in osmf player? I've tried draw mediaPlayerSprite result in a blank image
var pictureData:BitmapData = new BitmapData(width, height);
var x:DisplayObjectTrait = _mediaPlayer.media.getTrait(org.osmf.traits.MediaTraitType.DISPLAY_OBJECT) as DisplayObjectTrait;
var y:Video = x.displayObject as Video;
pictureData.draw(m_container, new Matrix());
var byteArray:ByteArray = new JPEGEncoder(80).encode(pictureData);
var file:FileReference = new FileReference();
file.save(byteArray, "pic.jpeg");
To get the Bitmapdata from the video:
var videoDisplay:VideoDisplay = video.videoDisplay;
var videoObject:Video = videoDisplay.videoObject;
var bitmapData:BitmapData = new BitmapData(videoObject.videoWidth, videoObject.videoHeight, true, 0x000000);

Getting Subgurim Map Marker Position in Button Click Event

I am using Subgurim Map for Setting Marker based on inputed Address. Using GMarker, I am able to load it into Map.
Now this address might be wrong or nearby actual address. So that I am giving opportunity to customer to set actual address with different Marker ( which is bubbled when a button called "Pick Bubble" button )
like,
StringBuilder sb = new StringBuilder();
sb.Append(txtAddress.Text.Trim());
sb.AppendFormat(", {0}", txtCity.Text.Trim());
sb.AppendFormat("- {0}", txtZipcode.Text.Trim());
sb.Append(", Gujarat");
sb.AppendFormat(", {0}", txtCountry.Text.Trim());
GetAddressCordinates googleCordinate = new GetAddressCordinates(Constants.APP_SETTINGS.GoogleMapAPIKey);
Coordinate cordinate = googleCordinate.GetCoordinates(sb.ToString());
GLatLng gLatLng = new GLatLng();
{
gLatLng.lat = double.Parse(cordinate.Latitude.ToString());
gLatLng.lng = double.Parse(cordinate.Longitude.ToString());
}
if (gLatLng != null)
{
GMarker gMarker;
GIcon gIcon = new GIcon();
gIcon.image = ResolveUrl(string.Format("~/Images/ActualFlag.png"));
gIcon.shadow = ResolveUrl(string.Format("~/Images/Shadow.png"));
gIcon.shadowSize = new GSize(30, 30);
gIcon.iconSize = new GSize(24, 24);
GMarkerOptions mOpts = new GMarkerOptions();
mOpts.draggable = true;
mOpts.RaiseOnDrag = true;
mOpts.Animation = GMarkerOptions.AnimationType.Bounce;
mOpts.icon = gIcon;
gMarker = new GMarker(gLatLng, mOpts);
GInfoWindow infoWindow = new GInfoWindow(gMarker, "", false);
GMap1.addInfoWindow(infoWindow);
//GMap1.addGMarker(gMarker);
string jsFunction = string.Format("function(){{ var ev = new serverEvent('dragend', 0); ev.addArg({0}.getPoint().lat()); ev.addArg({0}.getPoint().lng()); ev.send(); }}", gMarker.ID);
GListener listener = new GListener(gMarker.ID, GListener.Event.dragend, "alertame");
GMap1.Add(listener);
GMap1.setCenter(gLatLng);
}
Now I want this two marker position into Button called "NEXT" for manipulate.
How I will get Marker positions ?
Have you tried to put it into GinfoWindow ?
As a string as follows:
dir= "<input type='button' name='next'>"
into:
GInfoWindow infoWindow = new GInfoWindow(gMarker, dir, false);

Flex: access dynamically added RicheEditableText in other function

When I click on a userlist, the function addTab is triggered:
private var counter:int = 0;
public function addTab():void {
var new vBox:VBox = new VBox();
var textBox:RichEditableText = new RichEditableText();
var nameEm:String = "dynamicTextBox" + counter;
textBox.id = nameEm;
counter++;
var textFlow:TextFlow = new TextFlow();
vbox.addChild(textFlow);
vbox.addChild(textBox);
tabNavigator.add(vBox);
}
In another function, I would like to add Rich Text to the newly created TextBox, but I can not access it.
I tried getChildByName(vbox) and vbox.getChildByName(textBox), but that doesn't seem to work.
To get childbyname assigned a name to richeditable text and if you want to get it by counter then give name to vbox also, as in the sample here:
private var counter:int = 0;
public function addTab():void {
var new vBox:VBox = new VBox();
var textBox:RichEditableText = new RichEditableText();
var nameEm:String = "dynamicTextBox" + counter;
textBox.id = nameEm;
vBox.name=nameEm;
textBox.name=nameEm;
counter++;
var textFlow:TextFlow = new TextFlow();
vbox.addChild(textFlow);
vbox.addChild(textBox);
tabNavigator.add(vBox);
}
If you know the index then
var vb:VBox = tabNavigator.getChildByName("dynamicTextBox"+index) as VBox;
var txt:RichEditableText=vb.getChildByName("dynamicTextBox"+index) as VBox;
So this will give you RichEditableText.
Or if you want to get access just after adding in to tab then return RichEditableText from the addtab function.
Elaborating on my comment, the idea is that you do something like this:
private var counter:int = 0;
public function addTab():void {
var new vBox:VBox = new VBox();
vBox.name = "dynVbox" + counter;
var textBox:RichEditableText = new RichEditableText();
textBox.name = "dynamicTextBox" + counter;
var textFlow:TextFlow = new TextFlow();
textFlow.name = "dynamicTextFlow" + counter;
vbox.addChild(textFlow);
vbox.addChild(textBox);
tabNavigator.add(vBox);
counter++;
}
Now, outside this function, you just need to know the counter number of the vBox you want. So for example:
var myBox = tabNavigator.getChildByName('dynVbox'+ 32).getChildByName('dynamicTextBox'+32)
Without seeing more of your code, I don't know if this is the best way to go about it, but if you want to mark your boxes with some dynamically generated name and look them up later, this will work
Example documentation

multiple sprite different name

How do I create multiple instance of the same sprite with different name as in shap1, shape2, shape3, etc. Then the function will change the color of the sprite through getChildByName? Whether I create a new instance, the old sprite will be overwrite by the new sprite and when I call the old "shape1", it will be null. How do I retain all sprite?
var c:UIComponent = new UIComponent();
var s:Sprite = new Sprite();
private function shapeCreate() {
s.graphics.beginFill(0x333333);
s.graphics.drawEllipse(7,35,18,12);
s.graphics.endFill();
s.name = "shape1";
s.addEventListener(MouseEvent.MOUSE_DOWN,chgColorBlue);
s.addEventListener(MouseEvent.MOUSE_UP,chgColorReset);
c.addChild(s);
addElement(c);
}
private function chgColorBlue(e:MouseEvent):void {
e.currentTarget.graphics.beginFill(0x000099);
e.currentTarget.graphics.drawEllipse(7,35,18,12);
e.currentTarget.graphics.endFill();
}
You use the same Sprite instance so when you call the shapeCreate() method it overrides it.
You should define as many Sprites as you need and apply the shape creation to them separately so there will be no override.
var c:UIComponent = new UIComponent();
//Creating the Sprite instances for the shapes
var s1:Sprite = new Sprite();
s1.name = "s1_sp";
var s2:Sprite = new Sprite();
s2.name = "s2_sp";
var s3:Sprite = new Sprite();
s3.name = "s3_sp";
private function shapeCreate(s:Sprite)
{
s.graphics.beginFill(0x333333);
s.graphics.drawEllipse(7,35,18,12);
s.graphics.endFill();
s.addEventListener(MouseEvent.MOUSE_DOWN,chgColorBlue);
s.addEventListener(MouseEvent.MOUSE_UP,chgColorReset);
c.addChild(s);
addElement(c);
}
private function chgColorBlue(e:MouseEvent):void {
//Just added this line to clear the old graphics
e.currentTarget.graphics.clear();
e.currentTarget.graphics.beginFill(0x000099);
e.currentTarget.graphics.drawEllipse(7,35,18,12);
e.currentTarget.graphics.endFill();
}
//Creating the shapes
shapeCreate(s1);
shapeCreate(s2);
shapeCreate(s3);
I hope it helps and that's what you wanted to do.
Good luck,
Rob

Resources