/* 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. Spring cron 定时调度配置

    IDEA 或者 STS http://spring.io/guides/gs/scheduling-tasks/ spring  mvc : 结构: Seconds Minutes Hours Day ...

  2. mysql 5.7.10使用dbforget Studio 连接异常

    提示:The 'INFORMATION_SCHEMA.SESSION_VARIABLES' feature is disabled; see the documentation for 'show_c ...

  3. mysql大表优化

    https://segmentfault.com/a/1190000006158186 https://tech.meituan.com/mysql-index.html http://www.cnb ...

  4. Apache Hive 执行HQL语句报错 ( 10G )

    # 故障描述: hive > , ) as uuid, count(distinct(request_body["uuid"])) as count from log_bft ...

  5. ICE中间件相关

    Ice 是 网络通信引擎 Internet Communications Engine 的简称,是ZeroC开发的一个面向对象的中间件平台.它提供了面向对象的远程过程调用.网格计算和发布/订阅功能,并 ...

  6. Java 枚举那点事

    目录 最近有需求,想存自定义的枚举值,比如 HOTLINE("Hotline") 我想存 Hotline 于是研究了一下Java的枚举问题 如下数据库的Entity (贫血模型哈) ...

  7. 练习:自己写一个容器ArrayList集合 一一数组综合练习2

    package cn.bjsxt.collection; /** * 自己实现一个ArrayList */ import java.util.ArrayList; import java.util.L ...

  8. pandas 一行文本拆多行,一列拆多列

    https://zhuanlan.zhihu.com/p/28337202 一列拆多列: http://blog.csdn.net/qq_22238533/article/details/761875 ...

  9. The Last Stand

    The Last Stand https://ac.nowcoder.com/acm/contest/303/L 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语 ...

  10. iOS 静态库的封装

    参考网址:http://www.jianshu.com/p/b754709135fb http://www.jianshu.com/p/443a5b8f3894   注意:封装静态库时要注意的地方: ...