Help with CreateImage (Image Rotation)

174 views
Skip to first unread message

Chinedu Emeka

unread,
Oct 2, 2017, 6:01:32 PM10/2/17
to DroidScript
Hello,
I am new to droidscript.

 I have loaded an image with the CreateImage method of the app object.

I have also rotated the image with 

Rotate( anglepivotXpivotY )

and have seen that the rotation works as expected. My problem is that I am not able to save the rotated image (that is replace the original image file with the rotated image).

My app shows a diffirent camera orientiation for diffirent devices, my intention is to use rotation to get the pix "right side up".

I have tried jimp plugin and it works but it is taking a very long time on some of my older tables/phones.

Please how do I save a rotated image via CreateImage.

Thanks,

Chinedu

alex.symbroson

unread,
Oct 2, 2017, 6:14:19 PM10/2/17
to DroidScript
Create an other image (cnv) and do cnv.DrawImage(img,x,y,w,h,angle);

Chinedu Emeka

unread,
Oct 2, 2017, 6:46:25 PM10/2/17
to DroidScript
Hello Alex,
Thanks for your response, I don't  quite understand it. Please remember that I will like to save the rotated image to file.

I installed jimp plugin and tried the code below (NOT WHAT YOU SUGGESTED)
it does NOT work but it shows what I am trying to achieve.

  cnv = app.CreateImage("img/heading.jpg");
  cnv.Rotate(90);

  cnv.Save("photos/rotated.jpg");
   

alex.symbroson

unread,
Oct 3, 2017, 2:35:23 AM10/3/17
to DroidScript
I mean you have to create two images at least. Your base image and a temporary image. And you can draw the base image on the temporary rotazed woth the DrawImage method:


var angle = 0;

function OnStart() {
lay = app.CreateLayout( "linear", "VCenter,FillXY" );

//our base image
img = app.CreateImage( "Img/Hello World.png", -1, .45 );
lay.AddChild( img );

//temporary image (normally not shown)
imgTemp = app.CreateImage( "Img/Hello World.png", -1, .45 );
imgTemp.SetAutoUpdate( false );
lay.AddChild( imgTemp );

//button
btn = app.CreateButton( "rotate and save", 0.3, 0.1 );
lay.AddChild( btn );
btn.SetOnTouch( rotate );

app.AddLayout( lay );
}

function rotate() {
angle += 45;
imgTemp.Clear();
//draw 'img' rotated
imgTemp.DrawImage( img, 0, 0, 1, 1, angle );
imgTemp.Update();
//save 'imgTemp' with rotated image
imgTemp.Save( app.GetAppPath() + '/Img/rotated.png' );
}

Chinedu Emeka

unread,
Oct 4, 2017, 10:14:44 PM10/4/17
to DroidScript
Thank you Alex

Steve Garman

unread,
Oct 5, 2017, 2:40:53 AM10/5/17
to DroidScript
For the sake of completeness, it should be possible to do this with a single image.


cnv = app.CreateImage("img/heading.jpg");
cnv.Rotate(90);

cnv.Flatten();
cnv.Save("/sdcard/rotated.jpg");

Reply all
Reply to author
Forward
0 new messages