JPEGカメラを使ってみよう

写真を撮る

サイレントシステムで扱っているC1098-SSを用いて写真を撮り、USBを通してパソコン側でその画像を表示してみます。

main.cpp
/**
  パソコンからgあるいはGで撮影開始。データをパソコン側に送る。
  Rでリセットするかもしれない。
  p13: 緑色   p14:黄色
  Vdd=3.3V

 */

#include "mbed.h"
#include "CameraC1098.h"

static const int RAWIMG_X = 320;
static const int RAWIMG_Y = 240;

Serial pc(USBTX, USBRX); // tx, rx
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
static bool pict=false;
int num=0;

CameraC1098 camera(p13, p14, CameraC1098::baud115200);

void jpeg_callback(char *buf, size_t siz) {
    for (int i = 0; i < (int)siz; i++) {
        if(pict){
          pc.printf("%02x ", buf[i]);
          if(++num%16==0) pc.printf("\r\n");
        }
        else pc.printf("%c", buf[i]);
    }
}

/**
 * Synchronizing.
 */
void sync(void) {
    CameraC1098::ErrorNumber err = CameraC1098::NoError;

    err = camera.sync();
    int count=0;
    while(err) {
      switch(count)
      {
        case 0: camera.setBaud(CameraC1098::baud14400); break;
        case 1: camera.setBaud(CameraC1098::baud115200); break;
        case 2: camera.setBaud(CameraC1098::baud57600); break;
        case 3: camera.setBaud(CameraC1098::baud28800); break;
        default: count=0;
      }
      count++;
      err = camera.sync();
      pc.printf("init err1=%d\r\n", err);
      if(!err) {
        pc.printf("to 115200\r\n");
#ifdef S115200
        camera.init(CameraC1098::Baud115200, CameraC1098::JpegResolution320x240);
        pc.printf("init err2=%d\r\n", err);
        camera.setBaud(CameraC1098::baud115200);
        err = camera.sync();
        err = camera.init(CameraC1098::Baud115200, CameraC1098::JpegResolution320x240);
#else
        camera.init(CameraC1098::Baud57600, CameraC1098::JpegResolution320x240);
        pc.printf("init err2=%d\r\n", err);
        camera.setBaud(CameraC1098::baud57600);
        err = camera.sync();
        err = camera.init(CameraC1098::Baud57600, CameraC1098::JpegResolution320x240);
#endif
        pc.printf("init err3=%d\r\n", err);
      }
    }
}


/**
 * A entry point.
 */
int main() {
  char x;
  pc.baud(115200);

  while(true)
  {
    num=0;
    do{
      x = pc.getc();
      if(x=='R') camera.sendReset(true);
    } while(!(x=='g' || x=='G'));
    pict = x=='G';
    sync();
    led1=1;
    pc.printf("%c",x);
    camera.getJpegSnapshotPicture(jpeg_callback);
    led1=0;
    led2=0;
    led3=0;
    led4=0;
    if(pict) pc.printf("\r\n");
    camera.sendReset(0xff);
  }

  return 0;
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 


パソコン側のプログラム

こちらはC#で書きます。
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace getCamera
{
    public partial class Form1 : Form
    {
        String comport;
        Bitmap bitmap2 = null;
        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 13;

            //serialPort1.Open();
        }

        private void takePicture_Click(object sender, EventArgs e)
        {
            takePict("g");

        }
        private void takePict(String sw)
        {
            int IMG_X = 320;
            int IMG_Y = 240;
            Byte[] JPEG = new byte[60000];
            byte[] size = new byte[10];
            int p = 0;
            serialPort1.Write(sw);
            if (sw.Equals("g"))
            {
                using (BinaryWriter w = new BinaryWriter(File.OpenWrite("test.jpg")))
                {
                    try
                    {
                        do
                        {
                            if (serialPort1.BytesToRead != 0)
                            {
                                byte ch = (byte)serialPort1.ReadByte();
                                if (ch == sw[0]) break;
                            }
                        } while (true);
                        int count = 0;
                        // バイナリ形式でファイルに書き出し。
                        while (true)
                        {
                            if (serialPort1.BytesToRead != 0)
                            {
                                byte ch = (byte)serialPort1.ReadByte();
                                w.Write(ch);
                                JPEG[p++] = ch;
                                count = 0;
                            }
                            else
                            {
                                Thread.Sleep(150);
                                count++;
                                if (count > 2) break;
                            }

                        }
                        MemoryStream memStream = new MemoryStream(60000);
                        memStream.Write(JPEG, 0, JPEG.Length);
                        bitmap2 = new Bitmap(memStream, true);
                        pictureBox1.Image = bitmap2;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("error: "+e);
                    }
                }
            }
            else if(sw=="G")
            {
                try
                {
                    char[] line = new char[IMG_X];
                    for (int y = 0; y < IMG_Y; y++)
                    {
                        for (int x = 0; x < IMG_X; x++)
                        {
                            line[x] = (char)serialPort1.ReadByte();
                        }
                        Debug.Print(new String(line));
                    }
                    Console.WriteLine("End");
                }
                catch (Exception) {
                    Console.WriteLine("Length error");
                }
            }
        }

        private void setComPort(object sender, EventArgs e)
        {
            comport = comboBox1.Text;
            Debug.Print(comport);
            serialPort1.Close();
            serialPort1.PortName= comport;
            serialPort1.Open();
   
        }

        private void hexDumpClick(object sender, EventArgs e)
        {
            takePict("G");
        }

        private void resetClick(object sender, EventArgs e)
        {
            takePict("R");
        }
    }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 シリアルのポートの指定。本当はここにあってはいけない。
23 
24 
25 
26 
27 
28 
29 写真を取る指令を送る
30 
31 
32 mbedにコマンドを送り、画像を表示する