欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)手機(jī)監(jiān)控?cái)z像頭

 更新時(shí)間:2022年03月28日 15:29:00   作者:wenix1988  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手機(jī)監(jiān)控?cái)z像頭,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一直想在自己的Android手機(jī)上實(shí)現(xiàn)一個(gè)手機(jī)監(jiān)控?cái)z像頭功能。今天逛論壇,看到一個(gè)例子,于是做了出來(lái),留著以后完善。

功能點(diǎn):

1、Android和PC通過socket通信。

2、Android下Camera的使用。

看代碼:

package com.wenix.androidcameramonitor;
?
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);
?? ?// 設(shè)置全屏
?? ?requestWindowFeature(Window.FEATURE_NO_TITLE);
?? ?getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
?? ??? ?WindowManager.LayoutParams.FLAG_FULLSCREEN);
?? ?setContentView(R.layout.activity_main);
?
?? ?final Builder builder = new AlertDialog.Builder(this); // 定義一個(gè)AlertDialog.Builder對(duì)象
?? ?builder.setTitle("登錄服務(wù)器對(duì)話框"); // 設(shè)置對(duì)話框的標(biāo)題
?
?? ?// 裝載/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); // 設(shè)置對(duì)話框顯示的View對(duì)象
?? ?// 為對(duì)話框設(shè)置一個(gè)“登錄”按鈕
?? ?builder.setPositiveButton("登錄"
?? ?// 為按鈕設(shè)置監(jiān)聽器
?? ??? ?, new OnClickListener() {
?? ??? ? ? ?@Override
?? ??? ? ? ?public void onClick(DialogInterface dialog, int which) {
?? ??? ??? ?// 此處可執(zhí)行登錄處理
?? ??? ??? ?ipname = iptext.getText().toString().trim();
?? ??? ??? ?Bundle data = new Bundle();
?? ??? ??? ?data.putString("ipname", ipname);
?? ??? ??? ?Intent intent = new Intent(GetIP.this, MainActivity.class);
?? ??? ??? ?intent.putExtras(data);
?? ??? ??? ?startActivity(intent);
?? ??? ? ? ?}
?? ??? ?});
?? ?// 為對(duì)話框設(shè)置一個(gè)“取消”按鈕
?? ?builder.setNegativeButton("取消", new OnClickListener() {
?? ? ? ?@Override
?? ? ? ?public void onClick(DialogInterface dialog, int which) {
?? ??? ?// 取消登錄,不做任何事情。
?? ??? ?System.exit(1);
?? ? ? ?}
?? ?});
?? ?// 創(chuàng)建、并顯示對(duì)話框
?? ?builder.create().show();
? ? }
}

獲取ip后就跳轉(zhuǎn)到MainActivity。

package com.wenix.androidcameramonitor;
?
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 MainActivity extends Activity {
?? ?SurfaceView sView;
?? ?SurfaceHolder surfaceHolder;
?? ?int screenWidth, screenHeight;
?? ?Camera camera; // 定義系統(tǒng)所用的照相機(jī)
?? ?boolean isPreview = false; // 是否在瀏覽中
?? ?private String ipname;
?
?? ?@Override
?? ?public void onCreate(Bundle savedInstanceState) {
?? ??? ?super.onCreate(savedInstanceState);
?? ??? ?// 設(shè)置全屏
?? ??? ?requestWindowFeature(Window.FEATURE_NO_TITLE);
?? ??? ?getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
?? ??? ?setContentView(R.layout.activity_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添加一個(gè)回調(diào)監(jiān)聽器
?? ??? ?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);
?? ??? ??? ?}
?? ??? ?});
?? ??? ?// 設(shè)置該SurfaceView自己不維護(hù)緩沖
?? ??? ?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); // 設(shè)置預(yù)覽照片的大小
?? ??? ??? ??? ?parameters.setPreviewFpsRange(20, 30); // 每秒顯示20~30幀
?? ??? ??? ??? ?parameters.setPictureFormat(ImageFormat.NV21); // 設(shè)置圖片格式
?? ??? ??? ??? ?parameters.setPictureSize(screenWidth, screenHeight); // 設(shè)置照片的大小
?? ??? ??? ??? ?// camera.setParameters(parameters); // android2.3.3以后不需要此行代碼
?? ??? ??? ??? ?camera.setPreviewDisplay(surfaceHolder); // 通過SurfaceView顯示取景畫面
?? ??? ??? ??? ?camera.setPreviewCallback(new StreamIt(ipname)); // 設(shè)置回調(diào)的類
?? ??? ??? ??? ?camera.startPreview(); // 開始預(yù)覽
?? ??? ??? ??? ?camera.autoFocus(null); // 自動(dòng)對(duì)焦
?? ??? ??? ?} 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 {
?? ??? ??? ?// 調(diào)用image.compressToJpeg()將YUV格式圖像數(shù)據(jù)data轉(zhuǎn)為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();
?? ??? ??? ??? ?// 啟用線程將圖像數(shù)據(jù)發(fā)送出去
?? ??? ??? ??? ?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 {
?? ??? ??? ?// 將圖像數(shù)據(jù)通過Socket發(fā)送出去
?? ??? ??? ?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();
?? ??? ?}
?? ?}
?
}

這樣就打開了socket,然后把camera獲取的數(shù)據(jù)發(fā)送到PC端。

PC端代碼:

package com.wenix;
?
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 ImageServer { ? ? ? ?
? ? 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();
? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? // 文件選擇器以當(dāng)前的目錄打開
? ? ? ? ? ? ? ? JFileChooser jfc = new JFileChooser(".");
? ? ? ? ? ? ? ? jfc.showSaveDialog(new javax.swing.JFrame());
? ? ? ? ? ? ? ? // 獲取當(dāng)前的選擇文件引用
? ? ? ? ? ? ? ? File savedFile = jfc.getSelectedFile();
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? // 已經(jīng)選擇了文件
? ? ? ? ? ? ? ? if (savedFile != null) {
? ? ? ? ? ? ? ? ? ? // 讀取文件的數(shù)據(jù),可以每次以快的方式讀取數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? 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();
? ? ? ? ? ? ? ? }
? ? ? ? }
}

運(yùn)行結(jié)果如下:

手機(jī)視頻:

pc端視頻:

可以看到視頻數(shù)據(jù)已經(jīng)上傳到了PC端。

接下來(lái)要完善的地方:

1.Android端可以提供一個(gè)Url,然后PC端使用瀏覽器來(lái)瀏覽。

2.PC端添加視頻錄制功能。

3.添加圖像檢測(cè)功能,比如運(yùn)動(dòng)物體檢測(cè)等,這樣就可以擴(kuò)展為監(jiān)控?cái)z像頭了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論