Wednesday, May 22, 2013

Taking Larger Pictures with a pcDuino & a LinkSprite JPEG Color Camera (TTL Interface)

I posted a couple days ago about using the LinkSprite JPEG Color Camera (TTL Interface) to take pictures with a pcDuino. The default image size is 320 x 240 pixels.  From reading some of the comments online, it sounded like some people had problems changing the image size. I have found, however, that I can switch the image size to 640 x 480 without any trouble.  The only change, in addition to issuing the command to change the image size, is the longer pause after the read image data command.

Here is the revised code for taking pictures measuring 640 x 480 pixels -


import serial
import time
import datetime

# Initialize camera
serial = serial.Serial("/dev/ttyS1", baudrate=38400)
serial.write(b'\x56\x00\x26\x00')
resp = ""
time.sleep(1)
while (serial.inWaiting() > 0):
data = serial.read()
resp += data
if "Init end\r\n" in resp:
print "Ready"
break

# Set image size to 640 x 480
serial.write(b'\x56\x00\x54\x01\x00')
resp = ""
time.sleep(1)
while (serial.inWaiting() > 0):
        data = serial.read()
        resp += data
        if b'\x76\x00\x54\x00\x00' in resp:
                print "Size set"
                break


# Take picture
serial.write(b'\x56\x00\x36\x01\x00')
resp = ""
time.sleep(1)
while (serial.inWaiting() > 0):
data = serial.read()
resp += data
if b'\x76\x00\x36\x00\x00' in resp:
print "Picture taken"
break

#Get JPG size
serial.write(b'\x56\x00\x34\x01\x00')
resp = ""
time.sleep(1)
while (serial.inWaiting() > 0):
data = serial.read()
resp += data
if b'\x76\x00\x34\x00\x04\x00\x00' in resp:
msb = serial.read()
lsb = serial.read()
print "Image file size: %d bytes" % (ord(msb) << 8 | ord(lsb))

# Write image to file
serial.write(b'\x56\x00\x32\x0C\x00\x0A\x00\x00\x00\x00\x00\x00%c%c\x00\x0A' % (msb,lsb))
time.sleep(7)  # Increased to 7 seconds, otherwise image is clipped 
now = datetime.datetime.now()
filename = "%d.%02d.%02d.%02d.%02d.%02d.jpg" % \
(now.year,now.month,now.day,now.hour,now.minute,now.second)
resp = serial.read(size=5)
if b'\x76\x00\x32\x00\x00' in resp:
with open("/var/www/" + filename, 'wb') as f:
while (serial.inWaiting() > 0):
data = serial.read()
f.write('%c' % data)
print "Image written to /var/www/%s" % (filename)


Note that this approach to changing image size is not permanent - you will need to set the size every time.

While changing the image size does not pose any problems from what I have seen, changing the baud rate of the camera is a very different matter. I have read several posts online indicating that people have rendered the camera inoperable by trying to change the baud rate.

1 comment:

  1. Very usefull tutorial. We forward your post to learn.linksprite.com. If you have any other new post about pcDuino(www.pcduino.com) or any other LinkSprite's(www.linksprite.com) products, please feel free to let us know. If you have a new post, could you please give us a backlink?

    ReplyDelete