{"id":17625358,"url":"https://github.com/laheller/ptplibrary","last_synced_at":"2025-04-23T18:20:50.565Z","repository":{"id":163430728,"uuid":"238908451","full_name":"laheller/ptplibrary","owner":"laheller","description":"use ptp/ip and ptp/usb to connect to cameras like Canon and Nikon","archived":false,"fork":false,"pushed_at":"2020-02-07T10:48:33.000Z","size":82,"stargazers_count":27,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T02:14:13.400Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/laheller.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-07T11:40:26.000Z","updated_at":"2025-01-30T00:53:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"5c078016-c870-4a31-a1c1-2b5269b75e7f","html_url":"https://github.com/laheller/ptplibrary","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laheller%2Fptplibrary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laheller%2Fptplibrary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laheller%2Fptplibrary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laheller%2Fptplibrary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laheller","download_url":"https://codeload.github.com/laheller/ptplibrary/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250487632,"owners_count":21438628,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-22T22:42:04.444Z","updated_at":"2025-04-23T18:20:50.546Z","avatar_url":"https://github.com/laheller.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# ptplibrary\nuse ptp/ip and ptp/usb to connect to cameras like Canon and Nikon\n\n## Setup\n### Add dependency \n```Gradle\n//in build.gradle of module\ndependencies {\n    implementation 'com.github.rupiapps:ptplibrary:1.0'\n}\n//in build.gradle of project\nallprojects {\n    repositories {\n        maven {\n            url \"https://jitpack.io\"\n        }\n    }\n}\n```\n### Add permissions to manifest file\n```Xml\n\u003cuses-permission android:name=\"android.permission.INTERNET\"/\u003e\n```\n\n## Example usage\nDo not run this on mainthread otherwise you will get NetworkOnMainthreadException\n```Java\n            //this is only a test code\n            //do not make try-catch-block around all your code\n            try\n            {\n                System.out.println(\"start connect\");\n\n                //1. create a connection object by either using ptp/ip or ptp/usb\n                InetAddress host = InetAddress.getByName(\"192.168.1.1\");\n                PtpConnection connection = PtpConnection.create(host);\n\n                //2. connect to the host with your guid and name\n                //   provide a callback to give feedback to the user\n                //   if connection needs to be acknowledged\n                connection.connectAndInit(\n                        //16 byte guid\n                        new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\n                                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},\n                        \"appname\",\n                        () -\u003e System.out.println(\"waiting for acknowledge\")\n                );\n\n                //3. use connection object to create a session\n                PtpSession session = new PtpSession(connection);\n\n                //4. get deviceinfo - some cameras need to have open session to get this\n                Packet data = session.getDeviceInfo(true);\n                DeviceInfoPacket deviceInfo = new DeviceInfoPacket(data);\n\n                //check if valid\n                if(deviceInfo.getBufferSize()\u003e10)\n                {\n                    System.out.println(deviceInfo.getModel());\n                    System.out.println(deviceInfo.getManufacturer());\n                }\n\n                //5. close the session after use\n                session.closeAndDisconnect();\n            }\n            catch(InitFailException ife)\n            {\n                System.out.println(\"init failed: \" + ife.getReason());\n            }\n            catch(Exception e)\n            {\n                e.printStackTrace();\n            }\n```\nOutput: \n```\nI/System.out: start connect\nI/System.out: waiting for acknowledge\nI/System.out: D5300\nI/System.out: Nikon Corporation\n```\n## Using ptp/usb\nFor usb connections you will have to implement PtpUsbEndpoints on your operating system. \nAn implementation for Android is not part of this project to keep the project independent from android.\n\nHere is how it could look like:\n```Java\npublic class AndroidUsbEndpoints implements PtpUsbEndpoints\n{\n\tprivate static int DEFAULT_TIMEOUT = 10000;\n\tprivate UsbDeviceConnection usbconnection;\n\tprivate UsbInterface usbinterface;\n\tprivate UsbEndpoint data_out, data_in, interrupt;\n\tprivate int timeout;\n\t\n\t\n\tpublic AndroidUsbEndpoints(UsbDeviceConnection usbconnection, UsbInterface usbinterface)\n\t{\n\t\tthis.usbconnection = usbconnection;\n\t\tthis.usbinterface = usbinterface;\n\t\ttimeout = DEFAULT_TIMEOUT;\n\t}\n\t\n\tpublic void initalize() throws InitFailException\n\t{\n\t\tif(!usbconnection.claimInterface(usbinterface, true))\n\t\t\tthrow new InitFailException(InitFailReason.ClaimFailed);\n\t\t\n\t\tif(usbinterface.getEndpointCount()\u003c3)\n\t\t\tthrow new InitFailException(InitFailReason.NoEndpoints);\n\t\t\n\t\tfor(int i=0; i\u003cusbinterface.getEndpointCount(); i++)\n\t\t{\n\t\t\tUsbEndpoint ep = usbinterface.getEndpoint(i);\n\t\t\t\n\t\t\tboolean isout = ep.getDirection()==UsbConstants.USB_DIR_OUT;\n\t\t\tboolean isbulk = ep.getType()==UsbConstants.USB_ENDPOINT_XFER_BULK;\n\t\t\t\n\t\t\tif(isout \u0026\u0026 isbulk)\n\t\t\t\tdata_out = ep;\n\t\t\tif(!isout \u0026\u0026 isbulk)\n\t\t\t\tdata_in = ep;\n\t\t\tif(!isbulk)\n\t\t\t\tinterrupt = ep;\t\t\t\n\t\t}\n\t\t\n\t\tif(data_out==null || data_in==null || interrupt==null)\n\t\t\tthrow new InitFailException(InitFailReason.NoEndpoints);\n\t}\n\t\n\tpublic void release()\n\t{\n\t\tusbconnection.releaseInterface(usbinterface);\n\t}\n\t\n\tpublic int controlTransfer(int requestType, int request, int value, int index, byte[] buffer)\n\t{\n\t\treturn usbconnection.controlTransfer(requestType, request, value, index, buffer, buffer!=null?buffer.length:0, 1500);\n\t}\n\n\tpublic void setTimeOut(int to)\n\t{\n\t\ttimeout = to\u003e0?to:DEFAULT_TIMEOUT;\n\t}\n\t\n\tpublic int writeDataOut(byte[] buffer, int length) throws SendDataException\n\t{\n\t\tint len = usbconnection.bulkTransfer(data_out, buffer, length, timeout);\n\t\tif(len\u003c0)\n\t\t\tthrow new SendDataException(\"senderror: len is \"+len);\n\t\treturn len;\n\t}\n\t\n\tpublic int readDataIn(byte[] buffer) throws ReceiveDataException\n\t{\n\t\tint len=0; \n\t\twhile(len==0)\t\t\n\t\t  len = usbconnection.bulkTransfer(data_in, buffer, buffer!=null?buffer.length:0, timeout);\n\t\tif(len\u003c0)\n\t\t\tthrow new ReceiveDataException(\"receiveerror: len is \"+len);\n\t\treturn len;\n\t}\n\t\n\tpublic void readEvent(byte[] buffer, boolean bulk)\n\t{\t\t\n\t\tif(bulk)\n\t\t\tusbconnection.bulkTransfer(interrupt, buffer, buffer.length, 500);\n\t\telse\n\t\t{\n\t\t\tUsbRequest req = new UsbRequest();\n\t\t\treq.initialize(usbconnection, interrupt);\t\t\n\t\t\tByteBuffer bbuffer = ByteBuffer.wrap(buffer);\n\t\t\treq.queue(bbuffer, buffer.length);\t\t\t\t\t\t\t\n\t\t\treq = usbconnection.requestWait();\n\t\t\tPtpLog.debug(\"received event\");\n\t\t\tif(req!=null)\n\t\t\t{\n\t\t\t\tPtpLog.debug(\"req != null\");\n\t\t\t\tPtpLog.debug(new Packet(bbuffer.array()).toString());\n\t\t\t}\t\n\t\t\telse\n\t\t\t\tPtpLog.debug(\"event is null\");\n\t\t}\n\t\t\n\t}\n\t\n\tpublic int getMaxPacketSizeOut()\n\t{\n\t\treturn data_out.getMaxPacketSize();\n\t}\n\t\n\tpublic int getMaxPacketSizeIn()\n\t{\n\t\treturn data_in.getMaxPacketSize();\n\t}\n\t\n\tpublic int getMaxPacketSizeInterrupt()\n\t{\n\t\treturn interrupt.getMaxPacketSize();\n\t}\t\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaheller%2Fptplibrary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaheller%2Fptplibrary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaheller%2Fptplibrary/lists"}