Image from Camera Roll for Physcis

Hi Coronas,

I am stuck while trying to use a freshly imported image from camera roll instead of the included image…

How do I resize the image from e.g. 1000x1000 pixels down to e.g. 100x100 pixels. Not only scale it, but really reduce the size. Example: I’d like to replace an image inside a physics body with another one taken from the camera roll. The image is scaled by using xScale and yScale, but the physics boundary will use the original size from the camera roll-picture.

I used the great Ragdoll-example from here: http://developer.anscamobile.com/forum/2010/10/29/new-corona-any-radgoll-examples

This is the Listener that gets called when issuing the camera roll.

[lua]function onComplete ( event )
if (event.target ~= nil) then
local photo = event.target
xscale = (display.contentWidth / photo.width ) / (display.contentWidth / head.width)
yscale = (display.contentHeight / photo.height ) / (display.contentHeight / head.height)

w = head.width
h = head.height
x = head.x
y = head.y
head:removeSelf()
head = nil
head = photo
head.myName = “head”
head.x = x
head.y = y

–[[ Here sits the problem. If you use the scale-portion, the physics will work, but it will only display a part of the image (the center part). If you comment out the width/height, the image will be right-sized, but the physics size will be the full size of the camera roll-image.
]]
head.xScale = xscale
head.yScale = yscale
– head.width = w
– head.height = h

doll:insert (head)
physics.addBody (head, {bounce = 0.0, friction = 1.0})
doll:addFrictionJoint(head, torsoA, torsoA.x, torsoA.y, -22.5, 22.5)
head:addEventListener ( “touch”, doll )
end
end[/lua]

Any help is highly appreciated :slight_smile:

Cheers,
Jörg [import]uid: 10612 topic_id: 11129 reply_id: 311129[/import]

You could try putting the event.target into a display.newImageRect(event.target, 100,100)

Not sure this would work though…

– Chris [import]uid: 33866 topic_id: 11129 reply_id: 40394[/import]

Hi Chris,

thanks for the answer. Great idea! I can feel the solution coming nearer to me :slight_smile:

So I saved the image from camera roll to temporary directory, and reloaded it.
Still not the whole image after reloading. Corona seems to resize the image to 1024 px width automatically - which is no big deal. But the display.save() only writes a 260x150 px image when selecting my test-image. Is this correct?

The code goes something like this:

[lua]function onComplete ( event )
if (event.target ~= nil) then
display.save(event.target,“temp.jpg”, system.TemporaryDirectory )
event.target:removeSelf()
x = head.x
y = head.y
w = head.width
h = head.height
head:removeSelf()
head = nil
head = display.newImageRect(doll, “temp.jpg”, system.TemporaryDirectory, w, h)
head.myName = “head”
head.x = x
head.y = y
physics.addBody (head, {bounce = 0.0, friction = 1.0})
doll:addFrictionJoint(head, torsoA, torsoA.x, torsoA.y, -22.5, 22.5)
head:addEventListener ( “touch”, doll )
end
end[/lua]

– Jörg [import]uid: 10612 topic_id: 11129 reply_id: 40404[/import]

I think your going to have to provide some width and height settings as your nil’ing and removing the variable ‘head’ just before you load up your newImageRect.

Try:

[lua]–provide width and height
local w = 100
local h = 100
– provide x / y
local x = 100
local y = 100
– create image rect
local head = display.newImageRect(doll, “temp.jpg”, system.TemporaryDirectory, w, h)

doll.head = head – simplify reference to rect in group
head.x = x
head.y = y
head.isVisable = true[/lua]
– Chris

[import]uid: 33866 topic_id: 11129 reply_id: 40425[/import]

Hi Chris,

thanks again. Your code also works, and is a bit smaller. BUT… I suspect a bug (or something i don’t get) in display.save or the event caused by media.show (media.Camera). It’s ok the image gets resized to 1024 pixels in width.

I made a small jpeg of 120x120 px, only white, and used the following code to load it from camera-roll.

The image will be resized to about a quarter of its original size, and then be saved in 30x30 px size, but only the bottom right quarter - not fully.

This is inside the main.lua without any other assets or config-files.

[lua]function onComplete2 ( event )
if (event.target ~= nil) then
display.save(event.target,“temp.jpg”, system.TemporaryDirectory )
local head = display.newImageRect(“temp.jpg”, system.TemporaryDirectory, 120, 120)
head.x = 120
head.y = 120
print (event.target.width)
print (head.width)
end
end

media.show( media.Camera, onComplete2 )[/lua]

– Jörg

Edit: Bug-report here: http://developer.anscamobile.com/forum/2011/06/12/camera-roll-bug [import]uid: 10612 topic_id: 11129 reply_id: 40428[/import]

Are you using the latest SDK build as I’m sure this got fixed?

– Chris [import]uid: 33866 topic_id: 11129 reply_id: 40431[/import]

Yes, just checked the 537-build (was using 535). Still the same…

But I found out: display.save only saves the content visible on the display.
Which is implicated by the name display.save, btw - and is stated in the docs.

Really quirky.

Going to try a few things, and getting back to this topic later this week.

Thanks again for your effort, Chris!

– Jörg

[import]uid: 10612 topic_id: 11129 reply_id: 40436[/import]

Hi,

seems, I could solve this problem with a precious hint by Chris.

This should work now as expected…
[lua]function onComplete ( event )
if (event.target ~= nil) then
– Scale and move the image to fit on display completely
event.target.xScale = display.contentWidth / event.target.width
event.target.yScale = event.target.yScale * event.target.height / event.target.width
event.target.x = event.target.height / 2
event.target.y = event.target.width / 2

– save it and reload it in 120x120 px
display.save(event.target, “temp.jpg”, system.TemporaryDirectory )
local head = display.newImageRect(“temp.jpg”, system.TemporaryDirectory, 120, 120)

head.x = 120
head.y = 220
print (event.target.width)
print (head.width)
end
end

media.show( media.Camera, onComplete )[/lua]

– Jörg
[import]uid: 10612 topic_id: 11129 reply_id: 40464[/import]