桥下红药

以无心之心读书

Android调用IPC网络摄像头的操作

总分类 0 评

正常情况下网络摄像头需要和设备处于同一个局域网内,这种情况下就可以侦测到网络摄像头设备,否则只能指定IP地址。

网络摄像头基本都是支持 Onvif 协议的,购买前要注意下是否支持。Onvif 协议其实就是个 WebService,文档地址:https://www.onvif.org/profiles/specifications/

# 笔记开始

//发送数据 侦探网络中的 摄像头设备
public static final String DISCOVERY_PROBE_TDS = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Envelope xmlns:tds=\"http://www.onvif.org/ver10/device/wsdl\" xmlns=\"http://www.w3.org/2003/05/soap-envelope\"><Header><wsa:MessageID xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">uuid:5101931c-dd3e-4f14-a8aa-c46144af3433</wsa:MessageID><wsa:To xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To><wsa:Action xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</wsa:Action></Header><Body><Probe xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\"><Types>dn:NetworkVideoTransmitter</Types><Scopes /></Probe></Body></Envelope>";
// 广播端口
public static final int BROADCAST_SERVER_PORT = 3702;
1. 组播IP地址的获取
private static String getBroadcast() throws SocketException {
    System.setProperty("java.net.preferIPv4Stack", "true");
    for (Enumeration<NetworkInterface> niEnum = NetworkInterface
            .getNetworkInterfaces(); niEnum.hasMoreElements(); ) {
        NetworkInterface ni = niEnum.nextElement();
        if (!ni.isLoopback()) {
            for (InterfaceAddress interfaceAddress : ni
                    .getInterfaceAddresses()) {
                if (interfaceAddress.getBroadcast() != null) {
                    return interfaceAddress.getBroadcast().toString()
                            .substring(1);
                }
            }
        }
    }
    return null;
}

返回结果里面(XML)就会有 网络摄像头设备的节点信息(IP地址等),有了IP地址接下来的操作就和 WebServices 一样了,往地址发送指定格式的XML数据即可!

  • 举个栗子(获取网络摄像头的直播流地址)
public static final String GET_URI_BODY = "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Header><wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><wsse:UsernameToken><wsse:Username>%s</wsse:Username><wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">%s</wsse:Password><wsse:Nonce>%s</wsse:Nonce><wsu:Created>%s</wsu:Created></wsse:UsernameToken></wsse:Security></s:Header><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><GetStreamUri xmlns=\"http://www.onvif.org/ver10/media/wsdl\"><StreamSetup><Stream xmlns=\"http://www.onvif.org/ver10/schema\">RTP-Unicast</Stream><Transport xmlns=\"http://www.onvif.org/ver10/schema\"><Protocol>RTSP</Protocol></Transport></StreamSetup><ProfileToken>%s</ProfileToken></GetStreamUri></s:Body></s:Envelope>";

IPC摄像头Demo 点击可下载

## Run Demo
//启动 网络摄像头服务
private void IPCameraInit() {

    ipCameraManager = new IPCameraManager();
    ipCameraManager.setListener(cameras -> {

        Log.i(TAG, "search result " + cameras);
        if (cameras.size() <= 0) {
            return;
        }
        CameraDevice2 device2 = cameras.get(0);
        device2.setSecurity("", "");
        device2.setOnSoapDoneListener((device, success) -> {
            cameraRtsp = device.getRtspUri();
            runOnUiThread(() -> txtRtsp.setText(cameraRtsp));
        });
        device2.IPCamInit();

    });
    ipCameraManager.openSearch();
}

上一篇

发表评论