android通过获取摄像头照片,实时与点传输
http://blog.csdn.net/csh159/article/details/7926654/
GetIP:
- package com.zte.camera_socket;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.EditText;
- import android.widget.TableLayout;
- public class GetIP extends Activity {
- String ipname = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 设置全屏
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- final Builder builder = new AlertDialog.Builder(this); // 定义一个AlertDialog.Builder对象
- builder.setTitle("登录服务器对话框"); // 设置对话框的标题
- // 装载/res/layout/login.xml界面布局
- TableLayout loginForm = (TableLayout) getLayoutInflater().inflate(
- R.layout.login, null);
- final EditText iptext = (EditText) loginForm
- .findViewById(R.id.ipedittext);
- builder.setView(loginForm); // 设置对话框显示的View对象
- // 为对话框设置一个“登录”按钮
- builder.setPositiveButton("登录"
- // 为按钮设置监听器
- , new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // 此处可执行登录处理
- ipname = iptext.getText().toString().trim();
- Bundle data = new Bundle();
- data.putString("ipname", ipname);
- Intent intent = new Intent(GetIP.this,
- Camera_socket.class);
- intent.putExtras(data);
- startActivity(intent);
- }
- });
- // 为对话框设置一个“取消”按钮
- builder.setNegativeButton("取消", new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // 取消登录,不做任何事情。
- System.exit(1);
- }
- });
- // 创建、并显示对话框
- builder.create().show();
- }
- }
- package com.zte.camera_socket;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.Socket;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.ImageFormat;
- import android.graphics.Rect;
- import android.graphics.YuvImage;
- import android.hardware.Camera;
- import android.hardware.Camera.Size;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceHolder.Callback;
- import android.view.SurfaceView;
- import android.view.Window;
- import android.view.WindowManager;
- public class Camera_socket extends Activity {
- SurfaceView sView;
- SurfaceHolder surfaceHolder;
- int screenWidth, screenHeight;
- Camera camera; // 定义系统所用的照相机
- boolean isPreview = false; // 是否在浏览中
- private String ipname;
- @SuppressWarnings("deprecation")
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 设置全屏
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- // 获取IP地址
- Intent intent = getIntent();
- Bundle data = intent.getExtras();
- ipname = data.getString("ipname");
- screenWidth = 640;
- screenHeight = 480;
- sView = (SurfaceView) findViewById(R.id.sView); // 获取界面中SurfaceView组件
- surfaceHolder = sView.getHolder(); // 获得SurfaceView的SurfaceHolder
- // 为surfaceHolder添加一个回调监听器
- surfaceHolder.addCallback(new Callback() {
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format,
- int width, int height) {
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- initCamera(); // 打开摄像头
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // 如果camera不为null ,释放摄像头
- if (camera != null) {
- if (isPreview)
- camera.stopPreview();
- camera.release();
- camera = null;
- }
- System.exit(0);
- }
- });
- // 设置该SurfaceView自己不维护缓冲
- surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- }
- private void initCamera() {
- if (!isPreview) {
- camera = Camera.open();
- }
- if (camera != null && !isPreview) {
- try {
- Camera.Parameters parameters = camera.getParameters();
- parameters.setPreviewSize(screenWidth, screenHeight); // 设置预览照片的大小
- parameters.setPreviewFpsRange(20, 30); // 每秒显示20~30帧
- parameters.setPictureFormat(ImageFormat.NV21); // 设置图片格式
- parameters.setPictureSize(screenWidth, screenHeight); // 设置照片的大小
- // camera.setParameters(parameters); // android2.3.3以后不需要此行代码
- camera.setPreviewDisplay(surfaceHolder); // 通过SurfaceView显示取景画面
- camera.setPreviewCallback(new StreamIt(ipname)); // 设置回调的类
- camera.startPreview(); // 开始预览
- camera.autoFocus(null); // 自动对焦
- } catch (Exception e) {
- e.printStackTrace();
- }
- isPreview = true;
- }
- }
- }
- class StreamIt implements Camera.PreviewCallback {
- private String ipname;
- public StreamIt(String ipname) {
- this.ipname = ipname;
- }
- @Override
- public void onPreviewFrame(byte[] data, Camera camera) {
- Size size = camera.getParameters().getPreviewSize();
- try {
- // 调用image.compressToJpeg()将YUV格式图像数据data转为jpg格式
- YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width,
- size.height, null);
- if (image != null) {
- ByteArrayOutputStream outstream = new ByteArrayOutputStream();
- image.compressToJpeg(new Rect(0, 0, size.width, size.height),
- 80, outstream);
- outstream.flush();
- // 启用线程将图像数据发送出去
- Thread th = new MyThread(outstream, ipname);
- th.start();
- }
- } catch (Exception ex) {
- Log.e("Sys", "Error:" + ex.getMessage());
- }
- }
- }
- class MyThread extends Thread {
- private byte byteBuffer[] = new byte[1024];
- private OutputStream outsocket;
- private ByteArrayOutputStream myoutputstream;
- private String ipname;
- public MyThread(ByteArrayOutputStream myoutputstream, String ipname) {
- this.myoutputstream = myoutputstream;
- this.ipname = ipname;
- try {
- myoutputstream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void run() {
- try {
- // 将图像数据通过Socket发送出去
- Socket tempSocket = new Socket(ipname, 6000);
- outsocket = tempSocket.getOutputStream();
- ByteArrayInputStream inputstream = new ByteArrayInputStream(
- myoutputstream.toByteArray());
- int amount;
- while ((amount = inputstream.read(byteBuffer)) != -1) {
- outsocket.write(byteBuffer, 0, amount);
- }
- myoutputstream.flush();
- myoutputstream.close();
- tempSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
xml:
login.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/loginForm"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TableRow>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="IP:"
- android:textSize="10pt" />
- <!-- 输入用户名的文本框 -->
- <EditText
- android:id="@+id/ipedittext"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:digits="0123456789."
- android:hint="请填写服务器IP"
- android:selectAllOnFocus="true" />
- </TableRow>
- </TableLayout>
main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <SurfaceView
- android:id="@+id/sView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scaleType="fitCenter" />
- </LinearLayout>
PC端:
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.ServerSocket;
- import java.net.Socket;
- import javax.imageio.ImageIO;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class pc {
- public static ServerSocket ss = null;
- public static void main(String args[]) throws IOException {
- ss = new ServerSocket(6000);
- final ImageFrame frame = new ImageFrame(ss);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- while (true) {
- frame.panel.getimage();
- frame.repaint();
- }
- }
- }
- /**
- * A frame with an image panel
- */
- @SuppressWarnings("serial")
- class ImageFrame extends JFrame {
- public ImagePanel panel;
- public JButton jb;
- public ImageFrame(ServerSocket ss) {
- // get screen dimensions
- Toolkit kit = Toolkit.getDefaultToolkit();
- Dimension screenSize = kit.getScreenSize();
- int screenHeight = screenSize.height;
- int screenWidth = screenSize.width;
- // center frame in screen
- setTitle("ImageTest");
- setLocation((screenWidth - DEFAULT_WIDTH) / 2,
- (screenHeight - DEFAULT_HEIGHT) / 2);
- setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
- // add panel to frame
- this.getContentPane().setLayout(null);
- panel = new ImagePanel(ss);
- panel.setSize(640, 480);
- panel.setLocation(0, 0);
- add(panel);
- jb = new JButton("拍照");
- jb.setBounds(0, 480, 640, 50);
- add(jb);
- saveimage saveaction = new saveimage(ss);
- jb.addActionListener(saveaction);
- }
- public static final int DEFAULT_WIDTH = 640;
- public static final int DEFAULT_HEIGHT = 560;
- }
- /**
- * A panel that displays a tiled image
- */
- @SuppressWarnings("serial")
- class ImagePanel extends JPanel {
- private ServerSocket ss;
- private Image image;
- private InputStream ins;
- public ImagePanel(ServerSocket ss) {
- this.ss = ss;
- }
- public void getimage() throws IOException {
- Socket s = this.ss.accept();
- System.out.println("连接成功!");
- this.ins = s.getInputStream();
- this.image = ImageIO.read(ins);
- this.ins.close();
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- if (image == null)
- return;
- g.drawImage(image, 0, 0, null);
- }
- }
- class saveimage implements ActionListener {
- RandomAccessFile inFile = null;
- byte byteBuffer[] = new byte[1024];
- InputStream ins;
- private ServerSocket ss;
- public saveimage(ServerSocket ss) {
- this.ss = ss;
- }
- public void actionPerformed(ActionEvent event) {
- try {
- Socket s = ss.accept();
- ins = s.getInputStream();
- // 文件选择器以当前的目录打开
- JFileChooser jfc = new JFileChooser(".");
- jfc.showSaveDialog(new javax.swing.JFrame());
- // 获取当前的选择文件引用
- File savedFile = jfc.getSelectedFile();
- // 已经选择了文件
- if (savedFile != null) {
- // 读取文件的数据,可以每次以快的方式读取数据
- try {
- inFile = new RandomAccessFile(savedFile, "rw");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- int amount;
- while ((amount = ins.read(byteBuffer)) != -1) {
- inFile.write(byteBuffer, 0, amount);
- }
- inFile.close();
- ins.close();
- s.close();
- javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(),
- "已接保存成功", "提示!", javax.swing.JOptionPane.PLAIN_MESSAGE);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
下面是在虚拟机上测试的效果:
android通过获取摄像头照片,实时与点传输的更多相关文章
- Android OCR文字识别 实时扫描手机号(极速扫描单行文本方案)
身份证识别:https://github.com/wenchaosong/OCR_identify 遇到一个需求,要用手机扫描纸质面单,获取面单上的手机号,最后决定用tesseract这个开源OCR库 ...
- ORB-SLAM2 运行 —— ROS + Android 手机摄像头
转载请注明出处,谢谢 原创作者:Mingrui 原创链接:https://www.cnblogs.com/MingruiYu/p/12404730.html 本文要点: ROS 配置安装 解决 sud ...
- Android手机摄像头编程入门
本讲内容:Android手机摄像头编程入门智能手机中的摄像头和普通手机中的摄像头最大的区别在于,智能机上的摄像头可以由程序员写程序控制, 做一些有趣的应用譬如,画中画,做一些有用的应用譬如二维码识别, ...
- android基础---->摄像头与相册的调用
很多应用程序都可能会使用到调用摄像头拍照和从相册选取图片的功能,今天我们开始android中摄像头与相册调用的学习. 目录导航 调用摄像头拍照 从相册中选择照片 友情链接 调用摄像头拍照 大致流程:调 ...
- Android 调用摄像头功能【拍照与视频】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/ma_hoking/article/details/28292973 应用场景: 在Android开发 ...
- DSO 运行 —— dso_ros + Android 手机摄像头
转载请注明出处,谢谢 原创作者:Mingrui 原创链接:https://www.cnblogs.com/MingruiYu/p/12425855.html 本文要点: dso 配置安装 dso 离线 ...
- 引人遐想,用 Python 获取你想要的 “某个人” 摄像头照片
仅用来学习,希望给你们有提供到学习上的作用. 1.安装库 需要安装python3.5以上版本,在官网下载即可.然后安装库opencv-python,安装方式为打开终端输入命令行. 2.更改收件人和发件 ...
- TVideoGrabber如何将网络摄像头影像实时发布到网络
在TVideoGrabber中如何将网络摄像头影像实时发布到网络?如何设置正在运行TVideoGrabber的一台电脑,同时通过另一台电脑在网络中实时的观看在线视频呢? 在这里称发送视频流的电脑为“m ...
- Android和WCF通信 - 大数据压缩后传输
Android和WCF通信 - 大数据压缩后传输 本帖来源:http://www.cnblogs.com/lykbk/archive/2013/08/15/3259045.html 最近一直在优化项目 ...
随机推荐
- MVC入门——删除页
添加Action DeleteUserInfo using System; using System.Collections.Generic; using System.Linq; using Sys ...
- pygame 安装教程
步骤: 1.去官网下载PyGame 注意:要下载对应版本的包 官网地址:http://www.pygame.org/download.shtml 其中,如果python为以下版本: python 3. ...
- 【BZOJ4953】lydsy七月月赛 F DP
[BZOJ4953]lydsy七月月赛 F 题面 题解:设f[i][j]表示第i个强度取为j时的最小误差.那么每次转移时,我们只计算j'和j之间的像素点带来的误差,于是有: $f[i][j]=min( ...
- 九度OJ 1110:小白鼠排队 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1734 解决:1054 题目描述: N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的 ...
- opencv中的子库
1 FLANN 近似最近邻库,NN就是nearest neighbor的缩写. 2 IlmImf Ilm是Industrial light & magic公司的缩写. Imf是image fo ...
- AbstractFactory Pattern
AbstractFactory模式用来解决这类问题:要创建一组相关或者相互依赖的对象. AbstractFactory Pattern结构图 实现: #ifndef _PRODUCT_H_ #defi ...
- SOAP Only Authentication Using C#
Jason Lattimer's Blog Monday, February 9, 2015 SOAP Only Authentication ...
- 谷歌浏览器使用SelectorGadget和Xpath Helper获取xpath和css path
在上篇文章里,介绍了如何在火狐浏览器中获取网页元素的xpath和css path. 这篇文章将介绍,在谷歌浏览器中使用SelectorGadget和Xpath Helper实现同样功能. 这两个谷歌浏 ...
- 远程请求json数据,list中显示
public class MainActivity extends Activity { protected static final int WHAT_REQUEST_SUCCESS = 1; pr ...
- [HTML & CSS] HTML和CSS基础知识
最近将博客简单地修饰了下,需要用到HTML和CSS代码,花了一天时间学习了一下这两方面的知识.虽然内容很简单,但是足够用来修改自己的博客了. 1. HTML 1.1. HTML介绍 HTML与CSS的 ...