WIA image capture in Windows 7

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tuxalot
    New Member
    • Feb 2009
    • 200

    WIA image capture in Windows 7

    Using the code below to capture images in Access. On my form is an unbound OLE image and a single button. Clicking the button when a digital camera is connected via USB and turned on causes the camera to take a pic and show it in the OLE image frame.

    Code:
    Private Sub cmdGrabImg_Click()
        'Take a picture from a webcam and store it in a temp file.
        'By Justin Johnson Nov 26, 2007
    
        On Error GoTo Err_btnTakePicture_click
    
        Dim tempfile As String
        Dim mydevice As WIA.Device
        Dim item As WIA.item
        Dim imfile As WIA.imagefile
        Dim Commondialog1 As WIA.CommonDialog
        Dim MyFilename As String
    
        MyFilename = "C:\Documents and Settings\" & Environ("Username") & "\Desktop\" & "WIAGrab" & ".jpg"
    
        'put the path and name for the location of your temp file here.
        tempfile = ("" & MyFilename & "")
    
        'the next 4 lines deletes the old temp file if it exists
        Set filesystemobject = CreateObject("Scripting.FileSystemObject")
        If filesystemobject.FileExists(tempfile) Then
            Kill (tempfile)
        End If
    
        'the next two lines set up the configuration
        Set Commondialog1 = New CommonDialog
        Set mydevice = Commondialog1.ShowSelectDevice
    
        Set item = mydevice.ExecuteCommand(wiaCommandTakePicture)        'instructs the camera to take the picture
    
        Set imfile = item.Transfer        'transfers the picture from the camera
    
        'this line saves the picture to a specified file
        imfile.SaveFile (tempfile)
    
        'this sets the picture on the form to show the new picture
        Me.OLEUnbound1.Picture = (tempfile)
    
        'MsgBox "Picture taken"
    
    Exit_btnTakePicture_click:
        Set mydevice = Nothing
        Set item = Nothing
        Exit Sub
    
    Err_btnTakePicture_click:
        MsgBox Err.Description, vbOKOnly + vbCritical, "Error Taking Picture"
        Resume Exit_btnTakePicture_click
    
    End Sub
    Works great in Windows XP but fails in W7 with an error stating: "object variable or with block variable not set". The camera does open up and flash so it appears to take the picture but no result in image frame.

    Any ideas what is causing this to fail?

    Thanks!
    Last edited by tuxalot; Jan 31 '12, 01:49 AM. Reason: Added header
  • sierra7
    Recognized Expert Contributor
    • Sep 2007
    • 446

    #2
    Hi
    For this to work I presume you will have installed a device driver for your camera which will have included a '.dll' that installs Windows Image Acquisition services.

    My presumption is that you will need a different driver for Windows 7

    If you set a break point at the top of the code then step through it, you should be able to identify the line where the error ocurrs.

    Running your code without any sort of WIA driver causes a 'User-defined type not defined' error at Line #7.

    S7

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      Originally posted by Tux
      Tux:
      but fails in W7 with an error stating: "object variable or with block variable not set".
      I shouldn't be needing to tell you the basics like which line this occurs on Tux. You've also failed to include the procedure header line, which is often required to make sense of code - especially when the code posted is 50 lines. I won't delete the question as I'm sure you'll fill in the details, but please remember for future questions.

      Comment

      • tuxalot
        New Member
        • Feb 2009
        • 200

        #4
        Thanks Sierra - The camera when connected via usb I assume installs a universal driver, yes. It installed fine in XP and W7 with no interaction (Windows auto-installed drivers). It's a modest Nikon L22 point and click.

        NeoPa - If I comment line 26 or 27, I get the error mentioned and the camera does not take the picture. Leaving 26 and 27 in, I get the error mentioned and the camera snaps a shot. Looking at the camera SD card I see the image gets stored there, so I suppose it's just not transferring. I have read more recently that WIA may have issues with W7. Can't seem to find much on this topic unfortunately.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          It sounds like you don't know how to determine which line the error occurred on. When the error is reported, which line is highlighted in yellow in the debugger when you select Debug?

          Comment

          • tuxalot
            New Member
            • Feb 2009
            • 200

            #6
            There is no debug option in the dialog box that is presented.

            Comment

            • sierra7
              Recognized Expert Contributor
              • Sep 2007
              • 446

              #7
              Hi again,
              If you say the code works in XP, I can't think there is anything wrong with the code per se, which I why I suspect a driver problem.

              I've Googled 'windows 7 driver for Nikon L22' and found the a number of sites purporting to supply updated drivers. Note that there are 32 and 64 bit versions of software that have minor differences (What version of Access are you using? There are some issues with Service Pack 1 in the 64 bit version of Access 2010. Not certain what because I'm not affected by using 32 bit on 64 bit Win7, but I'll check it out)

              As you seem to be able to communicate to the camera but the system fails when processing the returned image to the OLE image frame, i still favour a driver update.
              S7

              PS. I've checked out the issue I mentioned above. It appears to be associated with running Wizards and generally not working. Here's a link "Access 2010 - Wizards not working after installing SP1"
              Last edited by sierra7; Jan 31 '12, 11:10 AM. Reason: Adding PS

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                Originally posted by Tux
                Tux:
                There is no debug option in the dialog box that is presented.
                It sounds like you haven't set your system up correctly for debugging then. Ooooops. Debugging in VBA - 3) General Tips can help you with that.

                You may find that much of your development will become easier after this point.

                PS. This doesn't argue with S7's comments and advice. They may be absolutely correct (I expect they are). However, it's still always important to include such information as the line number where the error occured as the basic details of a question if posting code that crashes :-)
                Last edited by NeoPa; Jan 31 '12, 03:01 PM. Reason: Added PS

                Comment

                • tuxalot
                  New Member
                  • Feb 2009
                  • 200

                  #9
                  Sierra - I am running 32 bit Access 2010. Also note on the MS site they say the L22 is compatible with W7 and no further drivers are necessary. I too looked at the many sites mentioning updated driver files, but those that I found are just feeder sites to get one to download non-related software which searches for updated drivers on your PC.

                  NeoPa - thanks for the tip :)

                  My code is failing at Line 31.
                  Last edited by tuxalot; Feb 1 '12, 01:32 AM. Reason: Set debug options correctly, added line generating error

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    Good for you Tux.

                    I'm afraid my comments were more as a moderator than as an expert as I have no experience with this device or driver. Having your code posted with the error message and line number included are always helpful though, and if it's helpful to any of us then it benefits you too, clearly.

                    Good luck with your problem.

                    Comment

                    • tuxalot
                      New Member
                      • Feb 2009
                      • 200

                      #11
                      Thanks NeoPa. I appreciate all you do. The bashing I get from you from time to time is clearly deserved. I WILL get this. I am just a bit dense at times ;)

                      Comment

                      • tuxalot
                        New Member
                        • Feb 2009
                        • 200

                        #12
                        I've established that the procedure above will activate a camera and save the pic to the SD card of the camera in XP and W7. I need code that can find the most recent saved file in a directory of my cameras' SD card (would be the current image), and save that path to an Access table. Seems doable, but the code below fails at line #13 (explanation follows):

                        Code:
                        Public Function GetMostRecentFile()
                            Dim FileSys As FileSystemObject
                            Dim objFile As File
                            Dim myFolder
                            Dim strFilename As String
                            Dim dteFile As Date
                                
                            'set path for files - change for your folder
                            Const myDir As String = "L22" '<<<fails here
                            
                            'set up filesys objects
                            Set FileSys = New FileSystemObject
                            Set myFolder = FileSys.GetFolder(myDir)
                                
                            
                            'loop through each file and get date last modified. If largest date then store Filename
                            dteFile = DateSerial(1900, 1, 1)
                            For Each objFile In myFolder.Files
                                If objFile.DateLastModified > dteFile Then
                                    dteFile = objFile.DateLastModified
                                    strFilename = objFile.Name
                                End If
                            Next objFile
                            
                            MsgBox strFilename
                                    
                            Set FileSys = Nothing
                            Set myFolder = Nothing
                            
                        End Function
                        I understand some cameras connect via USB using PTP (Picture Transfer Protocol) while others, especially the older and more high end cameras use MSC (Mass Storage Communication) or have the ability to choose the communication protocol. MSC devices are assigned a drive letter while PTP cameras are not. So that's a dead end unfortunately.

                        That said...

                        Is there a different way to transfer the most recent camera image captured? As mentioned, my code posted at #1 works fantastic in Windows XP, but not in W7.
                        Last edited by tuxalot; Feb 1 '12, 11:11 PM. Reason: Clarity. brought down to what is important.

                        Comment

                        • sierra7
                          Recognized Expert Contributor
                          • Sep 2007
                          • 446

                          #13
                          OK, I seem to have been on the wrong track here! I had assumed that the extra driver required to give you WIA functilonality was installed by the Nikon software.

                          Silly me; you seem to have downloaded the WIA Development kit from Microsoft. There are full instructions and example code included in the kit.

                          The required dll is WIAaut.dll for the WIA Automation layer. When I came to copy it from the kit I found there was one already installed (I'm using Win7 Ultimate. My version was 6.1.7600 the file from the kit is version 5.1.2600). Ive kept both but set a Reference in Access to the latest version. Although it was located in the System32 folder it was 'not on the list' and had to be browsed to.

                          I can't test out the code because although I have acquired a Nikon S4150 it does not seem to respond.(just yet!)

                          Refering back to your last post (find latest file) the code does work if a valid path (eg c:\L22) is defined at Line #9

                          However, when searching for a dll to control my camera I found that Nikon supply a fully functional application Camera Control Pro 2
                          It's far more comprehensive than I would write myself. I'm not sure what they will charge when the trial period expires!
                          S7.

                          Comment

                          • tuxalot
                            New Member
                            • Feb 2009
                            • 200

                            #14
                            Correct S7, that is the WIA download.

                            Comment

                            • sierra7
                              Recognized Expert Contributor
                              • Sep 2007
                              • 446

                              #15
                              Hi
                              I'm stll looking into this for my own interest but making slow progress because of other matters.

                              I've abandoned Win7, for the moment because it would not recognise the camera, so nothing would work. I can confirm your findings that thing works much better in XP.

                              While still using XP I found that although I could see the camera in Windows Explorer I could not read from the memory card. However, there was a utility in the Nikon software which came with camera that seems to have switched it so the memory card can now be read, but it still does not respond in Access 2010.

                              In passing, the camera is not assigned a drive letter so cannot be addressed using the syntax you used. It seems that your "L22" may be a volume name and will have to be referred to differently using "\\.\Volumename (Unique Identifier).." type of syntax. This is new to me but I'll be more clear when I get it working.

                              I am able to take pictures in Access 2003 using your code (well, it did not like the OLEUnbound1 object and I had to convert it to an Image frame, and I had to add a Commondialog1 control to the form)

                              Interestingly, in Access2010 it is possible to add a WIA.DeviceMange r control to a form but it is not possible to add a WIA.CommonDialo g control from the ActiveX list on the ToolBar. It gives a message saying the dll is nor registered but when you run RegSvr32 wiaaut.dll, it responds it has registered successfully. I think this is the hub of the problem.

                              I'm adding some code that cycles through the properties of the camera. This allows you to see the Unique Identifiers by which the camera is known to the system, where-upon you may be able to access the memory card, maybe! This is from MSDN Shared Samples
                              Code:
                              Dim dev 'As Device
                              Dim p 'As Property
                              Dim s 'As String
                              Dim i 'As Integer
                              
                              Set dev = CommonDialog1.ShowSelectDevice
                              
                              For Each p In dev.Properties
                                  s = p.Name & "(" & p.PropertyID & ") = "
                                  If p.IsVector Then
                                      s = s & "[vector of data]"
                                  Else
                                      s = s & p.Value
                                      If p.SubType <> UnspecifiedSubType Then       
                                  	    If p.Value <> p.SubTypeDefault Then
                                  		    s = s & "(Default = " & p.SubTypeDefault & ")"
                                  	    End If
                                      End If
                                  End If
                              
                                  If p.IsReadOnly then
                                      s= s & " [READ ONLY]"
                                  else
                                      Select Case p.SubType
                                      Case FlagSubType
                                          s = s & " [ valid flags include:"
                                          For i = 1 To p.SubTypeValues.Count
                                              s = s & p.SubTypeValues(i)
                                              If i <> p.SubTypeValues.Count Then
                                                  s = s & ", "
                                              End If
                                          Next
                                          s = s & " ]"
                                      Case ListSubType
                                          s = s & " [ valid values include:"
                                          For i = 1 To p.SubTypeValues.Count
                                              s = s & p.SubTypeValues(i)
                                              If i <> p.SubTypeValues.Count Then
                                                  s = s & ", "
                                              End If
                                          Next
                                          s = s & " ]"
                                      Case RangeSubType
                                          s = s & " [ valid values in the range from " & _
                                                 p.SubTypeMin & " to " & p.SubTypeMax & _
                                                 " in increments of " & p.SubTypeStep & " ]"
                                      Case Else 'UnspecifiedSubType
                                      End Select
                                  End If
                              
                                  MsgBox s
                              Next
                              I just put the code under a command button and the information is displayed in a series of MessageBoxes.
                              S7

                              Comment

                              Working...