Save image taken by the mobile camera device to a file in the sdcard

In the sample code that comes with Corona demonstrating the camera example

https://github.com/coronalabs/samples-coronasdk/blob/master/Media/Camera/main.lua

I was able to compile the code to an .apk, install in on my mobile device and successfully take a photo.

The sample code only DISPLAYS the image BUT I want to SAVE the image with a filename of my choice.

I am sure this is the snippet given at the end of this post, takes the photo and displays it and that the file is linked to the local variable called ‘image’.

The question is, how do it save it as a image file like ‘my_image.jpg’ in the mobile sdcard.

I found a line of code at the forum URL 

https://forums.coronalabs.com/topic/70706-cannot-save-photos-with-specified-name/?hl=%2Bsave+%2Bcamera+%2Bimage+%2Bto+%2Bfile#entry369193

display.save(photoGroup, "image.jpg", storeDirectory)

Will it work if I replace the keyword ‘display’ with ‘image’ to work with the code below? Or is there another command for that? I don’t understand the purpose of the variable ‘photoGroup’.

image.save(photoGroup, "my\_image.jpg", storeDirectory)

The snippet that takes the photo is given below.

-- Called when media.capturePhoto() finishes. If we get a photo back, display it. local displayedPhoto = nil local sessionComplete = function(event) local image = event.target -- If we got an image back, display some information about it, and fit it -- to the screen. if ( image ) then print( "media.capturePhoto() returned an image!" ) print( "event.target: ", image.\_properties) print( "Image resolution: ".. image.width .. "x" .. image.height ) -- Delete previous photo if we've displayed one. if ( displayedPhoto ) then displayedPhoto:removeSelf() end displayedPhoto = image -- Center the image on the screen. image.x = centerX image.y = centerY -- Resize the image to fit the screen. local diffX, diffY = image.width - \_W, image.height - \_H local scaleFactor = 1 if ( diffX \> diffY ) then scaleFactor = \_W / image.width else scaleFactor = \_H / image.height end image:scale( scaleFactor, scaleFactor ) -- Bring instruction text to the foreground. if ( displayText ) then displayText:toFront() end else print( "media.capturePhoto() was canceled." ) end end

UPDATE:

I have inserted the following code at line 12 and it saves the file BUT the file name is being saved as “Picture1.jpg” and subsequent pictures being saved as “Picture2.jpg” etc. even though I have specified the filename as “CamerShot.jpg”.

 -- save image to file local filePath = { baseDir = system.TemporaryDirectory, filename = "CameraShot.jpg" } media.show( media.Camera, onComplete, filePath )

How do I save the file according to the filename that I have specified.

Hi,

kindly post a proper title for the question you want to get resolved, You are asking multiple questions in a post. 

Take your time in order to get it easy to understand the post to other members.

Well coming to your questions:

1- Will it work if I replace the keyword ’ display’ with ’ image’ to work with the code below? Or is there another command for that? 

- No, You can’t use the keyword  ’ image’  instead of ’ display’ , as of it is not an official API library of Corona SDK. Even if u try u will get an error.

Ref API: https://docs.coronalabs.com/api/library/index.html

2-I don’t understand the purpose of the variable ‘photoGroup’.?

  • The  '** photoGroup **’  is simply a display group used in that snippet.

  display.newGroup(): Creates a display group for organizing/layering display objects. Initially, there are no children in a group. The local origin is at the parent’s origin; the anchor point is initialized to this local origin.

3- Save Captured Image:
[lua]

media.capturePhoto({listener=captured, destination = {baseDir = system.TemporaryDirectory, filename = “my_image.jpg”, type = “image”}})

[/lua]

For photo, it outputs it to your app’s directories like (system.TemporaryDirectory, system.CachesDirectory, system.DocumentsDirectory). 

If you want to store it in SD-CARD then you have to write a block which can read the image from the above directory & write it in SD card. Remeber Reading Image is not a big task, perhaps writing it on SD card will cause u much effort as of corona didn’t have any official API to read/write on sd-card.

-Assif

 

  

Hi,

kindly post a proper title for the question you want to get resolved, You are asking multiple questions in a post. 

Take your time in order to get it easy to understand the post to other members.

Well coming to your questions:

1- Will it work if I replace the keyword ’ display’ with ’ image’ to work with the code below? Or is there another command for that? 

- No, You can’t use the keyword  ’ image’  instead of ’ display’ , as of it is not an official API library of Corona SDK. Even if u try u will get an error.

Ref API: https://docs.coronalabs.com/api/library/index.html

2-I don’t understand the purpose of the variable ‘photoGroup’.?

  • The  '** photoGroup **’  is simply a display group used in that snippet.

  display.newGroup(): Creates a display group for organizing/layering display objects. Initially, there are no children in a group. The local origin is at the parent’s origin; the anchor point is initialized to this local origin.

3- Save Captured Image:
[lua]

media.capturePhoto({listener=captured, destination = {baseDir = system.TemporaryDirectory, filename = “my_image.jpg”, type = “image”}})

[/lua]

For photo, it outputs it to your app’s directories like (system.TemporaryDirectory, system.CachesDirectory, system.DocumentsDirectory). 

If you want to store it in SD-CARD then you have to write a block which can read the image from the above directory & write it in SD card. Remeber Reading Image is not a big task, perhaps writing it on SD card will cause u much effort as of corona didn’t have any official API to read/write on sd-card.

-Assif