Script from VB or VBA - Script Writing - CHDK Forum  

Script from VB or VBA

  • 12 Replies
  • 6589 Views
Script from VB or VBA
« on: 03 / June / 2013, 05:57:16 »
Advertisements
Morning

I'm trying to find a solution to taking a photograph within Microsoft Access and storing the image in a folder, specifially for the record on screen.  I'm hoping that the CHDK scripts will be my solution.

Has anyone tried to do this before, or do the scripts only work directly from the camera unit itself.

I've searched the forums, but cant find anything similar.  Before i sit down to learn the programming, i could really do with knowing that i'm on the right track and that this is the solution that i'm looking for.  Any help or pointers greatly appreciated.

kind regards


Re: Script from VB or VBA
« Reply #1 on: 03 / June / 2013, 12:02:56 »
CHDK scripts work only on the camera.

However, there considerable support for PTP (picture taking protocal), which could be used for what you want to do from a PC : http://chdk.wikia.com/wiki/PTP_Extension

In specific, chdkptp is a tool that can communicate with the camera from the PC and it is script driven as well. Integrating it with Microsoft Access is not something anyone had done but it seems technically possible of you have the right software skills.
Ported :   A1200    SD940   G10    Powershot N    G16

Re: Script from VB or VBA
« Reply #2 on: 25 / July / 2013, 02:34:45 »
Did you manage to sort out how to control your camera through VBA? I'd like to do something similar with Excel. Please can you post anything you manage to do with this.

Thanks

Derek Johnson

Re: Script from VB or VBA
« Reply #3 on: 03 / August / 2013, 01:50:21 »
I've now got this working In Excel with VBA using the Shell command to send command line messages to Chdkptp.exe, with a bit of extra coding to wait for the command to complete. I'm triggering shooting and downloading the file to the PC. The code I'm using then creates a local hyperlink in a cell to a renamed copy of the image. Clicking on the cell then brings up a preview of the image in Picasa viewer. I'll post the key bits of the code shortly.


Re: Script from VB or VBA
« Reply #4 on: 03 / August / 2013, 02:07:22 »
I've now got this working In Excel with VBA using the Shell command to send command line messages to Chdkptp.exe, with a bit of extra coding to wait for the command to complete. I'm triggering shooting and downloading the file to the PC. The code I'm using then creates a local hyperlink in a cell to a renamed copy of the image. Clicking on the cell then brings up a preview of the image in Picasa viewer. I'll post the key bits of the code shortly.
Awesome!!!
Pushing my luck here .. would you be willing to create a CHDK wiki page explaining what you have created? Forum posts tend to get buried ...  I can help with the wiki page if it works for you.
Ported :   A1200    SD940   G10    Powershot N    G16

Re: Script from VB or VBA
« Reply #5 on: 11 / August / 2013, 06:44:22 »
I've been a bit busy of late, but here is some code to control the camera through VBA. I'm using chdkptp.exe in command line mode and invoking it from VBA. One issue is that chdkptp uses output to the console for feedback so I've adopted a more complicated method than the standard SHELL command to get the results back into VBA. I re-direct the output to a file and then wait for completion. (You can in theory read the partial results from the file.) The advantage of this method is that it does not freeze Excel while it is running CHDKPTP.

I've created an Execute function which I will list later, but here is a usage example:

Code: [Select]

        StartTime = Timer()
        'This command connects to the camera, sets it to record mode, shoots and then returns
        'the image file back to the current directory.
        'The workbook containing this code should be in the same directory as chdkptp
        Params = "-c -e=reconnect -e=rec -e=""shoot  -dl "" "
        result = Execute(ThisWorkbook.Path, "chdkptp.exe " & Params)
        Elapsed = Timer() - StartTime
        Debug.Print "CaptureImage Result:"; result

You should now have the console output in the string 'result'. You can find the file name returned as follows:

Code: [Select]
        Debug.Print "CaptureImage Result:"; result
        'eg A/DCIM/109___08/IMG_0428.JPG->IMG_0428.JPG  (plus VBCRLF at end)
        Offset = InStr(1, result, "->")
        If Offset > 0 Then
            'remove trailing crlf
            LastFile = Replace(Mid(result, Offset + 2), vbCrLf, "")
            Debug.Print "Image Name:'"; LastFile; "'"
            'Check if the file has actually been created
            LastFile = Dir(CurrentDirectory & "\" & LastFile, vbNormal)
           
            GoTo foundit
        ELSE

End If

foundit:
   
     
Here is the Execute function:

Code: [Select]
'This declaration needs to be at the top of the module
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Function Execute(WorkingDirectory, MyCommand As String, Optional WaitFor = 30)
On Error GoTo handler
Dim WshShell, objExecObject, strText, i, Now, OutputFile, fileobj, RunThis As String
Set WshShell = VBA.CreateObject("WScript.Shell")
WshShell.CurrentDirectory = WorkingDirectory
'This file could be put anywhere you like
OutputFile = "c:\MyResult.log"
On Error Resume Next
Kill OutputFile  'Raises error if file does not exist
On Error GoTo handler
RunThis = "%COMSPEC% /c """ + MyCommand + """ > " & OutputFile
Debug.Print "Executing:" & RunThis

Now = Timer()
Set objExecObject = WshShell.Exec(RunThis)
Debug.Print "Waiting for output to complete"
Do Until Timer() - Now > WaitFor
    If Timer() - Now < 0 Then Now = Timer() 'Just in case you run this at midnight
    If objExecObject.Status <> 0 Then
            Set FSO = CreateObject("Scripting.FileSystemObject")
            Set fileobj = FSO.OpenTextFile(OutputFile, 1)
            strText = fileobj.ReadAll
            fileobj.Close
            Application.StatusBar = "Execute Command Finished Normally. Took " & Format(Timer() - Now, "### seconds")
            Debug.Print strText
            GoTo done
    End If
    Application.StatusBar = "Executing command " & String(i, ".")
    Sleep (1000)
    DoEvents
Loop
    Application.StatusBar = "Execute Timed out."
done:
    'set result to entire output
    Set objExecObject = Nothing
    Set WshShell = Nothing
    Execute = strText
    Application.visible = msoTrue
    Exit Function
   
handler:
    MsgBox "Error while executing command. " & Err.Description
    Stop
    Resume Next

End Function



Re: Script from VB or VBA
« Reply #6 on: 11 / August / 2013, 07:04:45 »
Just a further few notes on the VBA script example I submitted.

I found that the first time I connect to the camera and send a command it may not work. I modified by code to try execute up to 3 times. It has always worked by the 3rd attempt. Once working it tends to stay working.

You need to place the Excel workbook containing the code with the chkdptp.exe file.

When you are using a camera horizontally to take images of documents, the camera orientation recorded in the EXIF data may be arbitrary. You might need to update the orientation to match those of your document using EXIFTOOL with a command line such as
Code: [Select]
          exiftool.exe -n  -Orientation=6 filename

where Orientation has these values

'1 = Horizontal (normal)
'2 = Mirror horizontal
'3 = Rotate 180
'4 = Mirror vertical
'5 = Mirror horizontal and rotate 270 CW
'6 = Rotate 90 CW
'7 = Mirror horizontal and rotate 90 CW
'8 = Rotate 270 CW

This will allow you to view the images with the correct orientation. I don't recommend actually rotating the image in the saved document.


Re: Script from VB or VBA
« Reply #7 on: 12 / August / 2013, 14:25:29 »
Hi dqj_99 - interesting stuff !

i'd be interested to know if this basic approach could be extended to a large number of cameras [like 100 !]

Any comment appreciated.


Re: Script from VB or VBA
« Reply #8 on: 13 / August / 2013, 02:12:12 »
There are some threads elsewhere on synchronising cameras, but the main issue would I think be how to connect 100 devices via USB. You might look at virtual USB over Ethernet. If you were wanting to transfer the images back though, I think the I/O rate might be too much, I suspect about 10 to 20 might work if you could figure out the connectivity angle.

Re: Script from VB or VBA
« Reply #9 on: 13 / August / 2013, 02:28:17 »
Just thinking about the idea of controlling a large number of cameras, I think the way to do this is to use a Raspberry PI box connected to each camera, with an Ethernet -based App polling the PIs for data. That way you could scale it up gradually and I can't see what an upper limit might be.   

 

Related Topics