Cropping image from Camera

I would like to crop an image from the device camera but I can’t seem to get it working.

First I tried to do something with imagesheets - I made an imagesheet with one image entry, at the x,y and width, height of the crop I wanted, but that doesn’t seem to work with large images.

Second I tried using snapshot of crop width and height and placing the image inside it, but could not get that to work either (probably because the image was larger than the snapshot.

I could upload the image to a server, crop it and send it back - but that just seems insane.

Has anyone done this already?

https://forums.coronalabs.com/topic/17656-image-capturecropping-using-displaysave/

Imagesheets don’t work either. The problem is the image captured by the camera is larger than the screen, so corona don’t like it.

You probably need to scale the image down

I got something that seems to work… unfortunatley display.save applies scaling, so on small devices the cropped image is correct (in terms of chat was cropped) but is smaller than the crop dimensions… and on larger devices the image is bigger than the crop dimensions. 

Is there a way of turning off corona’s automatic scaling - just for one action?

This is what I’ve got.

function U.cropImageOnDevice(filename, newFileName, cropX, cropY, cropW, cropH) local photo=display.newImage( filename, system.TemporaryDirectory ) local endWidth = cropW local endHeight = cropH -- set the masking container local tempGroup = display.newSnapshot(endWidth, endHeight ) tempGroup.x = 0 tempGroup.y = 0 tempGroup.anchorX = 0 tempGroup.anchorY = 0 -- Define a solid color background, in case the final image is -- smaller than the cropping output local whiteRc = display.newRect( 0, 0, endWidth, endHeight ) tempGroup.group:insert(whiteRc) whiteRc.anchorX = 0.5 whiteRc.anchorY = 0.5 whiteRc:setFillColor( 1,1,1 ) -- insert the photo tempGroup.group:insert(photo) -- set photo to x0 to start the crop at x=0 local x0 = cropW \* -0.5 local y0 = cropH \* -0.5 local x = x0 + ( cropX \* -1 ) local y = y0 + ( cropY \* -1 ) photo.x = x photo.y = y photo.anchorX = 0 photo.anchorY = 0 -- save the cropped image display.save( tempGroup, { filename = newFileName, baseDir = system.TemporaryDirectory, isFullResolution = true } ) -- clean up the mess tempGroup:removeSelf() tempGroup = nil end

U.cropImageOnDevice(“a.jpg”, “b.jpg”, 100, 400, 500, 500)

@prographodeveloper I wouldn’t wait around for it, if you’re in a hurry, but for what it’s worth I’ve been putting together a plugin to, among other things, decode image files into raw bytes (a string, which you could crop with a bit of string.sub()'ing), the relevant function being this one. I’ve occasionally needed this feature in particular and my Lua decoders are dog slow on large images. :smiley:

I don’t know when it will be ready (it needs some testing and obviously more complete docs, which I’d like to have in reasonable shape before submitting), but I’m hoping soon. Anyhow, might be good to know.

(Down the road I also mean to submit the more heavyweight FreeImage, but that’s going to need considerably more attention.)

@StarCrunch, let me know when you release it - thx :slight_smile:

@prographodeveloper Will do.

Incidentally, I briefly demo’d my test case from the library on yesterday’s show (near the end). I have a companion module (actually a fork of this) that I can populate from a stream of bytes. What’s being presented (not sure if I explained it well; it was very off-the-cuff :D) is the library decoding a GIF and animating it by loading the current frame’s bytes on demand.

https://forums.coronalabs.com/topic/17656-image-capturecropping-using-displaysave/

Imagesheets don’t work either. The problem is the image captured by the camera is larger than the screen, so corona don’t like it.

You probably need to scale the image down

I got something that seems to work… unfortunatley display.save applies scaling, so on small devices the cropped image is correct (in terms of chat was cropped) but is smaller than the crop dimensions… and on larger devices the image is bigger than the crop dimensions. 

Is there a way of turning off corona’s automatic scaling - just for one action?

This is what I’ve got.

function U.cropImageOnDevice(filename, newFileName, cropX, cropY, cropW, cropH) local photo=display.newImage( filename, system.TemporaryDirectory ) local endWidth = cropW local endHeight = cropH -- set the masking container local tempGroup = display.newSnapshot(endWidth, endHeight ) tempGroup.x = 0 tempGroup.y = 0 tempGroup.anchorX = 0 tempGroup.anchorY = 0 -- Define a solid color background, in case the final image is -- smaller than the cropping output local whiteRc = display.newRect( 0, 0, endWidth, endHeight ) tempGroup.group:insert(whiteRc) whiteRc.anchorX = 0.5 whiteRc.anchorY = 0.5 whiteRc:setFillColor( 1,1,1 ) -- insert the photo tempGroup.group:insert(photo) -- set photo to x0 to start the crop at x=0 local x0 = cropW \* -0.5 local y0 = cropH \* -0.5 local x = x0 + ( cropX \* -1 ) local y = y0 + ( cropY \* -1 ) photo.x = x photo.y = y photo.anchorX = 0 photo.anchorY = 0 -- save the cropped image display.save( tempGroup, { filename = newFileName, baseDir = system.TemporaryDirectory, isFullResolution = true } ) -- clean up the mess tempGroup:removeSelf() tempGroup = nil end

U.cropImageOnDevice(“a.jpg”, “b.jpg”, 100, 400, 500, 500)

@prographodeveloper I wouldn’t wait around for it, if you’re in a hurry, but for what it’s worth I’ve been putting together a plugin to, among other things, decode image files into raw bytes (a string, which you could crop with a bit of string.sub()'ing), the relevant function being this one. I’ve occasionally needed this feature in particular and my Lua decoders are dog slow on large images. :smiley:

I don’t know when it will be ready (it needs some testing and obviously more complete docs, which I’d like to have in reasonable shape before submitting), but I’m hoping soon. Anyhow, might be good to know.

(Down the road I also mean to submit the more heavyweight FreeImage, but that’s going to need considerably more attention.)

@StarCrunch, let me know when you release it - thx :slight_smile:

@prographodeveloper Will do.

Incidentally, I briefly demo’d my test case from the library on yesterday’s show (near the end). I have a companion module (actually a fork of this) that I can populate from a stream of bytes. What’s being presented (not sure if I explained it well; it was very off-the-cuff :D) is the library decoding a GIF and animating it by loading the current frame’s bytes on demand.