本树莓派摄像头实现完成里树莓派CSI摄像头拍取照片,拍摄视频及简单的网络图片图片传输功能。
第一步:
将树莓派断电,并将CSI摄像头接入靠近网孔的排孔中,CSI摄像头蓝色面朝网孔方向。
第二步:
使能摄像头,命令sudo raspi-config
第三步:
拍摄一张照片并查看
拍摄:raspistill -o new.jpg
查看:xdg-open new.jpg # xdg-open 为系统默认应用打开
第四步:
录制视频:raspivid -o vv.h264 -t 10000s
播放可用windows的爱奇艺万能播放器
第五步:
简单网络获取实时照片实现,建立socket通信,树莓派用python写socket服务器,线程监听端口,Windows用java写socket客户端。客户端连接服务器后发送相关指令调用树莓派拍照程序进行拍照,然后把照片数据用byte格式传递给客户端,客户端进行有效接收,并显示。
其中启动按钮为启动socket客户端,测试按钮为测试连接状态,获取按钮为调用拍照指令,程序整体实现较简单,java没有使用线程,所以会有卡顿现象。
附件:
# Python代码:
//Java代码:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class Main2 extends JFrame{
JLabel jlde;
private Image image; //获取的图像
private Toolkit toolKit; //Toolkit对象,用于获取图像
private Socket sock;
private InputStream reader;
private OutputStream writer;
private boolean booxie;
private DataInputStream dis;
JLabel zt;
int sizex=0;
public Main2(){//构造函数
super("图片获取实现");//标题显示内容
this.setSize(600, 400);//页面大小
this.setResizable(false);//不可缩放
this.setLocationRelativeTo(null);//全屏居中居中显示
jlde=new JLabel();
jlde.setBounds(0, 0,this.getWidth(), this.getWidth());
this.add(jlde);
JButton qd=new JButton("启动");
JButton cs=new JButton("测试");
JButton hq=new JButton("获取");
JPanel jp=new JPanel();
zt = new JLabel();
jp.add(qd);
jp.add(cs);
jp.add(hq);
jp.add(zt);
Imgben("lala.png");//初始图片显示
this.setIconImage(new ImageIcon("lala.png").getImage());//图标图片文件
this.add(jp,"South");
this.setVisible(true);//显示界面
qd.addActionListener(new ActionListener(){//-------------------启动按钮监听,开启socket
@Override
public void actionPerformed(ActionEvent e) {
zt.setText("正在启动.......");
try {
sock=new Socket("192.168.43.243",2351);
writer=sock.getOutputStream();//获取输出流
reader=sock.getInputStream();//获取输入流
dis = new DataInputStream(sock.getInputStream());//包装输入流, 允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。
writer.write(("0").getBytes("GBK"));//发送确认信息
reads();//读数据
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}}});
cs.addActionListener(new ActionListener(){//------------------------测试是否连接成功
@Override
public void actionPerformed(ActionEvent e) {
try {
writer.write(("0").getBytes("GBK"));
reads();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}}});
hq.addActionListener(new ActionListener(){//-------------------获取图片按钮,点击可显示实时图片
@Override
public void actionPerformed(ActionEvent e) {
try {
zt.setText("开始获取......");
writer.write(("1").getBytes("GBK"));//发送获取图片指令
reads();
reads();
writer.write(("3").getBytes("GBK"));//发送空的确认获取指令
String str= reads();
sizex=Integer.parseInt(str.substring(str.indexOf("e")+2));//得到图片大小
reads();//读取图片数据
zt.setText(" 成功获取 *-* ");
Imgben("new.jpg");//显示获取的图片
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}}});
this.addWindowListener(new WindowAdapter() {//点击叉监听,完全退出程序
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});}
public String reads() throws FileNotFoundException{
String strread="";
FileOutputStream fos=null;
if(sizex>0){//【 开始接收文件 】
fos = new FileOutputStream("new.jpg");
byte[] bytes = new byte[1024];
int length = 0;
int bensizej=0;
try {
while(( length = dis.read(bytes, 0, bytes.length)) != -1) {
bensizej+=length;
try {
fos.write(bytes, 0, length);//以追加的方式写数据
fos.flush();
if(sizex<=bensizej) break;//获取完成
} catch (IOException e) {
e.printStackTrace();
} }} catch (IOException e) {
e.printStackTrace();}
sizex=0;
bensizej=0; //清零计数
}else{ //【 开始接收普通数据 】
byte[] bu=new byte[1024];
try {
reader.read(bu);
strread=(new String(bu,"GBK")).trim();
System.out.println(strread);
zt.setText(strread);
} catch (Exception e) {
e.printStackTrace();
}}
return strread;//返回读取的普通数据
}
//加载本地图片
public void Imgben(String imagePath){
toolKit = Toolkit.getDefaultToolkit();
repaint(); //重绘屏幕
image= toolKit.getImage(imagePath);
}
public void paint(Graphics g){
super.paint(g);
if (image!=null){
image.getScaledInstance(500, 500, Image.SCALE_DEFAULT);
ImageIcon ico=new ImageIcon(image);
ico.setImage(ico.getImage().getScaledInstance(600,370, Image.SCALE_DEFAULT));
jlde.setIcon(ico);
}}}
public class Main {
public static void main(String[] args) {
new Main2();
}
}