• android通过获取摄像头照片,实时与点传输


    http://blog.csdn.net/csh159/article/details/7926654/

    GetIP:

    [html] view plain copy
     print?
    1. package com.zte.camera_socket;  
    2.   
    3. import android.app.Activity;  
    4. import android.app.AlertDialog;  
    5. import android.app.AlertDialog.Builder;  
    6. import android.content.DialogInterface;  
    7. import android.content.DialogInterface.OnClickListener;  
    8. import android.content.Intent;  
    9. import android.os.Bundle;  
    10. import android.view.Window;  
    11. import android.view.WindowManager;  
    12. import android.widget.EditText;  
    13. import android.widget.TableLayout;  
    14.   
    15. public class GetIP extends Activity {  
    16.     String ipname = null;  
    17.   
    18.     @Override  
    19.     public void onCreate(Bundle savedInstanceState) {  
    20.         super.onCreate(savedInstanceState);  
    21.         // 设置全屏  
    22.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
    23.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
    24.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    25.         setContentView(R.layout.main);  
    26.   
    27.         final Builder builder = new AlertDialog.Builder(this); // 定义一个AlertDialog.Builder对象  
    28.         builder.setTitle("登录服务器对话框"); // 设置对话框的标题  
    29.   
    30.         // 装载/res/layout/login.xml界面布局  
    31.         TableLayout loginForm = (TableLayout) getLayoutInflater().inflate(  
    32.                 R.layout.login, null);  
    33.         final EditText iptext = (EditText) loginForm  
    34.                 .findViewById(R.id.ipedittext);  
    35.         builder.setView(loginForm); // 设置对话框显示的View对象  
    36.         // 为对话框设置一个“登录”按钮  
    37.         builder.setPositiveButton("登录"  
    38.         // 为按钮设置监听器  
    39.                 , new OnClickListener() {  
    40.                     @Override  
    41.                     public void onClick(DialogInterface dialog, int which) {  
    42.                         // 此处可执行登录处理  
    43.                         ipname = iptext.getText().toString().trim();  
    44.                         Bundle data = new Bundle();  
    45.                         data.putString("ipname", ipname);  
    46.                         Intent intent = new Intent(GetIP.this,  
    47.                                 Camera_socket.class);  
    48.                         intent.putExtras(data);  
    49.                         startActivity(intent);  
    50.                     }  
    51.                 });  
    52.         // 为对话框设置一个“取消”按钮  
    53.         builder.setNegativeButton("取消", new OnClickListener() {  
    54.             @Override  
    55.             public void onClick(DialogInterface dialog, int which) {  
    56.                 // 取消登录,不做任何事情。  
    57.                 System.exit(1);  
    58.             }  
    59.         });  
    60.         // 创建、并显示对话框  
    61.         builder.create().show();  
    62.     }  
    63. }  


    [html] view plain copy
     print?
    1. package com.zte.camera_socket;  
    2.   
    3. import java.io.ByteArrayInputStream;  
    4. import java.io.ByteArrayOutputStream;  
    5. import java.io.IOException;  
    6. import java.io.OutputStream;  
    7. import java.net.Socket;  
    8.   
    9. import android.app.Activity;  
    10. import android.content.Intent;  
    11. import android.graphics.ImageFormat;  
    12. import android.graphics.Rect;  
    13. import android.graphics.YuvImage;  
    14. import android.hardware.Camera;  
    15. import android.hardware.Camera.Size;  
    16. import android.os.Bundle;  
    17. import android.util.Log;  
    18. import android.view.SurfaceHolder;  
    19. import android.view.SurfaceHolder.Callback;  
    20. import android.view.SurfaceView;  
    21. import android.view.Window;  
    22. import android.view.WindowManager;  
    23.   
    24. public class Camera_socket extends Activity {  
    25.     SurfaceView sView;  
    26.     SurfaceHolder surfaceHolder;  
    27.     int screenWidth, screenHeight;  
    28.     Camera camera; // 定义系统所用的照相机  
    29.     boolean isPreview = false; // 是否在浏览中  
    30.     private String ipname;  
    31.   
    32.     @SuppressWarnings("deprecation")  
    33.     @Override  
    34.     public void onCreate(Bundle savedInstanceState) {  
    35.         super.onCreate(savedInstanceState);  
    36.         // 设置全屏  
    37.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
    38.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
    39.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    40.         setContentView(R.layout.main);  
    41.   
    42.         // 获取IP地址  
    43.         Intent intent = getIntent();  
    44.         Bundle data = intent.getExtras();  
    45.         ipname = data.getString("ipname");  
    46.   
    47.         screenWidth = 640;  
    48.         screenHeight = 480;  
    49.         sView = (SurfaceView) findViewById(R.id.sView); // 获取界面中SurfaceView组件  
    50.         surfaceHolder = sView.getHolder(); // 获得SurfaceView的SurfaceHolder  
    51.   
    52.         // 为surfaceHolder添加一个回调监听器  
    53.         surfaceHolder.addCallback(new Callback() {  
    54.             @Override  
    55.             public void surfaceChanged(SurfaceHolder holder, int format,  
    56.                     int width, int height) {  
    57.             }  
    58.   
    59.             @Override  
    60.             public void surfaceCreated(SurfaceHolder holder) {  
    61.                 initCamera(); // 打开摄像头  
    62.             }  
    63.   
    64.             @Override  
    65.             public void surfaceDestroyed(SurfaceHolder holder) {  
    66.                 // 如果camera不为null ,释放摄像头  
    67.                 if (camera != null) {  
    68.                     if (isPreview)  
    69.                         camera.stopPreview();  
    70.                     camera.release();  
    71.                     camera = null;  
    72.                 }  
    73.                 System.exit(0);  
    74.             }  
    75.         });  
    76.         // 设置该SurfaceView自己不维护缓冲  
    77.         surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
    78.   
    79.     }  
    80.   
    81.     private void initCamera() {  
    82.         if (!isPreview) {  
    83.             camera = Camera.open();  
    84.         }  
    85.         if (camera != null && !isPreview) {  
    86.             try {  
    87.                 Camera.Parameters parameters = camera.getParameters();  
    88.                 parameters.setPreviewSize(screenWidth, screenHeight); // 设置预览照片的大小  
    89.                 parameters.setPreviewFpsRange(20, 30); // 每秒显示20~30帧  
    90.                 parameters.setPictureFormat(ImageFormat.NV21); // 设置图片格式  
    91.                 parameters.setPictureSize(screenWidth, screenHeight); // 设置照片的大小  
    92.                 // camera.setParameters(parameters); // android2.3.3以后不需要此行代码  
    93.                 camera.setPreviewDisplay(surfaceHolder); // 通过SurfaceView显示取景画面  
    94.                 camera.setPreviewCallback(new StreamIt(ipname)); // 设置回调的类  
    95.                 camera.startPreview(); // 开始预览  
    96.                 camera.autoFocus(null); // 自动对焦  
    97.             } catch (Exception e) {  
    98.                 e.printStackTrace();  
    99.             }  
    100.             isPreview = true;  
    101.         }  
    102.     }  
    103.   
    104. }  
    105.   
    106. class StreamIt implements Camera.PreviewCallback {  
    107.     private String ipname;  
    108.   
    109.     public StreamIt(String ipname) {  
    110.         this.ipname = ipname;  
    111.     }  
    112.   
    113.     @Override  
    114.     public void onPreviewFrame(byte[] data, Camera camera) {  
    115.         Size size = camera.getParameters().getPreviewSize();  
    116.         try {  
    117.             // 调用image.compressToJpeg()将YUV格式图像数据data转为jpg格式  
    118.             YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width,  
    119.                     size.height, null);  
    120.             if (image != null) {  
    121.                 ByteArrayOutputStream outstream = new ByteArrayOutputStream();  
    122.                 image.compressToJpeg(new Rect(0, 0, size.width, size.height),  
    123.                         80, outstream);  
    124.                 outstream.flush();  
    125.                 // 启用线程将图像数据发送出去  
    126.                 Thread th = new MyThread(outstream, ipname);  
    127.                 th.start();  
    128.             }  
    129.         } catch (Exception ex) {  
    130.             Log.e("Sys", "Error:" + ex.getMessage());  
    131.         }  
    132.     }  
    133. }  
    134.   
    135. class MyThread extends Thread {  
    136.     private byte byteBuffer[] = new byte[1024];  
    137.     private OutputStream outsocket;  
    138.     private ByteArrayOutputStream myoutputstream;  
    139.     private String ipname;  
    140.   
    141.     public MyThread(ByteArrayOutputStream myoutputstream, String ipname) {  
    142.         this.myoutputstream = myoutputstream;  
    143.         this.ipname = ipname;  
    144.         try {  
    145.             myoutputstream.close();  
    146.         } catch (IOException e) {  
    147.             e.printStackTrace();  
    148.         }  
    149.     }  
    150.   
    151.     public void run() {  
    152.         try {  
    153.             // 将图像数据通过Socket发送出去  
    154.             Socket tempSocket = new Socket(ipname, 6000);  
    155.             outsocket = tempSocket.getOutputStream();  
    156.             ByteArrayInputStream inputstream = new ByteArrayInputStream(  
    157.                     myoutputstream.toByteArray());  
    158.             int amount;  
    159.             while ((amount = inputstream.read(byteBuffer)) != -1) {  
    160.                 outsocket.write(byteBuffer, 0, amount);  
    161.             }  
    162.             myoutputstream.flush();  
    163.             myoutputstream.close();  
    164.             tempSocket.close();  
    165.         } catch (IOException e) {  
    166.             e.printStackTrace();  
    167.         }  
    168.     }  
    169.   
    170. }  

    xml:

    login.xml:

    [html] view plain copy
     print?
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:id="@+id/loginForm"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     android:orientation="vertical" >  
    7.   
    8.     <TableRow>  
    9.   
    10.         <TextView  
    11.             android:layout_width="fill_parent"  
    12.             android:layout_height="wrap_content"  
    13.             android:text="IP:"  
    14.             android:textSize="10pt" />  
    15.         <!-- 输入用户名的文本框 -->  
    16.   
    17.         <EditText  
    18.             android:id="@+id/ipedittext"  
    19.             android:layout_width="fill_parent"  
    20.             android:layout_height="wrap_content"  
    21.             android:digits="0123456789."  
    22.             android:hint="请填写服务器IP"  
    23.             android:selectAllOnFocus="true" />  
    24.     </TableRow>  
    25.   
    26. </TableLayout>  

    main.xml:

    [html] view plain copy
     print?
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:orientation="vertical" >  
    6.   
    7.     <SurfaceView  
    8.         android:id="@+id/sView"  
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="fill_parent"  
    11.         android:scaleType="fitCenter" />  
    12.   
    13. </LinearLayout>  


    PC端:

    [html] view plain copy
     print?
    1. import java.awt.Dimension;  
    2. import java.awt.Graphics;  
    3. import java.awt.Image;  
    4. import java.awt.Toolkit;  
    5. import java.awt.event.ActionEvent;  
    6. import java.awt.event.ActionListener;  
    7. import java.io.File;  
    8. import java.io.FileNotFoundException;  
    9. import java.io.IOException;  
    10. import java.io.InputStream;  
    11. import java.io.RandomAccessFile;  
    12. import java.net.ServerSocket;  
    13. import java.net.Socket;  
    14.   
    15. import javax.imageio.ImageIO;  
    16. import javax.swing.JButton;  
    17. import javax.swing.JFileChooser;  
    18. import javax.swing.JFrame;  
    19. import javax.swing.JPanel;  
    20.   
    21. public class pc {  
    22.     public static ServerSocket ss = null;  
    23.   
    24.     public static void main(String args[]) throws IOException {  
    25.         ss = new ServerSocket(6000);  
    26.   
    27.         final ImageFrame frame = new ImageFrame(ss);  
    28.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    29.         frame.setVisible(true);  
    30.   
    31.         while (true) {  
    32.             frame.panel.getimage();  
    33.             frame.repaint();  
    34.         }  
    35.     }  
    36.   
    37. }  
    38.   
    39. /**  
    40.  * A frame with an image panel  
    41.  */  
    42. @SuppressWarnings("serial")  
    43. class ImageFrame extends JFrame {  
    44.     public ImagePanel panel;  
    45.     public JButton jb;  
    46.   
    47.     public ImageFrame(ServerSocket ss) {  
    48.         // get screen dimensions  
    49.         Toolkit kit = Toolkit.getDefaultToolkit();  
    50.         Dimension screenSize = kit.getScreenSize();  
    51.         int screenHeight = screenSize.height;  
    52.         int screenWidth = screenSize.width;  
    53.   
    54.         // center frame in screen  
    55.         setTitle("ImageTest");  
    56.         setLocation((screenWidth - DEFAULT_WIDTH) / 2,  
    57.                 (screenHeight - DEFAULT_HEIGHT) / 2);  
    58.         setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);  
    59.   
    60.         // add panel to frame  
    61.         this.getContentPane().setLayout(null);  
    62.         panel = new ImagePanel(ss);  
    63.         panel.setSize(640, 480);  
    64.         panel.setLocation(0, 0);  
    65.         add(panel);  
    66.         jb = new JButton("拍照");  
    67.         jb.setBounds(0, 480, 640, 50);  
    68.         add(jb);  
    69.         saveimage saveaction = new saveimage(ss);  
    70.         jb.addActionListener(saveaction);  
    71.     }  
    72.   
    73.     public static final int DEFAULT_WIDTH = 640;  
    74.     public static final int DEFAULT_HEIGHT = 560;  
    75. }  
    76.   
    77. /**  
    78.  * A panel that displays a tiled image  
    79.  */  
    80. @SuppressWarnings("serial")  
    81. class ImagePanel extends JPanel {  
    82.     private ServerSocket ss;  
    83.     private Image image;  
    84.     private InputStream ins;  
    85.   
    86.     public ImagePanel(ServerSocket ss) {  
    87.         this.ss = ss;  
    88.     }  
    89.   
    90.     public void getimage() throws IOException {  
    91.         Socket s = this.ss.accept();  
    92.         System.out.println("连接成功!");  
    93.         this.ins = s.getInputStream();  
    94.         this.image = ImageIO.read(ins);  
    95.         this.ins.close();  
    96.     }  
    97.   
    98.     public void paintComponent(Graphics g) {  
    99.         super.paintComponent(g);  
    100.         if (image == null)  
    101.             return;  
    102.         g.drawImage(image, 0, 0, null);  
    103.     }  
    104.   
    105. }  
    106.   
    107. class saveimage implements ActionListener {  
    108.     RandomAccessFile inFile = null;  
    109.     byte byteBuffer[] = new byte[1024];  
    110.     InputStream ins;  
    111.     private ServerSocket ss;  
    112.   
    113.     public saveimage(ServerSocket ss) {  
    114.         this.ss = ss;  
    115.     }  
    116.   
    117.     public void actionPerformed(ActionEvent event) {  
    118.         try {  
    119.             Socket s = ss.accept();  
    120.             ins = s.getInputStream();  
    121.   
    122.             // 文件选择器以当前的目录打开  
    123.             JFileChooser jfc = new JFileChooser(".");  
    124.             jfc.showSaveDialog(new javax.swing.JFrame());  
    125.             // 获取当前的选择文件引用  
    126.             File savedFile = jfc.getSelectedFile();  
    127.   
    128.             // 已经选择了文件  
    129.             if (savedFile != null) {  
    130.                 // 读取文件的数据,可以每次以快的方式读取数据  
    131.                 try {  
    132.                     inFile = new RandomAccessFile(savedFile, "rw");  
    133.                 } catch (FileNotFoundException e) {  
    134.                     e.printStackTrace();  
    135.                 }  
    136.             }  
    137.   
    138.             int amount;  
    139.             while ((amount = ins.read(byteBuffer)) != -1) {  
    140.                 inFile.write(byteBuffer, 0, amount);  
    141.             }  
    142.             inFile.close();  
    143.             ins.close();  
    144.             s.close();  
    145.             javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(),  
    146.                     "已接保存成功", "提示!", javax.swing.JOptionPane.PLAIN_MESSAGE);  
    147.         } catch (IOException e) {  
    148.   
    149.             e.printStackTrace();  
    150.         }  
    151.     }  
    152. }  

    下面是在虚拟机上测试的效果:

  • 相关阅读:
    修改mysql root用户密码(忘记密码)
    激活IDEA 2019.1
    数据库事务的4大特性与隔离级别
    使用HttpClient调用第三方接口
    SpringBoot使用logback自定义配置时遇到的坑 --- 在 /tmp目录下自动生成spring.log文件
    更新数据库中数据时出现: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences 问题
    数值return ++ 的坑
    string整合Quartz定时器
    idea配置自动编译项目配置
    CASE WHEN 及 SELECT CASE WHEN的用法
  • 原文地址:https://www.cnblogs.com/nafio/p/9137305.html
Copyright © 2020-2023  润新知