In the past, I had always planned to make a video communication software based on UDP communication on the local area network. I did not know how the video stream was transmitted on the network. Although I know the encoding format of several video streams, I have not actually put it into practice. When learning Android watch development, I saw a project on the github that transferred the real-time video of the mobile phone to the watch, and found a LAN video transmission method that seems to be low-grade but should be more practical on the local area network: directly compress the image obtained from the camera. Send it.This project GitHub homepage: https://github.com/retravel/AndroidRealTimeVideo
Get webcam real-time pictures, UDP send pictures
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
Private void initCanmera() { Int cameras = Camera.getNumberOfCameras(); Camera.CameraInfo info = new Camera.CameraInfo(); For (int i = 0; i < cameras; i++) { Camera.getCameraInfo(i, info); If (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { Camera = Camera.open(i); Break; } } //No front camera If (camera == null) camera = Camera.open(); Try { camera.setPreviewDisplay(surfaceHolder); camera.setPreviewCallback(this); } Catch (Exception e) { Camera.release();//release resources Camera = null; } } @Override Public void onPreviewFrame(byte[] data, Camera camera) { Camera.Size previewSize = camera.getParameters().getPreviewSize(); Int[] rgb = decodeYUV420SP(data,previewSize.width, previewSize.height); Bitmap bmp = Bitmap.createBitmap(rgb, previewSize.width, previewSize.height, Bitmap.Config.ARGB_8888); Int smallWidth, smallHeight; Int dimension = 200; If (previewSize.width > previewSize.height) { smallWidth = dimension; smallHeight = dimension * previewSize.height / previewSize.width; } Else { smallHeight = dimension; smallWidth = dimension *previewSize.width / previewSize.height; } Matrix matrix = new Matrix(); matrix.postRotate(mCameraOrientation); Bitmap bmpSmall = Bitmap.createScaledBitmap(bmp, smallWidth, smallHeight, false); Bitmap bmpSmallRotated = Bitmap.createBitmap(bmpSmall, 0, 0, smallWidth, smallHeight, matrix, false); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bmpSmallRotated.compress(Bitmap.CompressFormat.WEBP, 80, baos); up.sendMsg(baos.toByteArray()); } @Override Public void surfaceCreated(SurfaceHolder holder) { initCanmera(); } @Override Public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Int currentCamera = Camera.CameraInfo.CAMERA_FACING_FRONT; Camera.Parameters parameters = camera.getParameters();//Get camera settings Camera.Size size = camera.getParameters().getPreviewSize(); //Get preview size parameters.setPictureFormat(PixelFormat.JPEG);//Set the picture format Camera.CameraInfo info = new Camera.CameraInfo(); camera.getCameraInfo(currentCamera, info); Int rotation = this.getWindowManager().getDefaultDisplay().getRotation(); Int degrees = 0; Switch (rotation) { Case Surface.ROTATION_0: Degrees = 0; Break; Case Surface.ROTATION_90: Degrees = 90; Break; Case Surface.ROTATION_180: Degrees = 180; Break; Case Surface.ROTATION_270: Degrees = 270; Break; } Int resultA = 0, resultB = 0; If (currentCamera == Camera.CameraInfo.CAMERA_FACING_BACK) { resultA = (info.orientation - degrees + 360) % 360; resultB = (info.orientation - degrees + 360) % 360; camera.setDisplayOrientation(resultA); } Else { resultA = (360 + 360 - info.orientation - degrees) % 360; resultB = (info.orientation + degrees) % 360; camera.setDisplayOrientation(resultA); } camera.setPreviewCallback(this); parameters.setRotation(resultB); mCameraOrientation = resultB; camera.setParameters(parameters); camera.startPreview();//Start preview } @Override Public void surfaceDestroyed(SurfaceHolder holder) { If (camera != null) { camera.setPreviewCallback(null); camera.stopPreview(); Camera.release(); Camera = null; } } |
UDP server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
Package cn.wangbaiyuan.androidsocketav; /** * Created by BrainWang on 2016/3/21. */ Import java.io.IOException; Import java.net.DatagramPacket; Import java.net.DatagramSocket; Import java.net.InetSocketAddress; Import java.net.SocketAddress; Import java.net.SocketException; /** * Server based on UDP protocol to respond to packets from clients * * @author <a href="http://wangbaiyuan">Wang Baiyuan</a> */ Public class UDPServer { /** * Port */ //int port=1888; DatagramSocket socket; String lastString = (-1 + ""); Int sameTime = 0; Public handleReceiveData callback; Public UDPServer(int port) throws SocketException { Socket = new DatagramSocket(port); //Server DatagramSocket System.out.println("Server started."); } Public void setReceiveCallback(handleReceiveData call) { Callback = call; } Public void service() throws IOException { While (true) { DatagramPacket dp = new DatagramPacket(new byte[102400], 102400); Socket.receive(dp); // Receive client information Byte[] data = dp.getData(); callback.handleReceive(data); } } Public void start() throws SocketException, IOException { Service(); } Public void sendMsg(final byte[] data) { Thread send = new Thread(new Runnable() { @Override Public void run() { Try { If (socket != null) { SocketAddress socketAddres = new InetSocketAddress("192.168.1.110", 8804); Socket.send(new DatagramPacket(data, data.length, socketAddres)); } } Catch (IOException e) { e.printStackTrace(); } } }); Send.start(); } } |
This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!
Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/local-area-network-lan-simple-udp-video-calls-2.html
No Comment