{"id":24976133,"url":"https://github.com/dulingkang/camera","last_synced_at":"2025-07-16T20:32:17.428Z","repository":{"id":92096563,"uuid":"47802399","full_name":"dulingkang/Camera","owner":"dulingkang","description":"custom camera using swift","archived":false,"fork":false,"pushed_at":"2015-12-15T08:40:56.000Z","size":52,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T09:21:24.678Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dulingkang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-11T03:19:47.000Z","updated_at":"2020-04-15T14:09:04.000Z","dependencies_parsed_at":"2023-03-15T07:45:38.337Z","dependency_job_id":null,"html_url":"https://github.com/dulingkang/Camera","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dulingkang%2FCamera","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dulingkang%2FCamera/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dulingkang%2FCamera/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dulingkang%2FCamera/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dulingkang","download_url":"https://codeload.github.com/dulingkang/Camera/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248404649,"owners_count":21097789,"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":"2025-02-03T21:59:30.948Z","updated_at":"2025-04-11T13:08:17.711Z","avatar_url":"https://github.com/dulingkang.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Camera\nCustom camera using swift, can take photo, front camera flip\n\n## 最基本的捕获图像\n\n- 需要一个session：\n\n```\nlet session = AVCaptureSession()\n```\n\n- 需要相机设备输入，前置或者后置：\n\n```\nlet devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)\n        for device in devices {\n            if device.position == position {\n                captureDevice = device as! AVCaptureDevice\n            }\n        }\n```\n\nposition为init时指定的前置或者后置相机位置\n- 现在有了相机设备，就可以获得AVCaptureDeviceInput，将它设为session的输入：\n\n```\ndo {\n            input = try AVCaptureDeviceInput.init(device: captureDevice)\n            if session.canAddInput(input) {\n                session.addInput(input)\n            }\n        } catch {\n            print(\"add video input error\")\n        }\n}\n```\n\n- 如果获得了相机使用权限，显示相机图像流简单的方法是用AVCaptureVideoPreviewLayer：\n\n```\npreviewLayer = AVCaptureVideoPreviewLayer.init(session: session)\npreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill\n previewLayer.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kCameraBottomHeight - kNavigationHeight)\n```\n\n## 使用相机\n\n- 初始化preset:\n\n```\ncamera = SSCamera.init(sessionPreset: AVCaptureSessionPresetPhoto, position: .Front)\n```\n\n- 添加上previewLayer:\n\n```\ncameraView.preview.layer.addSublayer(camera.previewLayer)\n```\n\n## 拍完照图像翻转\n\n- 相机在不同的方向拍照时，拍出的照片带有方向信息，以前置拍照为例，如果想让前置所见即所得，应该这样：\n\n```\noutputImage = UIImage(CGImage: inputImage.CGImage!, scale: inputImage.scale, orientation: UIImageOrientation.LeftMirrored)\n```\n\n- 然后再来一次绘制，下面的方法会把图片按照当前的方向绘制出来：\n\n```\nUIGraphicsBeginImageContext(size)\ninputImage.drawInRect(CGRectMake(0,0, size.width, size.height))\nlet scaledImage = UIGraphicsGetImageFromCurrentImageContext()\nUIGraphicsEndImageContext()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdulingkang%2Fcamera","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdulingkang%2Fcamera","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdulingkang%2Fcamera/lists"}