/* Linksprite */

#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <SPI.h>
#define ReadLen 0x30 byte mac[] = { 0xDE, 0xAC, 0xEA, 0xEF, 0xFF, 0xE2 };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "192.168.9.6"; // name address for Google (using DNS)
char HexTable[] = "0123456789ABCDEF";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(,,,);
IPAddress gateway(,,,);
IPAddress dns_server(,,,);
IPAddress subnet(,,,);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client; byte incomingbyte;
SoftwareSerial mySerial(,); //Configure pin 4 and 5 as soft serial port
int a=0x0000,j=,k=,count=; //Read Starting address
uint8_t MH,ML;
boolean EndFlag=;
int errCount=;
int led=; void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();
void httpRequest(byte bytes[]); void setup()
{
Serial.begin();
mySerial.begin(); if (Ethernet.begin(mac) == ) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip,dns_server,gateway,subnet);
} setImgSize();
delay();
readEcho(false);
Serial.println("Ready");
} void loop()
{ SendResetCmd();
delay();//After reset, wait 2-3 second to send take picture command
EndFlag=;
a=0x0000;j=;k=; count=;
MH=;ML=;
SendTakePhotoCmd();
readEcho(false);
byte a[ReadLen];
while(!EndFlag)
{
j=;
k=;
count=;
SendReadDataCmd();
delay(); while(mySerial.available()>)
{
incomingbyte=mySerial.read();
k++;
if((k>)&&(j<ReadLen)&&(!EndFlag))
{
a[j]=incomingbyte;
if((a[j-]==0xFF)&&(a[j]==0xD9)){ //Check if the picture is over
EndFlag=;
StopTakePhotoCmd();
readEcho(false);
}
j++;
count++;
}
}
// for(j=0;j<count;j++)
// { if(a[j]<10)
// Serial.print("00");
// if(a[j]>=10 && a[j]<100)
// Serial.print("0");
// Serial.print(a[j]);
// Serial.print(" ");
// } //Send jpeg picture over the serial port
// Serial.println(); while(!client.connected()){
byte aa[]={,,};
httpRequest(aa,);
}
httpRequest(a,count);
// for(j=0;j<count;j++)
// { if(a[j]<0x10)
// Serial.print("0");
// Serial.print(a[j],HEX);
// Serial.print(" ");
// } //Send jpeg picture over the serial port
// Serial.println();
}
// delay(30000);
} //==============byte to hex string===============
String toHex(byte bs[],int count){
String bundle="";
for(int i=;i<count;i++){
bundle += (String)( HexTable[ bs[i] / ]) + (String)( HexTable[bs[i] % ]); }
return bundle;
}
//==============End byte to hex string============= //================Http Request Code========================
void httpRequest(byte bytes[],int count){
// if you get a connection, report back via serial: if(!client.connected()){
Serial.println("disconnecting.");
client.stop();
if (client.connect(server, )) {
Serial.println("connected");
}else{
// kf you didn't get a connection to the server:
Serial.println("connection failed");
errCount++;
if(errCount>=){
digitalWrite(led,HIGH);
delay();
}
}
} else{ String bundle=toHex(bytes,count);
//Serial.println(bundle);
client.println("GET /main.ashx?m=img&mil=" + String(millis()) + "&b="+ bundle +" HTTP/1.1");
client.println("Host:192.168.9.6");
//client.println("Connection: close"); client.println();
delay();
String reply="";
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
reply+=c; } //Serial.println(reply);
if(reply.indexOf("\"Model\":\"OPEN\"")>=){
Serial.println("OPEN"); }
if(reply.indexOf("\"Model\":\"CLOSE\"")>=){
Serial.println("CLOSE"); }
errCount=; //wdt_reset();
} }
//================End Http Request Code=============== void readEcho(bool show){
while(mySerial.available()>){
char c=mySerial.read();
if(show){
Serial.print(c);
}
}
}
//160 * 120
void setImgSize(){
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x31);
mySerial.write(0x05);
mySerial.write(0x04);
mySerial.write(0x01);
mySerial.write((byte)0x00);
mySerial.write(0x19);
mySerial.write(0x22); }
//Send Reset command
void SendResetCmd()
{
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x26);
mySerial.write((byte)0x00);
} //Send take picture command
void SendTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write((byte)0x00);
} //Read data
void SendReadDataCmd()
{
MH=a/0x100;
ML=a%0x100;
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x32);
mySerial.write(0x0c);
mySerial.write((byte)0x00);
mySerial.write(0x0a);
mySerial.write((byte)0x00);
mySerial.write((byte)0x00);
mySerial.write(MH);
mySerial.write(ML);
mySerial.write((byte)0x00);
mySerial.write((byte)0x00);
mySerial.write((byte)0x00);
mySerial.write(ReadLen);
mySerial.write((byte)0x00);
mySerial.write(0x0a);
a+=ReadLen; //address increases 32,set according to buffer size
} void StopTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(0x03);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;
namespace ColorRecognition.JPEGCamera
{
public class JPEGCameraRecord
{
private static long GRecId = ; private static int C_MAXLen = ;
private static int C_ShrinkageLen = ;
private List<byte> Bytes { get; set; } //正在收集的图片(单张)
private Queue<byte[]> Queue { get; set; } //已经收集的图片集合
private DateTime? BTime { get; set; }
private DateTime? ETime { get; set; }
public Queue<int> SpeedQueue = new Queue<int>();
public string IP { get; set; }
public long? LastClientMillis { get; set; }
public DateTime? LastEditTime { get; set; }
public long RecId { get; set; }
/// <summary>
/// 上传一张要多少秒
/// </summary>
public int? Speed { get; set; } public void ConcatImg(byte[] bytes)
{
lock (this)
{
#region
if (bytes.Length >= )
{
if (bytes[] == 0xFF && bytes[] == 0xD8)
{ //开始
Bytes.Clear();
Bytes = new List<byte>();
Bytes.AddRange(bytes);
BTime = DateTime.Now;
}
else if (bytes[bytes.Length - ] == 0xFF && bytes[bytes.Length - ] == 0xD9)
{
//结束
Bytes.AddRange(bytes);
Queue.Enqueue(Bytes.ToArray());
Bytes.Clear();
Bytes = new List<byte>();
if (Queue.Count >= C_MAXLen)
{
byte[] r = null;
while (Queue.Count > C_ShrinkageLen)
{
Queue.Dequeue();
}
}
ETime = DateTime.Now;
var due =(int)((TimeSpan) (ETime - BTime)).TotalSeconds;
SpeedQueue.Enqueue(due);
if (SpeedQueue.Count >=)
{
SpeedQueue.Dequeue();
}
Speed=(int) SpeedQueue.Average();
}
else
{
if (Bytes.Count > )
{
Bytes.AddRange(bytes);
}
if (Bytes.Count > * * ) //丢弃超过10M的图片
{
Bytes.Clear();
Bytes = new List<byte>();
}
} }
#endregion
}
}
public List<byte[]> GetImages()
{
lock (this)
{
var list = new List<byte[]>();
foreach (var img in Queue)
{
list.Add(img);
}
return list;
}
}
public JPEGCameraRecord()
{
Bytes = new List<byte>();
Queue = new Queue<byte[]>();
Interlocked.Increment(ref GRecId);
RecId = GRecId;
}
public long? BufferLen
{
get
{
lock (this)
{
return Bytes.Count;
}
}
}
public long? ImageCount
{
get
{
lock (this)
{
return Queue.Count;
}
}
} }
public class JPEGCameraRecordMgr
{
private static int C_MAXLen = ;
private static int C_ShrinkageLen = ;
public readonly static ConcurrentQueue<JPEGCameraRecord> Queue = new ConcurrentQueue<JPEGCameraRecord>();
public static void Add(byte[] bytes, string ip, long? clientMillis)
{
var it = Queue.FirstOrDefault(ent => string.Compare(ent.IP , ip,true)==);
if (it == null)
{
var record = new JPEGCameraRecord();
record.IP = ip;
Queue.Enqueue(record); if (Queue.Count > C_MAXLen)
{
JPEGCameraRecord r=null;
while (Queue.Count >= C_ShrinkageLen)
{
Queue.TryDequeue(out r);
}
}
it = record;
}
it.LastClientMillis = clientMillis;
it.LastEditTime = DateTime.Now;
it.ConcatImg(bytes);
} }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; namespace ColorRecognition.JPEGCamera
{
public class CameraHttpHandle:IHttpHandler
{
public HttpRequest Request { get; set; }
public HttpResponse Response { get; set; }
public void ProcessRequest(HttpContext context)
{ Request = context.Request;
Response = context.Response; Response.ContentType = "text/plain";
Response.Expires = -; var response = new ResponseBase<String>() { Code = };
try
{
var method = Request["m"];
if (string.Compare(method, "img", true) == )
{
DoCollect();
}
else
{
throw new Exception("未知道的方法");
} response.Code = ;
response.Msg = "OK"; }
catch (Exception ex)
{
response.Code = -;
response.Msg = ex.Message; } Response.Write(this.ToJson(response)); }
public virtual void DoCollect()
{
long? millis = this.TryParser<long?>(Request["mil"], );
var imgSegment = Request["b"];
var ip = this.GetClientIP();
if (!string.IsNullOrWhiteSpace(imgSegment))
{
var bytes=new byte[imgSegment.Length/];
int j = ;
for(int i=;i<imgSegment.Length;i+=){
var v = imgSegment.Substring(i, );
bytes[j] = byte.Parse(v,System.Globalization.NumberStyles.HexNumber);
j++;
}
JPEGCameraRecordMgr.Add(bytes, ip, millis);
}
} public bool IsReusable
{
get { return false; }
}
}
}

//调整后Arduino端

/* Linksprite */
#include <avr/io.h>
#include <avr/wdt.h>
#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <SPI.h>
#define ReadLen 0x30 byte mac[] = { 0xDE, 0xAC, 0xEA, 0xEF, 0xFF, 0xE2 };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "192.168.9.6"; // name address for Google (using DNS)
char HexTable[] = "0123456789ABCDEF";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(,,,);
IPAddress gateway(,,,);
IPAddress dns_server(,,,);
IPAddress subnet(,,,);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client; byte incomingbyte;
SoftwareSerial mySerial(,); //Configure pin 4 and 5 as soft serial port
int a=0x0000,j=,k=,count=; //Read Starting address
uint8_t MH,ML;
boolean EndFlag=;
int errCount=;
int led=;
int led2=;
long btime=;
boolean picGetOutTime=false; void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();
void httpRequest(byte bytes[]); void setup()
{ pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
digitalWrite(led2,LOW);
digitalWrite(led,LOW); Serial.begin();
mySerial.begin(); if (Ethernet.begin(mac) == ) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip,dns_server,gateway,subnet);
} SendResetCmd();
delay();
readEcho(false);
setImgSize();
delay();
readEcho(false); //wdt_enable(WDTO_8S);
Serial.println("Ready"); } void loop()
{
Serial.println("loop!");
SendResetCmd();
delay();//After reset, wait 2-3 second to send take picture command
EndFlag=;
a=0x0000;j=;k=; count=;
MH=;ML=;
SendTakePhotoCmd();
readEcho(false);
byte a[ReadLen]; btime=millis();
while(!EndFlag)
{
j=;
k=;
count=;
SendReadDataCmd();
delay(); if((millis() - btime) > ( * )){
digitalWrite(led2,HIGH);
Serial.write("break1");
picGetOutTime=true;
break;
}
while(mySerial.available()>)
{
if((millis() - btime) > ( * )){
picGetOutTime=true;
break;
} incomingbyte=mySerial.read();
k++;
if((k>)&&(j<ReadLen)&&(!EndFlag))
{
a[j]=incomingbyte;
if((a[j-]==0xFF)&&(a[j]==0xD9)){ //Check if the picture is over
EndFlag=;
picGetOutTime=false;
StopTakePhotoCmd();
readEcho(false);
Serial.println("end pic");
digitalWrite(led2,LOW);
}
j++;
count++;
}
}
// for(j=0;j<count;j++)
// { if(a[j]<10)
// Serial.print("00");
// if(a[j]>=10 && a[j]<100)
// Serial.print("0");
// Serial.print(a[j]);
// Serial.print(" ");
// } //Send jpeg picture over the serial port
// Serial.println(); if(picGetOutTime){
digitalWrite(led2,HIGH);
digitalWrite(led,HIGH);
delay();
}
// Serial.println(count);
while(!client.connected()){
byte aa[]={,,};
httpRequest(aa,);
} httpRequest(a,count); // for(j=0;j<count;j++)
// { if(a[j]<0x10)
// Serial.print("0");
// Serial.print(a[j],HEX);
// Serial.print(" ");
// } //Send jpeg picture over the serial port
// Serial.println();
}
// delay(30000);
} //==============byte to hex string===============
String toHex(byte bs[],int count){
String bundle="";
for(int i=;i<count;i++){
bundle += (String)( HexTable[ bs[i] / ]) + (String)( HexTable[bs[i] % ]); }
return bundle;
}
//==============End byte to hex string============= //================Http Request Code========================
void httpRequest(byte bytes[],int count){
// if you get a connection, report back via serial: if(!client.connected()){
Serial.println("disconnecting.");
client.stop();
if (client.connect(server, )) {
Serial.println("connected");
}else{
// kf you didn't get a connection to the server:
Serial.println("connection failed");
errCount++;
if(errCount>=){
digitalWrite(led,HIGH);
delay();
}
}
} else{ String bundle=toHex(bytes,count);
//Serial.println(bundle);
client.println("GET /main.ashx?m=img&mil=" + String(millis()) + "&b="+ bundle +" HTTP/1.1");
client.println("Host:192.168.9.6");
//client.println("Connection: close"); client.println();
delay();
String reply="";
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
reply+=c; } //Serial.println(reply);
if(reply.indexOf("\"Model\":\"OPEN\"")>=){
Serial.println("OPEN"); }
if(reply.indexOf("\"Model\":\"CLOSE\"")>=){
Serial.println("CLOSE"); }
errCount=; //wdt_reset();
} }
//================End Http Request Code=============== void readEcho(bool show){
while(mySerial.available()>){
char c=mySerial.read();
if(show){
Serial.print(c);
}
}
}
//160 * 120
void setImgSize(){
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x31);
mySerial.write(0x05);
mySerial.write(0x04);
mySerial.write(0x01);
mySerial.write((byte)0x00);
mySerial.write(0x19);
mySerial.write(0x22); }
//Send Reset command
void SendResetCmd()
{
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x26);
mySerial.write((byte)0x00);
} //Send take picture command
void SendTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write((byte)0x00);
} //Read data
void SendReadDataCmd()
{
MH=a/0x100;
ML=a%0x100;
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x32);
mySerial.write(0x0c);
mySerial.write((byte)0x00);
mySerial.write(0x0a);
mySerial.write((byte)0x00);
mySerial.write((byte)0x00);
mySerial.write(MH);
mySerial.write(ML);
mySerial.write((byte)0x00);
mySerial.write((byte)0x00);
mySerial.write((byte)0x00);
mySerial.write(ReadLen);
mySerial.write((byte)0x00);
mySerial.write(0x0a);
a+=ReadLen; //address increases 32,set according to buffer size
} void StopTakePhotoCmd()
{
mySerial.write(0x56);
mySerial.write((byte)0x00);
mySerial.write(0x36);
mySerial.write(0x01);
mySerial.write(0x03);
}

12,13,设置pinMode(OUTPUT)时无效(连网络扩展板上)

9V 1A,外接电源,错误时,无法带动继电器

JPEG Camer 图片上传的更多相关文章

  1. Asp.Net Mvc 使用WebUploader 多图片上传

    来博客园有一个月了,哈哈.在这里学到了很多东西.今天也来试着分享一下学到的东西.希望能和大家做朋友共同进步. 最近由于项目需要上传多张图片,对于我这只菜鸟来说,以前上传图片都是直接拖得控件啊,而且还是 ...

  2. 06.LoT.UI 前后台通用框架分解系列之——浮夸的图片上传

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  3. JS图片上传预览插件制作(兼容到IE6)

    其实,图片预览功能非常地常见.很意外,之前遇到上传图片的时候都不需要预览,也一直没有去实现过.现在手上的项目又需要有图片预览功能,所以就动手做了一个小插件.在此分享一下思路. 一.实现图片预览的一些方 ...

  4. .NET MVC实现多图片上传并附带参数(ajaxfileupload)

    做网站呢,都免不了要做图片上传. 还记得去年做微信的时候用WebAPI+ajaxfileupload.js做了一个能够附带参数上传的功能,博文地址:.NET WebAPI 实现图片上传(包括附带参数上 ...

  5. .NET WebAPI 实现图片上传(包括附带参数上传图片)

    博主的项目,客户端是APP,考虑到以后也可能会应用到微信端.网站等,图片上传方法就需要兼容多端,并且以目前的设计,不允许非登录用户上传图片,就得在上传时解决附带参数上传图片的问题. 先来看看后台方法( ...

  6. 兼容好的JS图片上传预览代码

    转 : http://www.codefans.net/articles/1395.shtml 兼容好的JS图片上传预览代码 (谷歌,IE11) <html xmlns="http:/ ...

  7. PHP 图片上传工具类(支持多文件上传)

    ====================ImageUploadTool======================== <?php class ImageUploadTool { private ...

  8. MVC4中基于bootstrap和HTML5的图片上传Jquery自定义控件

    场景:mvc4中上传图片,批量上传,上传前浏览,操作.图片进度条. 解决:自定义jquery控件 没有解决:非图片上传时,会有浏览样式的问题; 解决方案; 1.样式 – bootstrap 的css和 ...

  9. php 图片上传的公共方法(按图片宽高缩放或原图)

    写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...

随机推荐

  1. PCM 编码

    PCM编码,即无损编码(抽样->量化->编码) 这里的 无损 是个广义概念,任何数字编码都有损,只不过PCM的“损”最小:通常所说的无损编码都是指PCM编码 wav音频参数: 最重要的三个 ...

  2. 代码报错记录-MAVEN-2

    报错: 编译错误,程序包org.junit找不到 原因: 这个是父项目,报错是在子项目中,子项目使用了父项目的junit包,由于scope是test,导致子项目在编译时找不到junit, 修改: 将父 ...

  3. ActiveMQ安全设置:设置admin的用户名和密码

    ActiveMQ使用的是jetty服务器, 打开conf/jetty.xml文件,找到 <bean id="securityConstraint" class="o ...

  4. 书单BookList

    1. <代码大全> 史蒂夫·迈克康奈尔 (Code Complete) 2. <程序员修炼之道> Andrew Hunt [读过了,非常好的一本书] (Pragmatic Pr ...

  5. Why We Worry and What to Do About It

    Note: My new book Atomic Habits is available to preorder now. Click here to learn more. The Evolutio ...

  6. node集成mysql——pool连接池

    安装 mysql npm install mysql or cnpm install mysql 创建db.js,实现mysql操作模块 var mysql = require('mysql'); v ...

  7. Ansible 从MySQL数据库添加或删除用户

    mysql_user - 从MySQL数据库添加或删除用户. 概要 要求(在执行模块的主机上) 选项 例子 笔记 状态 支持 概要 从MySQL数据库添加或删除用户. 要求(在执行模块的主机上) My ...

  8. java.lang.IllegalArgumentException: Service not registered

    java.lang.IllegalArgumentException: Service not registered 首先检查一下,Service是否在AndroidManifest文件中注册.格式如 ...

  9. SVN的基本操作

    右键SVN Commit 提交成功了,我们把SVN的服务器端刷新一下 所有的操作如果只是删除本地的文件都不会影响服务器端的文件,除非右键SVN Commit删除文件或者是新增文件才会对服务器端的仓库里 ...

  10. Python globals() 函数

    Python globals() 函数  Python 内置函数 描述 globals() 函数会以字典类型返回当前位置的全部全局变量. 语法 globals() 函数语法: globals() 参数 ...