极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 27426|回复: 7

Arduino上用JPEG Color Camera拍照【转】

[复制链接]
发表于 2014-4-14 23:23:56 | 显示全部楼层 |阅读模式
原帖地址:http://www.oschina.net/question/1425530_143521


今天晚上用Arduino+JPEG摄像头拍照,并且存到了SD开里面。因为网上的很多例子都说照出来的照片很花,结果果然如此,花了很长的时候将这个问题解决,得到了清晰的图像,现在分享给大家。



硬件清单

接线方法
SD Card Breakout Board

  • MOSI – pin 11
  • MISO – pin 12
  • CLK – pin 13
  • CS – pin 4
  • VCC-5v
JPGE Camera
  • RX-pin6
  • TX-pin5
  • VCC-5v
示例代码  

  1. // LinkSprite.com
  2. // Note:
  3. // 1. SD must be formated to FAT16
  4. // 2. As the buffer of softserial has 64 bytes, so the code read 32 bytes each time
  5. // 3. Please add the libaray to the lib path

  6. //  * SD card attached to SPI bus as follows:
  7. //** MOSI - pin 11
  8. // ** MISO - pin 12
  9. // ** CLK - pin 13
  10. // ** CS - pin 4

  11. #include <SoftwareSerial.h>
  12. #include <SD.h>

  13. byte ZERO = 0x00;

  14. byte incomingbyte;
  15. SoftwareSerial mySerial(5,6);          // Set Arduino pin 4 and 5 as softserial

  16. long int a=0x0000,j=0,k=0,count=0,i=0;
  17. uint8_t MH,ML;
  18. boolean EndFlag=0;

  19. File  myFile;

  20. void SendResetCmd();
  21. void SetBaudRateCmd();
  22. void SetImageSizeCmd();
  23. void SendTakePhotoCmd();
  24. void SendReadDataCmd();
  25. void StopTakePhotoCmd();

  26. void setup()
  27. {

  28. Serial.begin(38400);
  29. while (!Serial) {
  30. ; // wait for serial port to connect. Needed for Leonardo only
  31. }

  32. mySerial.begin(38400);

  33. Serial.print("Initializing SD card...");
  34. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  35. // Note that even if it's not used as the CS pin, the hardware SS pin
  36. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  37. // or the SD library functions will not work.
  38. pinMode(10, OUTPUT);

  39. if (!SD.begin(4)) {
  40. Serial.println("initialization failed!");
  41. return;
  42. }
  43. Serial.println("initialization done.");

  44. }

  45. void loop()
  46. {

  47. byte a[32];
  48. int ii;

  49. SendResetCmd();
  50. delay(4000);                            //Wait 2-3 second to send take picture command

  51. SendTakePhotoCmd();

  52. while(mySerial.available()>0)
  53. {
  54. incomingbyte=mySerial.read();
  55. }

  56. myFile = SD.open("pic.jpg", FILE_WRITE); //<strong><span style="color: #ff0000;">The file name should not be too long</span></strong>

  57. while(!EndFlag)
  58. {
  59. j=0;
  60. k=0;
  61. count=0;
  62. SendReadDataCmd();

  63. delay(20); //250 for regular

  64. while(mySerial.available()>0)
  65. {
  66. incomingbyte=mySerial.read();
  67. k++;
  68. if((k>5)&&(j<32)&&(!EndFlag))
  69. {
  70. a[j]=incomingbyte;
  71. if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
  72. EndFlag=1;
  73. j++;
  74. count++;
  75. }
  76. }

  77. for(j=0;j<count;j++)
  78. {
  79. if(a[j]<0x10)
  80. Serial.print("0");
  81. Serial.print(a[j],HEX);           // observe the image through serial port
  82. Serial.print(" ");
  83. }

  84. for(ii=0; ii<count; ii++)
  85. myFile.write(a[ii]);

  86. Serial.println();

  87. i++;

  88. }

  89. myFile.close();

  90. Serial.print("Finished writing data to file");

  91. while(1);

  92. }

  93. void SendResetCmd()
  94. {
  95. mySerial.write(0x56);
  96. mySerial.write(ZERO);
  97. mySerial.write(0x26);
  98. mySerial.write(ZERO);
  99. }

  100. void SetImageSizeCmd()
  101. {
  102. mySerial.write(0x56);
  103. mySerial.write(ZERO);
  104. mySerial.write(0x31);
  105. mySerial.write(0x05);
  106. mySerial.write(0x04);
  107. mySerial.write(0x01);
  108. mySerial.write(ZERO);
  109. mySerial.write(0x19);
  110. mySerial.write(0x11);
  111. }

  112. void SetBaudRateCmd()
  113. {
  114. mySerial.write(0x56);
  115. mySerial.write(ZERO);
  116. mySerial.write(0x24);
  117. mySerial.write(0x03);
  118. mySerial.write(0x01);
  119. mySerial.write(0x2A);
  120. mySerial.write(0xC8);

  121. }

  122. void SendTakePhotoCmd()
  123. {
  124. mySerial.write(0x56);
  125. mySerial.write(ZERO);
  126. mySerial.write(0x36);
  127. mySerial.write(0x01);
  128. mySerial.write(ZERO);
  129. }

  130. void SendReadDataCmd()
  131. {
  132. MH=a/0x100;
  133. ML=a%0x100;
  134. mySerial.write(0x56);
  135. mySerial.write(ZERO);
  136. mySerial.write(0x32);
  137. mySerial.write(0x0c);
  138. mySerial.write(ZERO);
  139. mySerial.write(0x0a);
  140. mySerial.write(ZERO);
  141. mySerial.write(ZERO);
  142. mySerial.write(MH);
  143. mySerial.write(ML);
  144. mySerial.write(ZERO);
  145. mySerial.write(ZERO);
  146. mySerial.write(ZERO);
  147. mySerial.write(0x20);
  148. mySerial.write(ZERO);
  149. mySerial.write(0x0a);
  150. a+=0x20;
  151. }

  152. void StopTakePhotoCmd()
  153. {
  154. mySerial.write(0x56);
  155. mySerial.write(ZERO);
  156. mySerial.write(0x36);
  157. mySerial.write(0x01);
  158. mySerial.write(0x03);
  159. }
复制代码



测试运行

  • 将所有的线接好。
  • 在Arduino IDE 1.0.5里面运行代码
  • 编译下载
  • 打开串口触发拍照
  • 当监视串口打出“Finished writing data to file”则照片数据已经写到SD里面
看看我拍的照片。



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2014-4-15 12:50:21 | 显示全部楼层
不错不错,效果还可以
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-15 14:48:01 | 显示全部楼层
wing 发表于 2014-4-15 12:50
不错不错,效果还可以


嗯,转来的帖子,在这分享一下,我没实际做过
回复 支持 反对

使用道具 举报

发表于 2014-4-15 20:09:22 | 显示全部楼层
保存后是什么格式呢?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-16 00:32:26 | 显示全部楼层
hp198969 发表于 2014-4-15 20:09
保存后是什么格式呢?

没注意看程序吧,第75行:jpg
回复 支持 反对

使用道具 举报

发表于 2014-5-2 09:07:50 来自手机 | 显示全部楼层
如果要玩这个强烈建议用硬串口读取,速度会快很多
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-5-5 15:38:27 | 显示全部楼层
daiqx2 发表于 2014-5-2 09:07
如果要玩这个强烈建议用硬串口读取,速度会快很多

嗯,有待实验一下,给出个速度测试报告
回复 支持 反对

使用道具 举报

发表于 2016-9-17 14:14:19 | 显示全部楼层
好帖要顶呀
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-10 00:41 , Processed in 0.054412 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表