How to save images taken by camera

Hi,
I’m using the camera example. I used display.save to save the captured image into system.DocumentsDirectory. However, upon every click the previous image gets over-writed and hence only 1 picture is saved…

I was told that a way to fix this is to copy the image from system.DocumentsDirectory immediately after it is captured and save it in a different location.

How can I do that? [import]uid: 175611 topic_id: 33445 reply_id: 333445[/import]

Hello Ali,
I think you can solve this by using the Lua File System (LFS). Basically, before the image is saved, you check the Documents directory for an existing file by name. If the file exists, you change the name of the NEW file and the proceed to save it. You might need to use one of the other capture APIs instead of “save”, so please research all of the options.

For example:

  1. picture is captured (but not yet saved)
  2. you check for “capture-1.jpeg” using LFS
  3. if it exists, you save the file as “capture-2.jpeg” instead

This should allow you to save multiple images without overwriting them.

Here is the LFS info:
http://docs.coronalabs.com/api/library/lfs/index.html

Hope this helps,
Brent
[import]uid: 200026 topic_id: 33445 reply_id: 132882[/import]

Not sure if it’s possible in your code but display.capture handles all this for you.

Dave [import]uid: 117617 topic_id: 33445 reply_id: 132900[/import]

Thanks, this helps somewhat…

I used the example of traversing through the files of system.DocumentsDirectory. It checks for all file inside the directory, but when i try ti check for one particular file (by giving it’s name as image1.png) I get an error.

How can I search for one particular image and then how can i change it’s name? [import]uid: 175611 topic_id: 33445 reply_id: 132908[/import]

Hello,
What error are you getting with the LFS traverse? Can you show me that little piece of code, and what the Terminal is showing when you attempt it?

Brent [import]uid: 200026 topic_id: 33445 reply_id: 132939[/import]

Hello Ali,
I think you can solve this by using the Lua File System (LFS). Basically, before the image is saved, you check the Documents directory for an existing file by name. If the file exists, you change the name of the NEW file and the proceed to save it. You might need to use one of the other capture APIs instead of “save”, so please research all of the options.

For example:

  1. picture is captured (but not yet saved)
  2. you check for “capture-1.jpeg” using LFS
  3. if it exists, you save the file as “capture-2.jpeg” instead

This should allow you to save multiple images without overwriting them.

Here is the LFS info:
http://docs.coronalabs.com/api/library/lfs/index.html

Hope this helps,
Brent
[import]uid: 200026 topic_id: 33445 reply_id: 132882[/import]

local lfs = require “lfs”
local doc_path = system.pathForFile( “”, system.DocumentsDirectory )

for file in lfs.dir(doc_path) do
– file is the current file or directory name
print( "Found file: " … file )
end
this is the code i used… It displays all files in the Documents Directory.
then i made this change:

local doc_path = system.pathForFile( “image1.png”, system.DocumentsDirectory )

the error given is:

Runtime error
/Users/aliayyaz/Desktop/VahZay/test_camera/main.lua:36: cannot open /Users/aliayyaz/Library/Application Support/Corona Simulator/test_camera-465F18C75C8EA43A299BC5691631C0C4/Documents/image1.png: No such file or directory
stack traceback:
[C]: ?
[C]: in function ‘dir’
/Users/aliayyaz/Desktop/VahZay/test_camera/main.lua:36: in function

[import]uid: 175611 topic_id: 33445 reply_id: 133041[/import]

Not sure if it’s possible in your code but display.capture handles all this for you.

Dave [import]uid: 117617 topic_id: 33445 reply_id: 132900[/import]

Thanks, this helps somewhat…

I used the example of traversing through the files of system.DocumentsDirectory. It checks for all file inside the directory, but when i try ti check for one particular file (by giving it’s name as image1.png) I get an error.

How can I search for one particular image and then how can i change it’s name? [import]uid: 175611 topic_id: 33445 reply_id: 132908[/import]

Hello,
What error are you getting with the LFS traverse? Can you show me that little piece of code, and what the Terminal is showing when you attempt it?

Brent [import]uid: 200026 topic_id: 33445 reply_id: 132939[/import]

local lfs = require “lfs”
local doc_path = system.pathForFile( “”, system.DocumentsDirectory )

for file in lfs.dir(doc_path) do
– file is the current file or directory name
print( "Found file: " … file )
end
this is the code i used… It displays all files in the Documents Directory.
then i made this change:

local doc_path = system.pathForFile( “image1.png”, system.DocumentsDirectory )

the error given is:

Runtime error
/Users/aliayyaz/Desktop/VahZay/test_camera/main.lua:36: cannot open /Users/aliayyaz/Library/Application Support/Corona Simulator/test_camera-465F18C75C8EA43A299BC5691631C0C4/Documents/image1.png: No such file or directory
stack traceback:
[C]: ?
[C]: in function ‘dir’
/Users/aliayyaz/Desktop/VahZay/test_camera/main.lua:36: in function

[import]uid: 175611 topic_id: 33445 reply_id: 133041[/import]

Let me see If I understand it correctly. You want to save a image from camera, but when the user use the camera on the second time, you will loose the first saved image. Is that what you want to fix?

If so, you just need to change the file name that the image will be saved.

I wrote some code below (did not have time to test it). I am using the media.show with the filePath parameter, that will automatically save the camera picture to the file (unfortunately, the Corona simulador does not support it yet, but it will work fine in your device.)

I am using system.TemporaryDirectory but you can use the DocumentsDirectory as well.

If you want just to copy a file from a path to another, you can use this code located here (I am using it copy image files and it works well): http://developer.coronalabs.com/forum/2012/02/10/how-copy-image-file-android-devices

[lua]local imageFileIndex = 1 – variable outside that function that is called to take the picture

function TakePicture() – function that is called to take the picture

local onComplete = function(event)
if event.completed then
– displaying image that was saved
local img = display.newImage(CAMERA_SHOT_FILENAME, system.TemporaryDirectory)

– incrementing the imageFileIndex variable that is used in the image filename
imageFileIndex = imageFileIndex +1
else
– User canceled out
end
end

local CAMERA_SHOT_FILENAME = “camerashot” … imageFileIndex … “.jpg”

local filePath = { baseDir = system.TemporaryDirectory, filename = CAMERA_SHOT_FILENAME }

if media.hasSource( media.Camera ) then
media.show( media.Camera, onComplete, filePath )
else
text.text = “Camera not found.”
end

end[/lua] [import]uid: 181011 topic_id: 33445 reply_id: 133416[/import]

Let me see If I understand it correctly. You want to save a image from camera, but when the user use the camera on the second time, you will loose the first saved image. Is that what you want to fix?

If so, you just need to change the file name that the image will be saved.

I wrote some code below (did not have time to test it). I am using the media.show with the filePath parameter, that will automatically save the camera picture to the file (unfortunately, the Corona simulador does not support it yet, but it will work fine in your device.)

I am using system.TemporaryDirectory but you can use the DocumentsDirectory as well.

If you want just to copy a file from a path to another, you can use this code located here (I am using it copy image files and it works well): http://developer.coronalabs.com/forum/2012/02/10/how-copy-image-file-android-devices

[lua]local imageFileIndex = 1 – variable outside that function that is called to take the picture

function TakePicture() – function that is called to take the picture

local onComplete = function(event)
if event.completed then
– displaying image that was saved
local img = display.newImage(CAMERA_SHOT_FILENAME, system.TemporaryDirectory)

– incrementing the imageFileIndex variable that is used in the image filename
imageFileIndex = imageFileIndex +1
else
– User canceled out
end
end

local CAMERA_SHOT_FILENAME = “camerashot” … imageFileIndex … “.jpg”

local filePath = { baseDir = system.TemporaryDirectory, filename = CAMERA_SHOT_FILENAME }

if media.hasSource( media.Camera ) then
media.show( media.Camera, onComplete, filePath )
else
text.text = “Camera not found.”
end

end[/lua] [import]uid: 181011 topic_id: 33445 reply_id: 133416[/import]

thank you very much @rsc.
It works perfectly… however, please don’t mark this thread as Resolved yet. I may need to post further queries later.

Thanks. [import]uid: 175611 topic_id: 33445 reply_id: 133650[/import]

thank you very much @rsc.
It works perfectly… however, please don’t mark this thread as Resolved yet. I may need to post further queries later.

Thanks. [import]uid: 175611 topic_id: 33445 reply_id: 133650[/import]

-- displaying image that was saved local img = display.newImage(CAMERA\_SHOT\_FILENAME, system.TemporaryDirectory)
This part is causing me confusion. Initially i was saving the image with display.save and then displaying it. it worked fine.

If i remove display.save, the image is not saved and hence not displayed. Could you please explain why that is happening? [import]uid: 175611 topic_id: 33445 reply_id: 134494[/import]

-- displaying image that was saved local img = display.newImage(CAMERA\_SHOT\_FILENAME, system.TemporaryDirectory)
This part is causing me confusion. Initially i was saving the image with display.save and then displaying it. it worked fine.

If i remove display.save, the image is not saved and hence not displayed. Could you please explain why that is happening? [import]uid: 175611 topic_id: 33445 reply_id: 134494[/import]