Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Capture / Load / Reset need some help!
Started by dodi_games May 16 2018 03:42 PM

13 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 126 posts
  • Corona SDK

Hi guys I've been away for a while but come back.  To the point, I want to capture, load and reset the display object resulting from media capture and select image.

 

my code


local image
local photo
local camButton
local selectPhoto
local refreshButton

-- create()
function scene:create( event )

   local sceneGroup = self.view
   --cam
   local sessionComplete = function(event)
     image = event.target
     if ( image ) then
        print( "media.capturePhoto() returned an image!" )
        print( "Image resolution: ".. image.width .. "x" .. image.height )

        -- Center image
        image.x = centerX
        image.y = centerY
        sceneGroup:insert( image )

        image.width = 300
        image.height = 300

     else
        print( "media.capturePhoto() was canceled." )
     end
   end

   local function capturePhoto( event )
       display.remove( image ) --delete image to capture another
       if photo then           --if there are a photo selected remove it
          photo:removeSelf()
          photo = nil
       end
       if media.hasSource( media.Camera ) then
           media.capturePhoto( { listener=sessionComplete } )
       else
           native.showAlert( "App", "This device does not have a camera.", { "OK" } )
       end
    end
   --select photo
   local sessionComplete = function(event)
     photo = event.target

     if photo then
         print( "media.selectPhoto() user select a photo!" )

 	 -- Center photo
 	 photo.x = centerX
 	 photo.y = centerY
         sceneGroup:insert( photo )

 	 photo.width = 300
 	 photo.height = 300

     else
         print( "No photo Selected" )
     end
   end

   local function pickPhoto( event )
        display.remove( photo )  --delete photo to select another
        if image then            --if there are a image captured remove it
           image:removeSelf()
           image = nil
        end
        media.selectPhoto(
       {
           mediaSource = media.SavedPhotosAlbum,
           listener = sessionComplete,
           origin = selectPhoto.contentBounds,
           permittedArrowDirections = { "right" },
       })
   end
   --refresh
   local function refreshButton( event )
       if ( event.phase == "began" ) then
           --audio.play( some audio )
       elseif ( event.phase == "ended" ) then
           if image then                  --here is the problem
             display.remove( image )
           end
           if photo then
             display.remove( photo )
           end
       end
       return true
   end

  --buttons
  --cam
  camButton = widget.newButton(
      {
          width = 66,
          height = 66,
          defaultFile = "images/buttons/cam.png",
          overFile = "images/buttons/cam.png",
      }
  )
  -- Center cam btn
  camButton.x = display.contentWidth-610
  camButton.y = display.contentHeight-397
  sceneGroup:insert( camButton )

  camButton:addEventListener( "tap", capturePhoto )
  --photo
  selectPhoto = widget.newButton(
      {
          width = 66,
          height = 66,
          defaultFile = "images/buttons/select.png",
          overFile = "images/buttons/select.png",
      }
  )
  -- Center photo btn
  selectPhoto.x = display.contentWidth-535
  selectPhoto.y = display.contentHeight-397
  sceneGroup:insert( selectPhoto )

  selectPhoto:addEventListener( "tap", pickPhoto )

  -- refresh btn
  refreshButton = widget.newButton(
          {
            width = 35,
            height = 35,
            defaultFile = "images/buttons/refresh.png",
            overFile = "images/buttons/refresh.png",
            onEvent = refreshButton
          }

 )
  --center refresh button
  refreshButton.x = centerX+200  --150
  refreshButton.y = centerY+120
  sceneGroup:insert( refreshButton )

end

I think the problem is scope or a bad code for remove image from cam or select,

the code contains typos. I get an error something about trying to remove a nil object.

 

Any help guys?



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,377 posts
  • Corona SDK

It would be a lot easier to debug this with a complete project, but my guess is you want to simply replace all calls like this:

obj:removeSelf()

with:

display.remove( obj )

If this does not fix it and if you don't get suitable help from others, please consider this:

 

I have some old code that might be convertible to your purpose.  It allows you to take photos and collect them in a catalog.  You can then replace these photos with new camera shots if you want.

 

I could be convinced to update and modify this code for your use as a module.  I think this would be about a level-2 hit: https://forums.coronalabs.com/topic/69420-hire-a-hitman-problem-solver-is-back/

 

Cheers,

Ed


Edited by roaminggamer, 16 May 2018 - 04:19 PM.

  • dodi_games likes this

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,377 posts
  • Corona SDK

I just noticed your marker (not mentioned in your problem description... I marked the code, look for 'here is the problem').

           if image then                  --here is the problem
             display.remove( image )
           end

I find it hard to believe that this code is throwing an error.  

 

display.remove() is safe and will not to throw an error  if nil is passed in.

 

 

Are you sure the error message in your console is complaining about that specific code?


  • dodi_games likes this

[TOPIC: post.html]
#4

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 126 posts
  • Corona SDK

@roaminggamer

Thanks for your quick response. I thought about uploading the complete module but it is 1600 lines and I am 100% sure that nobody was going to read. I can delete the image(s) satisfactorily using the ''display.remove() and the if statement with the object:removeSelf() and object = nil'', but, if I use display.remove() for the two scenarios (capture and select / image & photo) I have an error. That's why in the image capture and in the image selection I use display.remove() and the if statement of removeSelf / nil combo. The problem is in the refresh button, that seems to be there is no display object, giving me an error that is trying to delete something that does not exist. The display.remove() documents say its verifies that the object is not nil before removing it, and for that reason I try to use it in all the calls. This app model do not save display objects only show them in the app.



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,377 posts
  • Corona SDK

1. Was that a post from a phone?  No line breaks and paragraphs made that positively painful to read.  I read it twice and I'm still a little confused as to what exactly you said.

 

2. You can post the project as a zip file and anyone can easily run it and zero in on the problem w/o needing to read all the code.

 

3. I don't mean my example saves display objects.  I meant there is a grid of image chips that can be saved to and replaced at will by taking photos.  It is like a larger version of what you're doing.  

 

 

 

Good luck on this.  If you do post the project I'll run it and help find the source of that error.

 

Off to the gym now, back later.


Edited by roaminggamer, 16 May 2018 - 04:59 PM.

  • dodi_games likes this

[TOPIC: post.html]
#6

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 126 posts
  • Corona SDK

@roaminggamer

Let me explain the app mechanics. There are two functions, capture and select, on the screen you will only see the object created by one function at a time in which it must be removed when calling the other function or repeating the same function. The refresh button must erase any image shown by any of the two functions.

 

Sorry for the grammar mistakes, I try to do my best, I find it a bit difficult to explain everything in a language that is not mine. :)



[TOPIC: post.html]
#7

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 126 posts
  • Corona SDK

do I need to return false?

do I need to set display object (image or photo) true or false?

do I need to create an empty variable to allocate image and photo?

do I need to make a function to check if image or photo are in scene?

 

If number 4 i need an example or where to read info. 

 

thanks in advance!



[TOPIC: post.html]
#8

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,377 posts
  • Corona SDK

 

Good luck on this.  If you do post the project I'll run it and help find the source of that error.

 



[TOPIC: post.html]
#9

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 126 posts
  • Corona SDK

@roaminggamer

Thank you for your answer RG but I can not upload the project because I am waiting for approval of copy right registration of the mechanics of the App. When I receive the document, I would be willing to upload it. I could do a similar module with the same code and different variables that work the same but I would be looking for the error in a new App, not in the existing one. It's a work of almost 2 years. I have designed each of the objects and as a graphic designer I took on the task of trying to take my art to mobile phones and to the world thanks to Corona SDK. I just ask for some basic rules, because I'm not a code expert, I should be overlooking. I do not want anyone to write me a code or give me a fix, I just want to learn to solve the problem on my own. When there is something I do not understand, I come here to ask the experts.

I understand and I appreciate all the good intentions to help but I would be letting you see in code and design the work of two years. I hope you can understand.

Thanks again.

[TOPIC: post.html]
#10

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,377 posts
  • Corona SDK

You can always PM me a link.  I have no interest in the code, just closing this post.

 

PS - If you do PM me, be sure to give instructions on how to reproduce the issue.



[TOPIC: post.html]
#11

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 1,723 posts
  • Corona SDK

There is no such thing as "copyright of code", otherwise there would be only one candy crush and not millions of clones!

 

You can trademark a logo or certain game assets (for example sonic or mario) but otherwise you are completely unprotected.

 

Please don't pay any money to anyone claiming to protect your work... you will be throwing money down the drain.



[TOPIC: post.html]
#12

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 126 posts
  • Corona SDK

@roaminggamer

I will try to read a little more and do some tests, if I can not solve I send you a PM.

@SGS

I'm not going to register a code, I'm going to register an intellectual property, the problem is that the ideas can't be registered because only they exist in the mind and anyone can think them, we have to take something that makes the idea came to live, a painting, a construction, a software, something that brings that idea to reality, I'm not going to register the code but the mechanics of App that shows what my idea reflects. That's just that I'm going to register, an idea demonstrated in an app.

Thanks for all your help and your time.

[TOPIC: post.html]
#13

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 23,714 posts
  • Corona Staff

Just a note: Our Corona community is international. Copyright, trademark, and patent laws are going to vary from country to country. Legal questions should be referred to an intellectual property lawyer to get answers appropriate for your local.

 

Rob


  • dodi_games likes this

[TOPIC: post.html]
#14

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 126 posts
  • Corona SDK

The error disappeared

After doing several tests with the if statement I just wrote display.remove(object) for everything without the if/then/end and the errors are over. The error seemed to be that the if statement was looking for something that did not exist and the display.remove(object) seems to already do the same thing.

I am not an expert programmer but I deduce that this is what happens. I share how I solved the problem so that other people who want to write a similar module with the same mechanics can read and try if my solution solves the problem.


[topic_controls]
[/topic_controls]