mirror of
https://github.com/gen2brain/cam2ip.git
synced 2025-12-16 04:18:39 +00:00
Compare commits
65 Commits
1.3
...
b302c77f20
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b302c77f20 | ||
|
|
a1b32804da | ||
|
|
880b41dea2 | ||
|
|
244b4f51fc | ||
|
|
c177a0bb77 | ||
|
|
071a6c4f3c | ||
|
|
30e30117a1 | ||
|
|
fd8152f7a4 | ||
|
|
7dc02de8f4 | ||
|
|
6e046d47d0 | ||
|
|
df2f672da6 | ||
|
|
1ea7956db5 | ||
|
|
fa99c12ec1 | ||
|
|
f58b475549 | ||
|
|
d4ea63f95d | ||
|
|
7b60039e66 | ||
|
|
89298de3e9 | ||
|
|
fc22ce5871 | ||
|
|
19ea541157 | ||
|
|
be5863de1d | ||
|
|
7c3fe4be3c | ||
|
|
cc5d3ad202 | ||
|
|
cb2f566154 | ||
|
|
3b86bc0d69 | ||
|
|
8bfe9c787e | ||
|
|
12b5f29452 | ||
|
|
adf2a742e6 | ||
|
|
1c342a67df | ||
|
|
9e6d20863a | ||
|
|
3844a46486 | ||
|
|
2628bbefbf | ||
|
|
3b0b066cc1 | ||
|
|
277d6c5b48 | ||
|
|
1baf0deb39 | ||
|
|
50f28f57db | ||
|
|
b4e1af7729 | ||
|
|
1c8347fee8 | ||
|
|
63dd32115c | ||
|
|
773b8920b0 | ||
|
|
01b9c564ac | ||
|
|
f1df08acc7 | ||
|
|
5b7d51a9ac | ||
|
|
6b28dfec25 | ||
|
|
ff09bbeff4 | ||
|
|
4d73010019 | ||
|
|
13385ba650 | ||
|
|
09932a978c | ||
|
|
fb1388a10a | ||
|
|
8ebd729dcd | ||
|
|
309f6d339f | ||
|
|
3d617687fa | ||
|
|
d5870110d9 | ||
|
|
72f12931da | ||
|
|
bb676e4e44 | ||
|
|
ec60387edf | ||
|
|
0503355005 | ||
|
|
c9d77f03e7 | ||
|
|
37b19fcfe8 | ||
|
|
5006c48690 | ||
|
|
d340fa2dc3 | ||
|
|
fa5233255e | ||
|
|
ac555cbf7b | ||
|
|
e1f03b55a1 | ||
|
|
3c0c949f31 | ||
|
|
8a740337ab |
22
Dockerfile
Normal file
22
Dockerfile
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
FROM golang:alpine as build
|
||||||
|
|
||||||
|
WORKDIR /build
|
||||||
|
|
||||||
|
RUN apk add --no-cache git
|
||||||
|
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN CGO_ENABLED=0 go build -tags jpeg -o cam2ip -ldflags "-s -w" github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|
||||||
|
|
||||||
|
FROM scratch
|
||||||
|
|
||||||
|
COPY --from=build /build/cam2ip /cam2ip
|
||||||
|
|
||||||
|
EXPOSE 56000
|
||||||
|
|
||||||
|
ENTRYPOINT ["/cam2ip"]
|
||||||
65
README.md
65
README.md
@@ -4,52 +4,77 @@ Turn any webcam into an IP camera.
|
|||||||
|
|
||||||
Example (in web browser):
|
Example (in web browser):
|
||||||
|
|
||||||
http://localhost:56000/mjpeg
|
http://localhost:56000/html
|
||||||
|
|
||||||
or
|
or
|
||||||
|
|
||||||
http://localhost:56000/html
|
http://localhost:56000/mjpeg
|
||||||
|
|
||||||
|
You can also use apps like `ffplay` or `vlc`:
|
||||||
|
|
||||||
|
ffplay -i http://localhost:56000/mjpeg
|
||||||
|
|
||||||
### Requirements
|
### Requirements
|
||||||
|
|
||||||
* [OpenCV 2.x](http://opencv.org/)
|
* On Linux/RPi native Go [V4L](https://github.com/korandiz/v4l) implementation is used to capture images.
|
||||||
|
* On Windows [Video for Windows (VfW)](https://en.wikipedia.org/wiki/Video_for_Windows) framework is used over win32 API.
|
||||||
|
|
||||||
|
### Build tags
|
||||||
|
|
||||||
|
* `cv2` - build with `OpenCV` 2.x ([go-opencv](https://github.com/lazywei/go-opencv))
|
||||||
|
* `cv4` - build with `OpenCV` 4.x ([gocv](https://github.com/hybridgroup/gocv))
|
||||||
|
* `turbo` - build with `libjpeg-turbo` ([libjpeg-turbo](https://www.libjpeg-turbo.org/)) instead of native Go `image/jpeg`
|
||||||
|
|
||||||
### Download
|
### Download
|
||||||
|
|
||||||
Binaries are compiled with static OpenCV library:
|
Binaries are compiled with static OpenCV/libjpeg-turbo libraries, they should just work:
|
||||||
|
|
||||||
- [Linux 64bit](https://github.com/gen2brain/cam2ip/releases/download/1.3/cam2ip-1.3-64bit.tar.gz)
|
- [Linux 64bit](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-64bit.tar.gz)
|
||||||
- [RPi 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.3/cam2ip-1.3-RPi.tar.gz)
|
- [Linux 64bit OpenCV](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-64bit-cv2.tar.gz)
|
||||||
- [RPi3 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.3/cam2ip-1.3-RPi3.tar.gz)
|
- [macOS 64bit OpenCV](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-darwin-cv2.zip)
|
||||||
- [Windows 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.3/cam2ip-1.3.zip)
|
- [RPi 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-RPi.tar.gz)
|
||||||
|
- [RPi 32bit OpenCV](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-RPi-cv2.tar.gz)
|
||||||
|
- [RPi 32bit Static](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-RPi-nocgo.tar.gz)
|
||||||
|
- [RPi3 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-RPi3.tar.gz)
|
||||||
|
- [RPi3 32bit OpenCV](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-RPi3-cv2.tar.gz)
|
||||||
|
- [Windows 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-32bit.zip)
|
||||||
|
- [Windows 32bit OpenCV](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-32bit-cv2.zip)
|
||||||
|
- [Windows 64bit](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-64bit.zip)
|
||||||
|
- [Windows 64bit OpenCV](https://github.com/gen2brain/cam2ip/releases/download/1.6/cam2ip-1.6-64bit-cv2.zip)
|
||||||
|
|
||||||
|
|
||||||
### Installation
|
### Installation
|
||||||
|
|
||||||
go get -v github.com/gen2brain/cam2ip
|
go get -v github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|
||||||
This will install app in `$GOPATH/bin/cam2ip`.
|
This will install app in `$GOPATH/bin/cam2ip`.
|
||||||
|
|
||||||
|
### Run in Docker container
|
||||||
|
|
||||||
|
docker run --device=/dev/video0:/dev/video0 -p56000:56000 -it gen2brain/cam2ip # on RPi use gen2brain/cam2ip:arm
|
||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
Usage of ./cam2ip:
|
Usage of cam2ip:
|
||||||
-bind-addr string
|
-bind-addr string
|
||||||
Bind address (default ":56000")
|
Bind address [CAM2IP_BIND_ADDR] (default ":56000")
|
||||||
-delay int
|
-delay int
|
||||||
Delay between frames, in milliseconds (default 10)
|
Delay between frames, in milliseconds [CAM2IP_DELAY] (default 10)
|
||||||
-height float
|
-height float
|
||||||
Frame height (default 480)
|
Frame height [CAM2IP_HEIGHT] (default 480)
|
||||||
-width float
|
|
||||||
Frame width (default 640)
|
|
||||||
-htpasswd-file string
|
-htpasswd-file string
|
||||||
Path to htpasswd file, if empty auth is disabled
|
Path to htpasswd file, if empty auth is disabled [CAM2IP_HTPASSWD_FILE]
|
||||||
-index int
|
-index int
|
||||||
Camera index
|
Camera index [CAM2IP_INDEX]
|
||||||
-nowebgl
|
-nowebgl
|
||||||
Disable WebGL drawing of images (html handler)
|
Disable WebGL drawing of images (html handler) [CAM2IP_NOWEBGL]
|
||||||
-video-file string
|
-rotate int
|
||||||
Use video file instead of camera
|
Rotate image, valid values are 90, 180, 270 [CAM2IP_ROTATE]
|
||||||
|
-timestamp
|
||||||
|
Draws timestamp on images [CAM2IP_TIMESTAMP]
|
||||||
|
-width float
|
||||||
|
Frame width [CAM2IP_WIDTH] (default 640)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Handlers
|
### Handlers
|
||||||
|
|||||||
107
camera/camera.go
107
camera/camera.go
@@ -1,103 +1,10 @@
|
|||||||
// Package camera.
|
|
||||||
package camera
|
package camera
|
||||||
|
|
||||||
import (
|
// Options.
|
||||||
"fmt"
|
type Options struct {
|
||||||
"image"
|
Index int
|
||||||
|
Rotate int
|
||||||
"github.com/lazywei/go-opencv/opencv"
|
Width float64
|
||||||
)
|
Height float64
|
||||||
|
Timestamp bool
|
||||||
// Property identifiers.
|
|
||||||
const (
|
|
||||||
PropPosMsec = iota
|
|
||||||
PropPosFrames
|
|
||||||
PropPosAviRatio
|
|
||||||
PropFrameWidth
|
|
||||||
PropFrameHeight
|
|
||||||
PropFps
|
|
||||||
PropFourcc
|
|
||||||
PropFrameCount
|
|
||||||
PropFormat
|
|
||||||
PropMode
|
|
||||||
PropBrightness
|
|
||||||
PropContrast
|
|
||||||
PropSaturation
|
|
||||||
PropHue
|
|
||||||
PropGain
|
|
||||||
PropExposure
|
|
||||||
PropConvertRgb
|
|
||||||
PropWhiteBalanceU
|
|
||||||
PropRectification
|
|
||||||
PropMonocrome
|
|
||||||
PropSharpness
|
|
||||||
PropAutoExposure
|
|
||||||
PropGamma
|
|
||||||
PropTemperature
|
|
||||||
PropTrigger
|
|
||||||
PropTriggerDelay
|
|
||||||
PropWhiteBalanceV
|
|
||||||
PropZoom
|
|
||||||
PropFocus
|
|
||||||
PropGuid
|
|
||||||
PropIsoSpeed
|
|
||||||
PropMaxDc1394
|
|
||||||
PropBacklight
|
|
||||||
PropPan
|
|
||||||
PropTilt
|
|
||||||
PropRoll
|
|
||||||
PropIris
|
|
||||||
PropSettings
|
|
||||||
PropBuffersize
|
|
||||||
)
|
|
||||||
|
|
||||||
// Camera represents camera.
|
|
||||||
type Camera struct {
|
|
||||||
camera *opencv.Capture
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns new Camera for given camera index.
|
|
||||||
func New(index int) (camera *Camera, err error) {
|
|
||||||
camera = &Camera{}
|
|
||||||
|
|
||||||
camera.camera = opencv.NewCameraCapture(index)
|
|
||||||
if camera.camera == nil {
|
|
||||||
err = fmt.Errorf("camera: can not open camera %d", index)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read reads next frame from camera and returns image.
|
|
||||||
func (c *Camera) Read() (img image.Image, err error) {
|
|
||||||
if c.camera.GrabFrame() {
|
|
||||||
frame := c.camera.RetrieveFrame(1)
|
|
||||||
img = frame.ToImage()
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("camera: can not grab frame")
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetProperty returns the specified camera property.
|
|
||||||
func (c *Camera) GetProperty(id int) float64 {
|
|
||||||
return c.camera.GetProperty(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetProperty sets a camera property.
|
|
||||||
func (c *Camera) SetProperty(id int, value float64) int {
|
|
||||||
return c.camera.SetProperty(id, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close closes camera.
|
|
||||||
func (c *Camera) Close() (err error) {
|
|
||||||
if c.camera == nil {
|
|
||||||
err = fmt.Errorf("camera: camera is not opened")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.camera.Release()
|
|
||||||
c.camera = nil
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|||||||
304
camera/camera_android.go
Normal file
304
camera/camera_android.go
Normal file
@@ -0,0 +1,304 @@
|
|||||||
|
//go:build android
|
||||||
|
// +build android
|
||||||
|
|
||||||
|
// Package camera.
|
||||||
|
package camera
|
||||||
|
|
||||||
|
/*
|
||||||
|
#include <android/log.h>
|
||||||
|
|
||||||
|
#include <media/NdkImageReader.h>
|
||||||
|
|
||||||
|
#include <camera/NdkCameraDevice.h>
|
||||||
|
#include <camera/NdkCameraManager.h>
|
||||||
|
|
||||||
|
#define TAG "camera"
|
||||||
|
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
|
||||||
|
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
|
||||||
|
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
|
||||||
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
|
||||||
|
|
||||||
|
AImage *image;
|
||||||
|
AImageReader *imageReader;
|
||||||
|
|
||||||
|
ANativeWindow *nativeWindow;
|
||||||
|
|
||||||
|
ACameraDevice *cameraDevice;
|
||||||
|
ACameraManager *cameraManager;
|
||||||
|
ACameraOutputTarget *cameraOutputTarget;
|
||||||
|
ACameraCaptureSession *cameraCaptureSession;
|
||||||
|
|
||||||
|
ACaptureRequest *captureRequest;
|
||||||
|
ACaptureSessionOutput *captureSessionOutput;
|
||||||
|
ACaptureSessionOutputContainer *captureSessionOutputContainer;
|
||||||
|
|
||||||
|
void device_on_disconnected(void *context, ACameraDevice *device) {
|
||||||
|
LOGI("camera %s is diconnected.\n", ACameraDevice_getId(device));
|
||||||
|
}
|
||||||
|
|
||||||
|
void device_on_error(void *context, ACameraDevice *device, int error) {
|
||||||
|
LOGE("error %d on camera %s.\n", error, ACameraDevice_getId(device));
|
||||||
|
}
|
||||||
|
|
||||||
|
ACameraDevice_stateCallbacks deviceStateCallbacks = {
|
||||||
|
.context = NULL,
|
||||||
|
.onDisconnected = device_on_disconnected,
|
||||||
|
.onError = device_on_error,
|
||||||
|
};
|
||||||
|
|
||||||
|
void session_on_ready(void *context, ACameraCaptureSession *session) {
|
||||||
|
LOGI("session is ready. %p\n", session);
|
||||||
|
}
|
||||||
|
|
||||||
|
void session_on_active(void *context, ACameraCaptureSession *session) {
|
||||||
|
LOGI("session is activated. %p\n", session);
|
||||||
|
}
|
||||||
|
|
||||||
|
void session_on_closed(void *context, ACameraCaptureSession *session) {
|
||||||
|
LOGI("session is closed. %p\n", session);
|
||||||
|
}
|
||||||
|
|
||||||
|
ACameraCaptureSession_stateCallbacks captureSessionStateCallbacks = {
|
||||||
|
.context = NULL,
|
||||||
|
.onActive = session_on_active,
|
||||||
|
.onReady = session_on_ready,
|
||||||
|
.onClosed = session_on_closed,
|
||||||
|
};
|
||||||
|
|
||||||
|
void image_callback(void *context, AImageReader *reader) {
|
||||||
|
LOGD("image_callback");
|
||||||
|
|
||||||
|
media_status_t status = AImageReader_acquireLatestImage(reader, &image);
|
||||||
|
if(status != AMEDIA_OK) {
|
||||||
|
LOGE("failed to acquire next image (reason: %d).\n", status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AImageReader_ImageListener imageListener = {
|
||||||
|
.context = NULL,
|
||||||
|
.onImageAvailable = image_callback,
|
||||||
|
};
|
||||||
|
|
||||||
|
int openCamera(int index, int width, int height) {
|
||||||
|
ACameraIdList *cameraIdList;
|
||||||
|
const char *selectedCameraId;
|
||||||
|
|
||||||
|
camera_status_t status = ACAMERA_OK;
|
||||||
|
|
||||||
|
cameraManager = ACameraManager_create();
|
||||||
|
|
||||||
|
status = ACameraManager_getCameraIdList(cameraManager, &cameraIdList);
|
||||||
|
if(status != ACAMERA_OK) {
|
||||||
|
LOGE("failed to get camera id list (reason: %d).\n", status);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cameraIdList->numCameras < 1) {
|
||||||
|
LOGE("no camera device detected.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cameraIdList->numCameras < index+1) {
|
||||||
|
LOGE("no camera at index %d.\n", index);
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedCameraId = cameraIdList->cameraIds[index];
|
||||||
|
LOGI("open camera (id: %s, num of cameras: %d).\n", selectedCameraId, cameraIdList->numCameras);
|
||||||
|
|
||||||
|
status = ACameraManager_openCamera(cameraManager, selectedCameraId, &deviceStateCallbacks, &cameraDevice);
|
||||||
|
if(status != ACAMERA_OK) {
|
||||||
|
LOGE("failed to open camera device (id: %s)\n", selectedCameraId);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = ACameraDevice_createCaptureRequest(cameraDevice, TEMPLATE_STILL_CAPTURE, &captureRequest);
|
||||||
|
if(status != ACAMERA_OK) {
|
||||||
|
LOGE("failed to create snapshot capture request (id: %s)\n", selectedCameraId);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = ACaptureSessionOutputContainer_create(&captureSessionOutputContainer);
|
||||||
|
if(status != ACAMERA_OK) {
|
||||||
|
LOGE("failed to create session output container (id: %s)\n", selectedCameraId);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
media_status_t mstatus = AImageReader_new(width, height, AIMAGE_FORMAT_YUV_420_888, 2, &imageReader);
|
||||||
|
if(mstatus != AMEDIA_OK) {
|
||||||
|
LOGE("failed to create image reader (reason: %d).\n", mstatus);
|
||||||
|
return mstatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
mstatus = AImageReader_setImageListener(imageReader, &imageListener);
|
||||||
|
if(mstatus != AMEDIA_OK) {
|
||||||
|
LOGE("failed to set image listener (reason: %d).\n", mstatus);
|
||||||
|
return mstatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
AImageReader_getWindow(imageReader, &nativeWindow);
|
||||||
|
ANativeWindow_acquire(nativeWindow);
|
||||||
|
|
||||||
|
ACameraOutputTarget_create(nativeWindow, &cameraOutputTarget);
|
||||||
|
ACaptureRequest_addTarget(captureRequest, cameraOutputTarget);
|
||||||
|
|
||||||
|
ACaptureSessionOutput_create(nativeWindow, &captureSessionOutput);
|
||||||
|
ACaptureSessionOutputContainer_add(captureSessionOutputContainer, captureSessionOutput);
|
||||||
|
|
||||||
|
status = ACameraDevice_createCaptureSession(cameraDevice, captureSessionOutputContainer, &captureSessionStateCallbacks, &cameraCaptureSession);
|
||||||
|
if(status != ACAMERA_OK) {
|
||||||
|
LOGE("failed to create capture session (reason: %d).\n", status);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
ACameraManager_deleteCameraIdList(cameraIdList);
|
||||||
|
ACameraManager_delete(cameraManager);
|
||||||
|
|
||||||
|
return ACAMERA_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int captureCamera() {
|
||||||
|
camera_status_t status = ACameraCaptureSession_capture(cameraCaptureSession, NULL, 1, &captureRequest, NULL);
|
||||||
|
if(status != ACAMERA_OK) {
|
||||||
|
LOGE("failed to capture image (reason: %d).\n", status);
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
int closeCamera() {
|
||||||
|
camera_status_t status = ACAMERA_OK;
|
||||||
|
|
||||||
|
if(captureRequest != NULL) {
|
||||||
|
ACaptureRequest_free(captureRequest);
|
||||||
|
captureRequest = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cameraOutputTarget != NULL) {
|
||||||
|
ACameraOutputTarget_free(cameraOutputTarget);
|
||||||
|
cameraOutputTarget = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cameraDevice != NULL) {
|
||||||
|
status = ACameraDevice_close(cameraDevice);
|
||||||
|
|
||||||
|
if(status != ACAMERA_OK) {
|
||||||
|
LOGE("failed to close camera device.\n");
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
cameraDevice = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(captureSessionOutput != NULL) {
|
||||||
|
ACaptureSessionOutput_free(captureSessionOutput);
|
||||||
|
captureSessionOutput = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(captureSessionOutputContainer != NULL) {
|
||||||
|
ACaptureSessionOutputContainer_free(captureSessionOutputContainer);
|
||||||
|
captureSessionOutputContainer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(imageReader != NULL) {
|
||||||
|
AImageReader_delete(imageReader);
|
||||||
|
imageReader = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(image != NULL) {
|
||||||
|
AImage_delete(image);
|
||||||
|
image = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGI("camera closed.\n");
|
||||||
|
return ACAMERA_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int openCamera(int index, int width, int height);
|
||||||
|
int captureCamera();
|
||||||
|
int closeCamera();
|
||||||
|
|
||||||
|
#cgo android CFLAGS: -D__ANDROID_API__=24
|
||||||
|
#cgo android LDFLAGS: -lcamera2ndk -lmediandk -llog -landroid
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Camera represents camera.
|
||||||
|
type Camera struct {
|
||||||
|
opts Options
|
||||||
|
img *image.YCbCr
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns new Camera for given camera index.
|
||||||
|
func New(opts Options) (camera *Camera, err error) {
|
||||||
|
camera = &Camera{}
|
||||||
|
camera.opts = opts
|
||||||
|
|
||||||
|
camera.img = image.NewYCbCr(image.Rect(0, 0, int(opts.Width), int(opts.Height)), image.YCbCrSubsampleRatio420)
|
||||||
|
|
||||||
|
ret := C.openCamera(C.int(opts.Index), C.int(opts.Width), C.int(opts.Height))
|
||||||
|
if bool(int(ret) != 0) {
|
||||||
|
err = fmt.Errorf("camera: can not open camera %d: error %d", opts.Index, int(ret))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads next frame from camera and returns image.
|
||||||
|
func (c *Camera) Read() (img image.Image, err error) {
|
||||||
|
ret := C.captureCamera()
|
||||||
|
if bool(int(ret) != 0) {
|
||||||
|
err = fmt.Errorf("camera: can not grab frame: error %d", int(ret))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if C.image == nil {
|
||||||
|
err = fmt.Errorf("camera: can not retrieve frame")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var yStride C.int
|
||||||
|
var yLen, cbLen, crLen C.int
|
||||||
|
var yPtr, cbPtr, crPtr *C.uint8_t
|
||||||
|
|
||||||
|
C.AImage_getPlaneRowStride(C.image, 0, &yStride)
|
||||||
|
C.AImage_getPlaneData(C.image, 0, &yPtr, &yLen)
|
||||||
|
C.AImage_getPlaneData(C.image, 1, &cbPtr, &cbLen)
|
||||||
|
C.AImage_getPlaneData(C.image, 2, &crPtr, &crLen)
|
||||||
|
|
||||||
|
c.img.YStride = int(yStride)
|
||||||
|
c.img.CStride = int(yStride) / 2
|
||||||
|
|
||||||
|
c.img.Y = C.GoBytes(unsafe.Pointer(yPtr), yLen)
|
||||||
|
c.img.Cb = C.GoBytes(unsafe.Pointer(cbPtr), cbLen)
|
||||||
|
c.img.Cr = C.GoBytes(unsafe.Pointer(crPtr), crLen)
|
||||||
|
|
||||||
|
img = c.img
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProperty returns the specified camera property.
|
||||||
|
func (c *Camera) GetProperty(id int) float64 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetProperty sets a camera property.
|
||||||
|
func (c *Camera) SetProperty(id int, value float64) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes camera.
|
||||||
|
func (c *Camera) Close() (err error) {
|
||||||
|
ret := C.closeCamera()
|
||||||
|
if bool(int(ret) != 0) {
|
||||||
|
err = fmt.Errorf("camera: can not close camera %d: error %d", c.opts.Index, int(ret))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
47
camera/camera_const_cv.go
Normal file
47
camera/camera_const_cv.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
//go:build cv2 || cv4
|
||||||
|
// +build cv2 cv4
|
||||||
|
|
||||||
|
package camera
|
||||||
|
|
||||||
|
// Property identifiers.
|
||||||
|
const (
|
||||||
|
PropPosMsec = iota
|
||||||
|
PropPosFrames
|
||||||
|
PropPosAviRatio
|
||||||
|
PropFrameWidth
|
||||||
|
PropFrameHeight
|
||||||
|
PropFps
|
||||||
|
PropFourcc
|
||||||
|
PropFrameCount
|
||||||
|
PropFormat
|
||||||
|
PropMode
|
||||||
|
PropBrightness
|
||||||
|
PropContrast
|
||||||
|
PropSaturation
|
||||||
|
PropHue
|
||||||
|
PropGain
|
||||||
|
PropExposure
|
||||||
|
PropConvertRgb
|
||||||
|
PropWhiteBalanceU
|
||||||
|
PropRectification
|
||||||
|
PropMonocrome
|
||||||
|
PropSharpness
|
||||||
|
PropAutoExposure
|
||||||
|
PropGamma
|
||||||
|
PropTemperature
|
||||||
|
PropTrigger
|
||||||
|
PropTriggerDelay
|
||||||
|
PropWhiteBalanceV
|
||||||
|
PropZoom
|
||||||
|
PropFocus
|
||||||
|
PropGuid
|
||||||
|
PropIsoSpeed
|
||||||
|
PropMaxDc1394
|
||||||
|
PropBacklight
|
||||||
|
PropPan
|
||||||
|
PropTilt
|
||||||
|
PropRoll
|
||||||
|
PropIris
|
||||||
|
PropSettings
|
||||||
|
PropBuffersize
|
||||||
|
)
|
||||||
22
camera/camera_const_linux.go
Normal file
22
camera/camera_const_linux.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
//go:build !cv2 && !cv4 && !android
|
||||||
|
// +build !cv2,!cv4,!android
|
||||||
|
|
||||||
|
package camera
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/korandiz/v4l"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Property identifiers.
|
||||||
|
const (
|
||||||
|
PropBrightness = v4l.CtrlBrightness
|
||||||
|
PropContrast = v4l.CtrlContrast
|
||||||
|
PropSaturation = v4l.CtrlSaturation
|
||||||
|
PropHue = v4l.CtrlHue
|
||||||
|
PropGain = v4l.CtrlGain
|
||||||
|
PropExposure = v4l.CtrlExposure
|
||||||
|
PropWhiteBalanceU = v4l.CtrlWhiteBalance
|
||||||
|
PropSharpness = v4l.CtrlSharpness
|
||||||
|
PropWhiteBalanceV = v4l.CtrlDoWhiteBalance
|
||||||
|
PropBacklight = v4l.CtrlBacklightCompensation
|
||||||
|
)
|
||||||
101
camera/camera_cv2.go
Normal file
101
camera/camera_cv2.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
//go:build cv2 && !cv4
|
||||||
|
// +build cv2,!cv4
|
||||||
|
|
||||||
|
// Package camera.
|
||||||
|
package camera
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"image/draw"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
|
"github.com/gen2brain/go-opencv/opencv"
|
||||||
|
"github.com/pbnjay/pixfont"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Camera represents camera.
|
||||||
|
type Camera struct {
|
||||||
|
opts Options
|
||||||
|
camera *opencv.Capture
|
||||||
|
frame *opencv.IplImage
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns new Camera for given camera index.
|
||||||
|
func New(opts Options) (camera *Camera, err error) {
|
||||||
|
camera = &Camera{}
|
||||||
|
camera.opts = opts
|
||||||
|
|
||||||
|
camera.camera = opencv.NewCameraCapture(opts.Index)
|
||||||
|
if camera.camera == nil {
|
||||||
|
err = fmt.Errorf("camera: can not open camera %d", opts.Index)
|
||||||
|
}
|
||||||
|
|
||||||
|
camera.SetProperty(PropFrameWidth, opts.Width)
|
||||||
|
camera.SetProperty(PropFrameHeight, opts.Height)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads next frame from camera and returns image.
|
||||||
|
func (c *Camera) Read() (img image.Image, err error) {
|
||||||
|
if !c.camera.GrabFrame() {
|
||||||
|
err = fmt.Errorf("camera: can not grab frame")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.frame = c.camera.RetrieveFrame(1)
|
||||||
|
if c.frame == nil {
|
||||||
|
err = fmt.Errorf("camera: can not retrieve frame")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
img = c.frame.ToImage()
|
||||||
|
|
||||||
|
switch c.opts.Rotate {
|
||||||
|
case 90:
|
||||||
|
img = imaging.Rotate90(img)
|
||||||
|
case 180:
|
||||||
|
img = imaging.Rotate180(img)
|
||||||
|
case 270:
|
||||||
|
img = imaging.Rotate270(img)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.opts.Timestamp {
|
||||||
|
dimg, ok := img.(draw.Image)
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("camera: %T is not a drawable image type", img)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pixfont.DrawString(dimg, 10, 10, time.Now().Format("2006-01-02 15:04:05"), color.White)
|
||||||
|
img = dimg
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProperty returns the specified camera property.
|
||||||
|
func (c *Camera) GetProperty(id int) float64 {
|
||||||
|
return c.camera.GetProperty(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetProperty sets a camera property.
|
||||||
|
func (c *Camera) SetProperty(id int, value float64) {
|
||||||
|
c.camera.SetProperty(id, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes camera.
|
||||||
|
func (c *Camera) Close() (err error) {
|
||||||
|
if c.camera == nil {
|
||||||
|
err = fmt.Errorf("camera: camera is not opened")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.frame.Release()
|
||||||
|
c.camera.Release()
|
||||||
|
c.camera = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
108
camera/camera_cv4.go
Normal file
108
camera/camera_cv4.go
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
//go:build cv4 && !cv2
|
||||||
|
// +build cv4,!cv2
|
||||||
|
|
||||||
|
// Package camera.
|
||||||
|
package camera
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"image/draw"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
|
"github.com/pbnjay/pixfont"
|
||||||
|
"gocv.io/x/gocv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Camera represents camera.
|
||||||
|
type Camera struct {
|
||||||
|
opts Options
|
||||||
|
camera *gocv.VideoCapture
|
||||||
|
frame *gocv.Mat
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns new Camera for given camera index.
|
||||||
|
func New(opts Options) (camera *Camera, err error) {
|
||||||
|
camera = &Camera{}
|
||||||
|
camera.opts = opts
|
||||||
|
|
||||||
|
mat := gocv.NewMat()
|
||||||
|
camera.frame = &mat
|
||||||
|
|
||||||
|
camera.camera, err = gocv.VideoCaptureDevice(opts.Index)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("camera: can not open camera %d: %s", opts.Index, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
camera.SetProperty(PropFrameWidth, opts.Width)
|
||||||
|
camera.SetProperty(PropFrameHeight, opts.Height)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads next frame from camera and returns image.
|
||||||
|
func (c *Camera) Read() (img image.Image, err error) {
|
||||||
|
ok := c.camera.Read(c.frame)
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("camera: can not grab frame")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
img, e := c.frame.ToImage()
|
||||||
|
if e != nil {
|
||||||
|
err = fmt.Errorf("camera: %v", e)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.frame == nil {
|
||||||
|
err = fmt.Errorf("camera: can not retrieve frame")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch c.opts.Rotate {
|
||||||
|
case 90:
|
||||||
|
img = imaging.Rotate90(img)
|
||||||
|
case 180:
|
||||||
|
img = imaging.Rotate180(img)
|
||||||
|
case 270:
|
||||||
|
img = imaging.Rotate270(img)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.opts.Timestamp {
|
||||||
|
dimg, ok := img.(draw.Image)
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("camera: %T is not a drawable image type", img)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pixfont.DrawString(dimg, 10, 10, time.Now().Format("2006-01-02 15:04:05"), color.White)
|
||||||
|
img = dimg
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProperty returns the specified camera property.
|
||||||
|
func (c *Camera) GetProperty(id int) float64 {
|
||||||
|
return c.camera.Get(gocv.VideoCaptureProperties(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetProperty sets a camera property.
|
||||||
|
func (c *Camera) SetProperty(id int, value float64) {
|
||||||
|
c.camera.Set(gocv.VideoCaptureProperties(id), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes camera.
|
||||||
|
func (c *Camera) Close() (err error) {
|
||||||
|
if c.camera == nil {
|
||||||
|
err = fmt.Errorf("camera: camera is not opened")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.frame.Close()
|
||||||
|
err = c.camera.Close()
|
||||||
|
c.camera = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
136
camera/camera_linux.go
Normal file
136
camera/camera_linux.go
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
//go:build !cv2 && !cv4 && !android
|
||||||
|
// +build !cv2,!cv4,!android
|
||||||
|
|
||||||
|
// Package camera.
|
||||||
|
package camera
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"image/draw"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
|
"github.com/korandiz/v4l"
|
||||||
|
"github.com/korandiz/v4l/fmt/mjpeg"
|
||||||
|
"github.com/pbnjay/pixfont"
|
||||||
|
|
||||||
|
im "github.com/gen2brain/cam2ip/image"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Camera represents camera.
|
||||||
|
type Camera struct {
|
||||||
|
opts Options
|
||||||
|
camera *v4l.Device
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns new Camera for given camera index.
|
||||||
|
func New(opts Options) (camera *Camera, err error) {
|
||||||
|
camera = &Camera{}
|
||||||
|
camera.opts = opts
|
||||||
|
|
||||||
|
devices := v4l.FindDevices()
|
||||||
|
if len(devices) < opts.Index+1 {
|
||||||
|
err = fmt.Errorf("camera: no camera at index %d", opts.Index)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
camera.camera, err = v4l.Open(devices[opts.Index].Path)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("camera: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if camera.camera == nil {
|
||||||
|
err = fmt.Errorf("camera: can not open camera %d", opts.Index)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
config, err := camera.camera.GetConfig()
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("camera: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
config.Format = mjpeg.FourCC
|
||||||
|
config.Width = int(opts.Width)
|
||||||
|
config.Height = int(opts.Height)
|
||||||
|
|
||||||
|
err = camera.camera.SetConfig(config)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("camera: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = camera.camera.TurnOn()
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("camera: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads next frame from camera and returns image.
|
||||||
|
func (c *Camera) Read() (img image.Image, err error) {
|
||||||
|
|
||||||
|
buffer, err := c.camera.Capture()
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("camera: can not grab frame: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
img, err = im.NewDecoder(buffer).Decode()
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("camera: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch c.opts.Rotate {
|
||||||
|
case 90:
|
||||||
|
img = imaging.Rotate90(img)
|
||||||
|
case 180:
|
||||||
|
img = imaging.Rotate180(img)
|
||||||
|
case 270:
|
||||||
|
img = imaging.Rotate270(img)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.opts.Timestamp {
|
||||||
|
dimg, ok := img.(draw.Image)
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("camera: %T is not a drawable image type", img)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pixfont.DrawString(dimg, 10, 10, time.Now().Format("2006-01-02 15:04:05"), color.White)
|
||||||
|
img = dimg
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProperty returns the specified camera property.
|
||||||
|
func (c *Camera) GetProperty(id int) float64 {
|
||||||
|
ret, _ := c.camera.GetControl(uint32(id))
|
||||||
|
return float64(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetProperty sets a camera property.
|
||||||
|
func (c *Camera) SetProperty(id int, value float64) {
|
||||||
|
c.camera.SetControl(uint32(id), int32(value))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes camera.
|
||||||
|
func (c *Camera) Close() (err error) {
|
||||||
|
if c.camera == nil {
|
||||||
|
err = fmt.Errorf("camera: camera is not opened")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.camera.TurnOff()
|
||||||
|
|
||||||
|
c.camera.Close()
|
||||||
|
c.camera = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestCamera(t *testing.T) {
|
func TestCamera(t *testing.T) {
|
||||||
camera, err := New(1)
|
camera, err := New(Options{0, 0, 640, 480})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -25,18 +25,6 @@ func TestCamera(t *testing.T) {
|
|||||||
|
|
||||||
defer os.RemoveAll(tmpdir)
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
var width, height float64 = 640, 480
|
|
||||||
camera.SetProperty(PropFrameWidth, width)
|
|
||||||
camera.SetProperty(PropFrameHeight, height)
|
|
||||||
|
|
||||||
if camera.GetProperty(PropFrameWidth) != width {
|
|
||||||
t.Error("FrameWidth not correct")
|
|
||||||
}
|
|
||||||
|
|
||||||
if camera.GetProperty(PropFrameHeight) != height {
|
|
||||||
t.Error("FrameHeight not correct")
|
|
||||||
}
|
|
||||||
|
|
||||||
var i int
|
var i int
|
||||||
var n int = 10
|
var n int = 10
|
||||||
|
|
||||||
|
|||||||
417
camera/camera_windows.go
Normal file
417
camera/camera_windows.go
Normal file
@@ -0,0 +1,417 @@
|
|||||||
|
//go:build !cv2 && !cv4
|
||||||
|
// +build !cv2,!cv4
|
||||||
|
|
||||||
|
// Package camera.
|
||||||
|
package camera
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"image/draw"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
|
"github.com/pbnjay/pixfont"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Camera represents camera.
|
||||||
|
type Camera struct {
|
||||||
|
opts Options
|
||||||
|
camera syscall.Handle
|
||||||
|
frame *image.RGBA
|
||||||
|
hdr *videoHdr
|
||||||
|
instance syscall.Handle
|
||||||
|
className string
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns new Camera for given camera index.
|
||||||
|
func New(opts Options) (camera *Camera, err error) {
|
||||||
|
camera = &Camera{}
|
||||||
|
camera.opts = opts
|
||||||
|
camera.className = "capWindowClass"
|
||||||
|
|
||||||
|
camera.instance, err = getModuleHandle()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
camera.frame = image.NewRGBA(image.Rect(0, 0, int(camera.opts.Width), int(camera.opts.Height)))
|
||||||
|
|
||||||
|
go func(c *Camera) {
|
||||||
|
fn := func(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
|
||||||
|
switch msg {
|
||||||
|
case wmClose:
|
||||||
|
destroyWindow(hwnd)
|
||||||
|
case wmDestroy:
|
||||||
|
postQuitMessage(0)
|
||||||
|
default:
|
||||||
|
ret := defWindowProc(hwnd, msg, wparam, lparam)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
err = registerClass(c.className, c.instance, fn)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hwnd, err := createWindow(0, c.className, "", wsOverlappedWindow, cwUseDefault, cwUseDefault, int64(c.opts.Width)+100, int64(c.opts.Height)+100, 0, 0, c.instance)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.camera, err = capCreateCaptureWindow("", wsChild, 0, 0, int64(c.opts.Width), int64(c.opts.Height), hwnd, 0)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := sendMessage(c.camera, wmCapDriverConnect, uintptr(c.opts.Index), 0)
|
||||||
|
if bool(int(ret) == 0) {
|
||||||
|
err = fmt.Errorf("camera: can not open camera %d", c.opts.Index)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var bi bitmapInfo
|
||||||
|
size := sendMessage(c.camera, wmCapGetVideoformat, 0, 0)
|
||||||
|
sendMessage(c.camera, wmCapGetVideoformat, size, uintptr(unsafe.Pointer(&bi)))
|
||||||
|
|
||||||
|
bi.BmiHeader.BiWidth = int32(c.opts.Width)
|
||||||
|
bi.BmiHeader.BiHeight = int32(c.opts.Height)
|
||||||
|
|
||||||
|
ret = sendMessage(c.camera, wmCapSetVideoformat, size, uintptr(unsafe.Pointer(&bi)))
|
||||||
|
if bool(int(ret) == 0) {
|
||||||
|
err = fmt.Errorf("camera: can not set video format")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessage(c.camera, wmCapSetCallbackFrame, 0, syscall.NewCallback(c.callback))
|
||||||
|
|
||||||
|
messageLoop(c.camera)
|
||||||
|
}(camera)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads next frame from camera and returns image.
|
||||||
|
func (c *Camera) Read() (img image.Image, err error) {
|
||||||
|
ret := sendMessage(c.camera, wmCapGrabFrameNoStop, 0, 0)
|
||||||
|
if bool(int(ret) == 0) {
|
||||||
|
err = fmt.Errorf("camera: can not grab frame")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := (*[1 << 24]uint8)(unsafe.Pointer(c.hdr.LpData))[0:c.hdr.DwBytesUsed]
|
||||||
|
r := bytes.NewReader(data)
|
||||||
|
|
||||||
|
width := int(c.opts.Width)
|
||||||
|
height := int(c.opts.Height)
|
||||||
|
|
||||||
|
// Taken from https://github.com/hotei/bmp/blob/master/bmpRGBA.go#L12
|
||||||
|
// There are 3 bytes per pixel, and each row is 4-byte aligned.
|
||||||
|
b := make([]byte, (3*width+3)&^3)
|
||||||
|
// BMP images are stored bottom-up rather than top-down.
|
||||||
|
for y := height - 1; y >= 0; y-- {
|
||||||
|
_, err = r.Read(b)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("camera: can not retrieve frame: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
p := c.frame.Pix[y*c.frame.Stride : y*c.frame.Stride+width*4]
|
||||||
|
for i, j := 0, 0; i < len(p); i, j = i+4, j+3 {
|
||||||
|
// BMP images are stored in BGR order rather than RGB order.
|
||||||
|
p[i+0] = b[j+2]
|
||||||
|
p[i+1] = b[j+1]
|
||||||
|
p[i+2] = b[j+0]
|
||||||
|
p[i+3] = 0xFF
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
img = c.frame
|
||||||
|
|
||||||
|
switch c.opts.Rotate {
|
||||||
|
case 90:
|
||||||
|
img = imaging.Rotate90(img)
|
||||||
|
case 180:
|
||||||
|
img = imaging.Rotate180(img)
|
||||||
|
case 270:
|
||||||
|
img = imaging.Rotate270(img)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.opts.Timestamp {
|
||||||
|
dimg, ok := img.(draw.Image)
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("camera: %T is not a drawable image type", img)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pixfont.DrawString(dimg, 10, 10, time.Now().Format("2006-01-02 15:04:05"), color.White)
|
||||||
|
img = dimg
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProperty returns the specified camera property.
|
||||||
|
func (c *Camera) GetProperty(id int) float64 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetProperty sets a camera property.
|
||||||
|
func (c *Camera) SetProperty(id int, value float64) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes camera.
|
||||||
|
func (c *Camera) Close() (err error) {
|
||||||
|
sendMessage(c.camera, wmCapSetCallbackFrame, 0, 0)
|
||||||
|
unregisterClass(c.className, c.instance)
|
||||||
|
sendMessage(c.camera, wmCapDriverDisconnect, 0, 0)
|
||||||
|
destroyWindow(c.camera)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// callback function.
|
||||||
|
func (c *Camera) callback(hwvd syscall.Handle, hdr *videoHdr) uintptr {
|
||||||
|
c.hdr = hdr
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
user32 = syscall.NewLazyDLL("user32.dll")
|
||||||
|
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||||
|
avicap32 = syscall.NewLazyDLL("avicap32.dll")
|
||||||
|
|
||||||
|
createWindowExW = user32.NewProc("CreateWindowExW")
|
||||||
|
destroyWindowW = user32.NewProc("DestroyWindow")
|
||||||
|
defWindowProcW = user32.NewProc("DefWindowProcW")
|
||||||
|
dispatchMessageW = user32.NewProc("DispatchMessageW")
|
||||||
|
translateMessageW = user32.NewProc("TranslateMessage")
|
||||||
|
getMessageW = user32.NewProc("GetMessageW")
|
||||||
|
sendMessageW = user32.NewProc("SendMessageW")
|
||||||
|
postQuitMessageW = user32.NewProc("PostQuitMessage")
|
||||||
|
registerClassExW = user32.NewProc("RegisterClassExW")
|
||||||
|
unregisterClassW = user32.NewProc("UnregisterClassW")
|
||||||
|
|
||||||
|
getModuleHandleW = kernel32.NewProc("GetModuleHandleW")
|
||||||
|
capCreateCaptureWindowW = avicap32.NewProc("capCreateCaptureWindowW")
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
wmDestroy = 0x0002
|
||||||
|
wmClose = 0x0010
|
||||||
|
wmUser = 0x0400
|
||||||
|
|
||||||
|
wmCapStart = wmUser
|
||||||
|
wmCapSetCallbackFrame = wmCapStart + 5
|
||||||
|
wmCapDriverConnect = wmCapStart + 10
|
||||||
|
wmCapDriverDisconnect = wmCapStart + 11
|
||||||
|
wmCapGetVideoformat = wmCapStart + 44
|
||||||
|
wmCapSetVideoformat = wmCapStart + 45
|
||||||
|
wmCapGrabFrame = wmCapStart + 60
|
||||||
|
wmCapGrabFrameNoStop = wmCapStart + 61
|
||||||
|
wmCapStop = wmCapStart + 68
|
||||||
|
wmCapAbort = wmCapStart + 69
|
||||||
|
|
||||||
|
wsChild = 0x40000000
|
||||||
|
wsOverlappedWindow = 0x00CF0000
|
||||||
|
|
||||||
|
cwUseDefault = 0x7fffffff
|
||||||
|
)
|
||||||
|
|
||||||
|
// wndClassExW https://msdn.microsoft.com/en-us/library/windows/desktop/ms633577.aspx
|
||||||
|
type wndClassExW struct {
|
||||||
|
size uint32
|
||||||
|
style uint32
|
||||||
|
wndProc uintptr
|
||||||
|
clsExtra int32
|
||||||
|
wndExtra int32
|
||||||
|
instance syscall.Handle
|
||||||
|
icon syscall.Handle
|
||||||
|
cursor syscall.Handle
|
||||||
|
background syscall.Handle
|
||||||
|
menuName *uint16
|
||||||
|
className *uint16
|
||||||
|
iconSm syscall.Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
// msgW https://msdn.microsoft.com/en-us/library/windows/desktop/ms644958.aspx
|
||||||
|
type msgW struct {
|
||||||
|
hwnd syscall.Handle
|
||||||
|
message uint32
|
||||||
|
wParam uintptr
|
||||||
|
lParam uintptr
|
||||||
|
time uint32
|
||||||
|
pt pointW
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://msdn.microsoft.com/en-us/ecb0f0e1-90c2-48ab-a069-552262b49c7c
|
||||||
|
type pointW struct {
|
||||||
|
x, y int32
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376.aspx
|
||||||
|
type bitmapInfoHeader struct {
|
||||||
|
BiSize uint32
|
||||||
|
BiWidth int32
|
||||||
|
BiHeight int32
|
||||||
|
BiPlanes uint16
|
||||||
|
BiBitCount uint16
|
||||||
|
BiCompression uint32
|
||||||
|
BiSizeImage uint32
|
||||||
|
BiXPelsPerMeter int32
|
||||||
|
BiYPelsPerMeter int32
|
||||||
|
BiClrUsed uint32
|
||||||
|
BiClrImportant uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd183375.aspx
|
||||||
|
type bitmapInfo struct {
|
||||||
|
BmiHeader bitmapInfoHeader
|
||||||
|
BmiColors *rgbQuad
|
||||||
|
}
|
||||||
|
|
||||||
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd162938.aspx
|
||||||
|
type rgbQuad struct {
|
||||||
|
RgbBlue byte
|
||||||
|
RgbGreen byte
|
||||||
|
RgbRed byte
|
||||||
|
RgbReserved byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/vfw/ns-vfw-videohdr_tag
|
||||||
|
type videoHdr struct {
|
||||||
|
LpData *uint8
|
||||||
|
DwBufferLength uint32
|
||||||
|
DwBytesUsed uint32
|
||||||
|
DwTimeCaptured uint32
|
||||||
|
DwUser uint64
|
||||||
|
DwFlags uint32
|
||||||
|
DwReserved [4]uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/libloaderapi/nf-libloaderapi-getmodulehandlea
|
||||||
|
func getModuleHandle() (syscall.Handle, error) {
|
||||||
|
ret, _, err := getModuleHandleW.Call(uintptr(0))
|
||||||
|
if ret == 0 {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return syscall.Handle(ret), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-createwindowexw
|
||||||
|
func createWindow(exStyle uint64, className, windowName string, style uint64, x, y, width, height int64,
|
||||||
|
parent, menu, instance syscall.Handle) (syscall.Handle, error) {
|
||||||
|
ret, _, err := createWindowExW.Call(uintptr(exStyle), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))),
|
||||||
|
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(windowName))), uintptr(style), uintptr(x), uintptr(y),
|
||||||
|
uintptr(width), uintptr(height), uintptr(parent), uintptr(menu), uintptr(instance), uintptr(0))
|
||||||
|
|
||||||
|
if ret == 0 {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return syscall.Handle(ret), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-destroywindow
|
||||||
|
func destroyWindow(hwnd syscall.Handle) error {
|
||||||
|
ret, _, err := destroyWindowW.Call(uintptr(hwnd))
|
||||||
|
if ret == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-defwindowprocw
|
||||||
|
func defWindowProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
|
||||||
|
ret, _, _ := defWindowProcW.Call(uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam))
|
||||||
|
return uintptr(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-dispatchmessagew
|
||||||
|
func dispatchMessage(msg *msgW) {
|
||||||
|
dispatchMessageW.Call(uintptr(unsafe.Pointer(msg)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-translatemessage
|
||||||
|
func translateMessage(msg *msgW) {
|
||||||
|
translateMessageW.Call(uintptr(unsafe.Pointer(msg)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getmessagew
|
||||||
|
func getMessage(msg *msgW, hwnd syscall.Handle, msgFilterMin, msgFilterMax uint32) (bool, error) {
|
||||||
|
ret, _, err := getMessageW.Call(uintptr(unsafe.Pointer(msg)), uintptr(hwnd), uintptr(msgFilterMin), uintptr(msgFilterMax))
|
||||||
|
if int32(ret) == -1 {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return int32(ret) != 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendmessage
|
||||||
|
func sendMessage(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
|
||||||
|
ret, _, _ := sendMessageW.Call(uintptr(hwnd), uintptr(msg), wparam, lparam, 0, 0)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-postquitmessage
|
||||||
|
func postQuitMessage(exitCode int32) {
|
||||||
|
postQuitMessageW.Call(uintptr(exitCode))
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registerclassexw
|
||||||
|
func registerClass(className string, instance syscall.Handle, fn interface{}) error {
|
||||||
|
var wcx wndClassExW
|
||||||
|
wcx.size = uint32(unsafe.Sizeof(wcx))
|
||||||
|
wcx.wndProc = syscall.NewCallback(fn)
|
||||||
|
wcx.instance = instance
|
||||||
|
wcx.className = syscall.StringToUTF16Ptr(className)
|
||||||
|
|
||||||
|
ret, _, err := registerClassExW.Call(uintptr(unsafe.Pointer(&wcx)))
|
||||||
|
if ret == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-unregisterclassw
|
||||||
|
func unregisterClass(className string, instance syscall.Handle) bool {
|
||||||
|
ret, _, _ := unregisterClassW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))), uintptr(instance))
|
||||||
|
return ret != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://docs.microsoft.com/en-us/windows/desktop/api/vfw/nf-vfw-capcreatecapturewindoww
|
||||||
|
func capCreateCaptureWindow(lpszWindowName string, dwStyle, x, y, width, height int64, parent syscall.Handle, id int64) (syscall.Handle, error) {
|
||||||
|
ret, _, err := capCreateCaptureWindowW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpszWindowName))),
|
||||||
|
uintptr(dwStyle), uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(parent), uintptr(id))
|
||||||
|
if ret == 0 {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return syscall.Handle(ret), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// messageLoop function
|
||||||
|
func messageLoop(hwnd syscall.Handle) {
|
||||||
|
for {
|
||||||
|
msg := &msgW{}
|
||||||
|
ok, _ := getMessage(msg, 0, 0, 0)
|
||||||
|
if ok {
|
||||||
|
translateMessage(msg)
|
||||||
|
dispatchMessage(msg)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -5,14 +5,15 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/jamiealquiza/envy"
|
||||||
|
|
||||||
"github.com/gen2brain/cam2ip/camera"
|
"github.com/gen2brain/cam2ip/camera"
|
||||||
"github.com/gen2brain/cam2ip/server"
|
"github.com/gen2brain/cam2ip/server"
|
||||||
"github.com/gen2brain/cam2ip/video"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
name = "cam2ip"
|
name = "cam2ip"
|
||||||
version = "1.3"
|
version = "1.6"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -22,10 +23,13 @@ func main() {
|
|||||||
flag.IntVar(&srv.Delay, "delay", 10, "Delay between frames, in milliseconds")
|
flag.IntVar(&srv.Delay, "delay", 10, "Delay between frames, in milliseconds")
|
||||||
flag.Float64Var(&srv.FrameWidth, "width", 640, "Frame width")
|
flag.Float64Var(&srv.FrameWidth, "width", 640, "Frame width")
|
||||||
flag.Float64Var(&srv.FrameHeight, "height", 480, "Frame height")
|
flag.Float64Var(&srv.FrameHeight, "height", 480, "Frame height")
|
||||||
|
flag.IntVar(&srv.Rotate, "rotate", 0, "Rotate image, valid values are 90, 180, 270")
|
||||||
flag.BoolVar(&srv.NoWebGL, "nowebgl", false, "Disable WebGL drawing of images (html handler)")
|
flag.BoolVar(&srv.NoWebGL, "nowebgl", false, "Disable WebGL drawing of images (html handler)")
|
||||||
|
flag.BoolVar(&srv.Timestamp, "timestamp", false, "Draws timestamp on images")
|
||||||
flag.StringVar(&srv.Bind, "bind-addr", ":56000", "Bind address")
|
flag.StringVar(&srv.Bind, "bind-addr", ":56000", "Bind address")
|
||||||
flag.StringVar(&srv.Htpasswd, "htpasswd-file", "", "Path to htpasswd file, if empty auth is disabled")
|
flag.StringVar(&srv.Htpasswd, "htpasswd-file", "", "Path to htpasswd file, if empty auth is disabled")
|
||||||
flag.StringVar(&srv.FileName, "video-file", "", "Use video file instead of camera")
|
|
||||||
|
envy.Parse("CAM2IP")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
srv.Name = name
|
srv.Name = name
|
||||||
@@ -47,27 +51,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if srv.FileName != "" {
|
cam, err := camera.New(camera.Options{srv.Index, srv.Rotate, srv.FrameWidth, srv.FrameHeight, srv.Timestamp})
|
||||||
vid, err := video.New(srv.FileName)
|
if err != nil {
|
||||||
if err != nil {
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
||||||
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
os.Exit(1)
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
srv.Reader = vid
|
|
||||||
} else {
|
|
||||||
cam, err := camera.New(srv.Index)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
cam.SetProperty(camera.PropFrameWidth, srv.FrameWidth)
|
|
||||||
cam.SetProperty(camera.PropFrameHeight, srv.FrameHeight)
|
|
||||||
|
|
||||||
srv.Reader = cam
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
srv.Reader = cam
|
||||||
|
|
||||||
defer srv.Reader.Close()
|
defer srv.Reader.Close()
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "Listening on %s\n", srv.Bind)
|
fmt.Fprintf(os.Stderr, "Listening on %s\n", srv.Bind)
|
||||||
26
go.mod
Normal file
26
go.mod
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
module github.com/gen2brain/cam2ip
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/abbot/go-http-auth v0.4.0
|
||||||
|
github.com/antonini/golibjpegturbo v0.0.0-20141208033414-c03a2fa1e89a
|
||||||
|
github.com/disintegration/imaging v1.6.2
|
||||||
|
github.com/gen2brain/base64 v0.0.0-20221015184129-317a5c93030c
|
||||||
|
github.com/gen2brain/go-opencv v0.0.0-20191005190506-bf186fc94f7a
|
||||||
|
github.com/jamiealquiza/envy v1.1.0
|
||||||
|
github.com/korandiz/v4l v1.1.0
|
||||||
|
github.com/pbnjay/pixfont v0.0.0-20200714042608-33b744692567
|
||||||
|
gocv.io/x/gocv v0.35.0
|
||||||
|
nhooyr.io/websocket v1.8.10
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
|
github.com/spf13/cobra v1.8.0 // indirect
|
||||||
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
|
golang.org/x/crypto v0.18.0 // indirect
|
||||||
|
golang.org/x/image v0.15.0 // indirect
|
||||||
|
golang.org/x/net v0.20.0 // indirect
|
||||||
|
golang.org/x/sys v0.16.0 // indirect
|
||||||
|
)
|
||||||
|
|
||||||
|
go 1.21
|
||||||
42
go.sum
Normal file
42
go.sum
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
github.com/abbot/go-http-auth v0.4.0 h1:QjmvZ5gSC7jm3Zg54DqWE/T5m1t2AfDu6QlXJT0EVT0=
|
||||||
|
github.com/abbot/go-http-auth v0.4.0/go.mod h1:Cz6ARTIzApMJDzh5bRMSUou6UMSp0IEXg9km/ci7TJM=
|
||||||
|
github.com/antonini/golibjpegturbo v0.0.0-20141208033414-c03a2fa1e89a h1:+Q4qlzO9KeavJSvWgRKckT3ViTSQ8rR6GroSiXPRXhs=
|
||||||
|
github.com/antonini/golibjpegturbo v0.0.0-20141208033414-c03a2fa1e89a/go.mod h1:UOX4aiVZ5WVUBY3D/31H4m6Z8UHgBj5Qr/oldRBBUMY=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
|
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||||
|
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||||
|
github.com/gen2brain/base64 v0.0.0-20221015184129-317a5c93030c h1:TUjjeJ2rV4KZxH6hIEi/boEQB3v6aKvwdakUJR3AwiE=
|
||||||
|
github.com/gen2brain/base64 v0.0.0-20221015184129-317a5c93030c/go.mod h1:VG58IUyxPWojCtGwqwoZ/6LLXwClu1tssqa5ktOxI9o=
|
||||||
|
github.com/gen2brain/go-opencv v0.0.0-20191005190506-bf186fc94f7a h1:0arrt5Ke40opD5glNdh9ltrkZ0jaqPWsquGPSE3ukug=
|
||||||
|
github.com/gen2brain/go-opencv v0.0.0-20191005190506-bf186fc94f7a/go.mod h1:pOLh42huXUuMoJWvD2K+EeXzvQ9GZ5HN6gdFk5ZwIuU=
|
||||||
|
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
github.com/jamiealquiza/envy v1.1.0 h1:Nwh4wqTZ28gDA8zB+wFkhnUpz3CEcO12zotjeqqRoKE=
|
||||||
|
github.com/jamiealquiza/envy v1.1.0/go.mod h1:MP36BriGCLwEHhi1OU8E9569JNZrjWfCvzG7RsPnHus=
|
||||||
|
github.com/korandiz/v4l v1.1.0 h1:VbzaWlhqNzVPfHEYEM+V8T7184ndiEzljJgDHSHc7pc=
|
||||||
|
github.com/korandiz/v4l v1.1.0/go.mod h1:pftxPG7hkuUgepioAY6PAE81mShaVjzd95X/WF4Izus=
|
||||||
|
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
|
||||||
|
github.com/pbnjay/pixfont v0.0.0-20200714042608-33b744692567 h1:pKjmNHL7BCXhgsnSlN6Ov3WAN2jbJMCx6IvrMN9GNfc=
|
||||||
|
github.com/pbnjay/pixfont v0.0.0-20200714042608-33b744692567/go.mod h1:ytYavTmrpWG4s7UOfDhP6m4ASL5XA66nrOcUn1e2M78=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
|
||||||
|
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
|
||||||
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
gocv.io/x/gocv v0.35.0 h1:Qaxb5KdVyy8Spl4S4K0SMZ6CVmKtbfoSGQAxRD3FZlw=
|
||||||
|
gocv.io/x/gocv v0.35.0/go.mod h1:oc6FvfYqfBp99p+yOEzs9tbYF9gOrAQSeL/dyIPefJU=
|
||||||
|
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
|
||||||
|
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
|
||||||
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
|
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
|
||||||
|
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
|
||||||
|
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
|
||||||
|
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
|
||||||
|
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
|
||||||
|
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
|
||||||
|
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
|
||||||
@@ -13,20 +13,13 @@ type HTML struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewHTML returns new HTML handler.
|
// NewHTML returns new HTML handler.
|
||||||
func NewHTML(bind string, width, height float64, nogl bool) *HTML {
|
func NewHTML(width, height float64, nogl bool) *HTML {
|
||||||
h := &HTML{}
|
h := &HTML{}
|
||||||
|
|
||||||
b := strings.Split(bind, ":")
|
|
||||||
if b[0] == "" {
|
|
||||||
bind = "127.0.0.1" + bind
|
|
||||||
}
|
|
||||||
|
|
||||||
tpl := htmlWebGL
|
tpl := htmlWebGL
|
||||||
if nogl {
|
if nogl {
|
||||||
tpl = html
|
tpl = html
|
||||||
}
|
}
|
||||||
|
|
||||||
tpl = strings.Replace(tpl, "{BIND}", bind, -1)
|
|
||||||
tpl = strings.Replace(tpl, "{WIDTH}", fmt.Sprintf("%.0f", width), -1)
|
tpl = strings.Replace(tpl, "{WIDTH}", fmt.Sprintf("%.0f", width), -1)
|
||||||
tpl = strings.Replace(tpl, "{HEIGHT}", fmt.Sprintf("%.0f", height), -1)
|
tpl = strings.Replace(tpl, "{HEIGHT}", fmt.Sprintf("%.0f", height), -1)
|
||||||
|
|
||||||
@@ -52,7 +45,7 @@ var html = `<html>
|
|||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<title>cam2ip</title>
|
<title>cam2ip</title>
|
||||||
<script>
|
<script>
|
||||||
ws = new WebSocket("ws://{BIND}/socket");
|
ws = new WebSocket("ws://" + window.location.host + "/socket");
|
||||||
var image = new Image();
|
var image = new Image();
|
||||||
|
|
||||||
ws.onopen = function() {
|
ws.onopen = function() {
|
||||||
@@ -85,11 +78,11 @@ var htmlWebGL = `<html>
|
|||||||
<script>
|
<script>
|
||||||
var texture, vloc, tloc, vertexBuff, textureBuff;
|
var texture, vloc, tloc, vertexBuff, textureBuff;
|
||||||
|
|
||||||
ws = new WebSocket("ws://{BIND}/socket");
|
ws = new WebSocket("ws://" + window.location.host + "/socket");
|
||||||
var image = new Image();
|
var image = new Image();
|
||||||
|
|
||||||
ws.onopen = function() {
|
ws.onopen = function() {
|
||||||
var gl = document.getElementById('canvas').getContext('webgl');
|
var gl = document.getElementById('canvas').getContext('webgl',{antialias:false}) || canvas.getContext('experimental-webgl');
|
||||||
|
|
||||||
var vertexShaderSrc =
|
var vertexShaderSrc =
|
||||||
"attribute vec2 aVertex;" +
|
"attribute vec2 aVertex;" +
|
||||||
|
|||||||
32
handlers/index.go
Normal file
32
handlers/index.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Index handler.
|
||||||
|
type Index struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewIndex returns new Index handler.
|
||||||
|
func NewIndex() *Index {
|
||||||
|
return &Index{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP handles requests on incoming connections.
|
||||||
|
func (i *Index) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "GET" && r.Method != "HEAD" {
|
||||||
|
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write([]byte(`<html>
|
||||||
|
<head><title>cam2ip</title></head>
|
||||||
|
<body>
|
||||||
|
<h1>cam2ip</h1>
|
||||||
|
<p><a href='/html'>html</a></p>
|
||||||
|
<p><a href='/jpeg'>jpeg</a></p>
|
||||||
|
<p><a href='/mjpeg'>mjpeg</a></p>
|
||||||
|
</body>
|
||||||
|
</html>`))
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gen2brain/cam2ip/encoder"
|
"github.com/gen2brain/cam2ip/image"
|
||||||
"github.com/gen2brain/cam2ip/reader"
|
"github.com/gen2brain/cam2ip/reader"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ func NewJPEG(reader reader.ImageReader) *JPEG {
|
|||||||
|
|
||||||
// ServeHTTP handles requests on incoming connections.
|
// ServeHTTP handles requests on incoming connections.
|
||||||
func (j *JPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (j *JPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" {
|
if r.Method != "GET" && r.Method != "HEAD" {
|
||||||
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -35,7 +35,7 @@ func (j *JPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = encoder.New(w).Encode(img)
|
err = image.NewEncoder(w).Encode(img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("jpeg: encode: %v", err)
|
log.Printf("jpeg: encode: %v", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"net/textproto"
|
"net/textproto"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gen2brain/cam2ip/encoder"
|
"github.com/gen2brain/cam2ip/image"
|
||||||
"github.com/gen2brain/cam2ip/reader"
|
"github.com/gen2brain/cam2ip/reader"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ func NewMJPEG(reader reader.ImageReader, delay int) *MJPEG {
|
|||||||
|
|
||||||
// ServeHTTP handles requests on incoming connections.
|
// ServeHTTP handles requests on incoming connections.
|
||||||
func (m *MJPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (m *MJPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" {
|
if r.Method != "GET" && r.Method != "HEAD" {
|
||||||
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -62,7 +62,7 @@ loop:
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = encoder.New(partWriter).Encode(img)
|
err = image.NewEncoder(partWriter).Encode(img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("mjpeg: encode: %v", err)
|
log.Printf("mjpeg: encode: %v", err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -2,13 +2,14 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/websocket"
|
"nhooyr.io/websocket"
|
||||||
|
|
||||||
"github.com/gen2brain/cam2ip/encoder"
|
"github.com/gen2brain/cam2ip/image"
|
||||||
"github.com/gen2brain/cam2ip/reader"
|
"github.com/gen2brain/cam2ip/reader"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,13 +20,20 @@ type Socket struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewSocket returns new socket handler.
|
// NewSocket returns new socket handler.
|
||||||
func NewSocket(reader reader.ImageReader, delay int) websocket.Handler {
|
func NewSocket(reader reader.ImageReader, delay int) *Socket {
|
||||||
s := &Socket{reader, delay}
|
return &Socket{reader, delay}
|
||||||
return websocket.Handler(s.write)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write writes images to socket
|
// ServeHTTP handles requests on incoming connections.
|
||||||
func (s *Socket) write(ws *websocket.Conn) {
|
func (s *Socket) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
conn, err := websocket.Accept(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("socket: accept: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
img, err := s.reader.Read()
|
img, err := s.reader.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -35,19 +43,21 @@ func (s *Socket) write(ws *websocket.Conn) {
|
|||||||
|
|
||||||
w := new(bytes.Buffer)
|
w := new(bytes.Buffer)
|
||||||
|
|
||||||
err = encoder.New(w).Encode(img)
|
err = image.NewEncoder(w).Encode(img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("socket: encode: %v", err)
|
log.Printf("socket: encode: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
b64 := base64.StdEncoding.EncodeToString(w.Bytes())
|
b64 := image.EncodeToString(w.Bytes())
|
||||||
|
|
||||||
_, err = ws.Write([]byte(b64))
|
err = conn.Write(ctx, websocket.MessageText, []byte(b64))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Duration(s.delay) * time.Millisecond)
|
time.Sleep(time.Duration(s.delay) * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conn.Close(websocket.StatusNormalClosure, "")
|
||||||
}
|
}
|
||||||
|
|||||||
12
image/base64.go
Normal file
12
image/base64.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
//go:build !amd64
|
||||||
|
// +build !amd64
|
||||||
|
|
||||||
|
package image
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
)
|
||||||
|
|
||||||
|
func EncodeToString(src []byte) string {
|
||||||
|
return base64.StdEncoding.EncodeToString(src)
|
||||||
|
}
|
||||||
12
image/base64_amd64.go
Normal file
12
image/base64_amd64.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
//go:build amd64
|
||||||
|
// +build amd64
|
||||||
|
|
||||||
|
package image
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gen2brain/base64"
|
||||||
|
)
|
||||||
|
|
||||||
|
func EncodeToString(src []byte) string {
|
||||||
|
return base64.StdEncoding.EncodeToString(src)
|
||||||
|
}
|
||||||
27
image/decode.go
Normal file
27
image/decode.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
//go:build turbo
|
||||||
|
// +build turbo
|
||||||
|
|
||||||
|
// Package image.
|
||||||
|
package image
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
jpeg "github.com/antonini/golibjpegturbo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewDecoder returns a new Decoder.
|
||||||
|
func NewDecoder(r io.Reader) *Decoder {
|
||||||
|
return &Decoder{r}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoder struct.
|
||||||
|
type Decoder struct {
|
||||||
|
r io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode decodes image from JPEG.
|
||||||
|
func (d Decoder) Decode() (image.Image, error) {
|
||||||
|
return jpeg.Decode(d.r)
|
||||||
|
}
|
||||||
26
image/decode_jpeg.go
Normal file
26
image/decode_jpeg.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//go:build !turbo
|
||||||
|
// +build !turbo
|
||||||
|
|
||||||
|
// Package image.
|
||||||
|
package image
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/jpeg"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewDecoder returns a new Decoder.
|
||||||
|
func NewDecoder(r io.Reader) *Decoder {
|
||||||
|
return &Decoder{r}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoder struct.
|
||||||
|
type Decoder struct {
|
||||||
|
r io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode decodes image from JPEG.
|
||||||
|
func (d Decoder) Decode() (image.Image, error) {
|
||||||
|
return jpeg.Decode(d.r)
|
||||||
|
}
|
||||||
@@ -1,16 +1,18 @@
|
|||||||
// Package encoder.
|
//go:build turbo
|
||||||
package encoder
|
// +build turbo
|
||||||
|
|
||||||
|
// Package image.
|
||||||
|
package image
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
//"image/jpeg"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
jpeg "github.com/antonini/golibjpegturbo"
|
jpeg "github.com/antonini/golibjpegturbo"
|
||||||
)
|
)
|
||||||
|
|
||||||
// New returns a new Encoder.
|
// NewEncoder returns a new Encoder.
|
||||||
func New(w io.Writer) *Encoder {
|
func NewEncoder(w io.Writer) *Encoder {
|
||||||
return &Encoder{w}
|
return &Encoder{w}
|
||||||
}
|
}
|
||||||
|
|
||||||
31
image/encode_jpeg.go
Normal file
31
image/encode_jpeg.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
//go:build !turbo
|
||||||
|
// +build !turbo
|
||||||
|
|
||||||
|
// Package image.
|
||||||
|
package image
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/jpeg"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewEncoder returns a new Encoder.
|
||||||
|
func NewEncoder(w io.Writer) *Encoder {
|
||||||
|
return &Encoder{w}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encoder struct.
|
||||||
|
type Encoder struct {
|
||||||
|
w io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode encodes image to JPEG.
|
||||||
|
func (e Encoder) Encode(img image.Image) error {
|
||||||
|
err := jpeg.Encode(e.w, img, &jpeg.Options{Quality: 75})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
72
make.bash
72
make.bash
@@ -2,33 +2,43 @@
|
|||||||
|
|
||||||
CHROOT="/usr/x86_64-pc-linux-gnu-static"
|
CHROOT="/usr/x86_64-pc-linux-gnu-static"
|
||||||
MINGW="/usr/i686-w64-mingw32"
|
MINGW="/usr/i686-w64-mingw32"
|
||||||
|
MINGW64="/usr/x86_64-w64-mingw32"
|
||||||
RPI="/usr/armv6j-hardfloat-linux-gnueabi"
|
RPI="/usr/armv6j-hardfloat-linux-gnueabi"
|
||||||
RPI3="/usr/armv7a-hardfloat-linux-gnueabi"
|
RPI3="/usr/armv7a-hardfloat-linux-gnueabi"
|
||||||
|
APPLE="/usr/x86_64-apple-darwin14"
|
||||||
|
ANDROID="/usr/arm-linux-androideabi"
|
||||||
|
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
|
|
||||||
LIBRARY_PATH="$CHROOT/usr/lib:$CHROOT/lib" \
|
LIBRARY_PATH="$CHROOT/usr/lib64:$CHROOT/lib64" \
|
||||||
PKG_CONFIG_PATH="$CHROOT/usr/lib/pkgconfig" \
|
PKG_CONFIG_PATH="$CHROOT/usr/lib64/pkgconfig" \
|
||||||
PKG_CONFIG_LIBDIR="$CHROOT/usr/lib/pkgconfig" \
|
PKG_CONFIG_LIBDIR="$CHROOT/usr/lib64/pkgconfig" \
|
||||||
CGO_LDFLAGS="-L$CHROOT/usr/lib -L$CHROOT/lib" \
|
CGO_LDFLAGS="-L$CHROOT/usr/lib64 -L$CHROOT/lib" \
|
||||||
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v -x -o build/cam2ip.linux.amd64 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip
|
CGO_CFLAGS="-I$CHROOT/usr/include" \
|
||||||
|
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -tags cv2,turbo -o build/cam2ip.linux.amd64.cv2 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|
||||||
|
LIBRARY_PATH="$CHROOT/usr/lib64:$CHROOT/lib64" \
|
||||||
|
PKG_CONFIG_PATH="$CHROOT/usr/lib64/pkgconfig" \
|
||||||
|
PKG_CONFIG_LIBDIR="$CHROOT/usr/lib64/pkgconfig" \
|
||||||
|
CGO_LDFLAGS="-L$CHROOT/usr/lib64 -L$CHROOT/lib64" \
|
||||||
|
CGO_CFLAGS="-I$CHROOT/usr/include" \
|
||||||
|
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o build/cam2ip.linux.amd64 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|
||||||
PKG_CONFIG="/usr/bin/i686-w64-mingw32-pkg-config" \
|
PKG_CONFIG="/usr/bin/x86_64-w64-mingw32-pkg-config" \
|
||||||
PKG_CONFIG_PATH="$MINGW/usr/lib/pkgconfig" \
|
PKG_CONFIG_PATH="$MINGW64/usr/lib/pkgconfig" \
|
||||||
PKG_CONFIG_LIBDIR="$MINGW/usr/lib/pkgconfig" \
|
PKG_CONFIG_LIBDIR="$MINGW64/usr/lib/pkgconfig" \
|
||||||
CGO_LDFLAGS="-L$MINGW/usr/lib" \
|
CGO_LDFLAGS="-L$MINGW64/usr/lib" \
|
||||||
CGO_CFLAGS="-I$MINGW/usr/include" \
|
CGO_CFLAGS="-I$MINGW64/usr/include" \
|
||||||
CC="i686-w64-mingw32-gcc" CXX="i686-w64-mingw32-g++" \
|
CC="x86_64-w64-mingw32-gcc" CXX="x86_64-w64-mingw32-g++" \
|
||||||
CGO_ENABLED=1 GOOS=windows GOARCH=386 go build -v -x -o build/cam2ip.exe -ldflags "-linkmode external -s -w '-extldflags=-static'" github.com/gen2brain/cam2ip
|
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -tags cv2,turbo,pkgconfig -o build/cam2ip.amd64.cv2.exe -ldflags "-linkmode external -s -w '-extldflags=-static'" github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|
||||||
PKG_CONFIG="/usr/bin/armv6j-hardfloat-linux-gnueabi-pkg-config" \
|
PKG_CONFIG="/usr/bin/x86_64-w64-mingw32-pkg-config" \
|
||||||
PKG_CONFIG_PATH="$RPI/usr/lib/pkgconfig" \
|
PKG_CONFIG_PATH="$MINGW64/usr/lib/pkgconfig" \
|
||||||
PKG_CONFIG_LIBDIR="$RPI/usr/lib/pkgconfig" \
|
PKG_CONFIG_LIBDIR="$MINGW64/usr/lib/pkgconfig" \
|
||||||
CGO_LDFLAGS="-L$RPI/usr/lib" \
|
CGO_LDFLAGS="-L$MINGW64/usr/lib" \
|
||||||
CGO_CFLAGS="-I$RPI/usr/include" \
|
CGO_CFLAGS="-I$MINGW64/usr/include" \
|
||||||
CC="armv6j-hardfloat-linux-gnueabi-gcc" CXX="armv6j-hardfloat-linux-gnueabi-g++" \
|
CC="x86_64-w64-mingw32-gcc" CXX="x86_64-w64-mingw32-g++" \
|
||||||
CGO_ENABLED=1 GOOS=linux GOARCH=arm go build -v -x -o build/cam2ip.linux.arm -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip
|
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o build/cam2ip.amd64.exe -ldflags "-linkmode external -s -w '-extldflags=-static'" github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|
||||||
PKG_CONFIG="/usr/bin/armv7a-hardfloat-linux-gnueabi-pkg-config" \
|
PKG_CONFIG="/usr/bin/armv7a-hardfloat-linux-gnueabi-pkg-config" \
|
||||||
PKG_CONFIG_PATH="$RPI3/usr/lib/pkgconfig" \
|
PKG_CONFIG_PATH="$RPI3/usr/lib/pkgconfig" \
|
||||||
@@ -36,4 +46,26 @@ PKG_CONFIG_LIBDIR="$RPI3/usr/lib/pkgconfig" \
|
|||||||
CGO_LDFLAGS="-L$RPI3/usr/lib" \
|
CGO_LDFLAGS="-L$RPI3/usr/lib" \
|
||||||
CGO_CFLAGS="-I$RPI3/usr/include" \
|
CGO_CFLAGS="-I$RPI3/usr/include" \
|
||||||
CC="armv7a-hardfloat-linux-gnueabi-gcc" CXX="armv7a-hardfloat-linux-gnueabi-g++" \
|
CC="armv7a-hardfloat-linux-gnueabi-gcc" CXX="armv7a-hardfloat-linux-gnueabi-g++" \
|
||||||
CGO_ENABLED=1 GOOS=linux GOARCH=arm go build -v -x -o build/cam2ip.linux.arm7 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip
|
CGO_ENABLED=1 GOOS=linux GOARCH=arm go build -tags cv2,turbo -o build/cam2ip.linux.arm7.cv2 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|
||||||
|
PKG_CONFIG="/usr/bin/armv7a-hardfloat-linux-gnueabi-pkg-config" \
|
||||||
|
PKG_CONFIG_PATH="$RPI3/usr/lib/pkgconfig" \
|
||||||
|
PKG_CONFIG_LIBDIR="$RPI3/usr/lib/pkgconfig" \
|
||||||
|
CGO_LDFLAGS="-L$RPI3/usr/lib" \
|
||||||
|
CGO_CFLAGS="-I$RPI3/usr/include" \
|
||||||
|
CC="armv7a-hardfloat-linux-gnueabi-gcc" CXX="armv7a-hardfloat-linux-gnueabi-g++" \
|
||||||
|
CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -o build/cam2ip.linux.arm7 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|
||||||
|
PKG_CONFIG_PATH="$APPLE/SDK/MacOSX10.10.sdk/usr/lib/pkgconfig" \
|
||||||
|
PKG_CONFIG_LIBDIR="$APPLE/SDK/MacOSX10.10.sdk/usr/lib/pkgconfig" \
|
||||||
|
CGO_LDFLAGS="-L$APPLE/SDK/MacOSX10.10.sdk/usr/lib -mmacosx-version-min=10.10" \
|
||||||
|
CGO_CFLAGS="-I$APPLE/SDK/MacOSX10.10.sdk/usr/include -mmacosx-version-min=10.10" \
|
||||||
|
CC="$APPLE/bin/x86_64-apple-darwin14-clang" CXX="$APPLE/bin/x86_64-apple-darwin14-clang++" \
|
||||||
|
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -tags cv2,turbo -o build/cam2ip.darwin.amd64 -ldflags "-linkmode external -s -w '-extldflags=-mmacosx-version-min=10.10'" github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|
||||||
|
PKG_CONFIG_PATH="$ANDROID/lib/pkgconfig" \
|
||||||
|
PKG_CONFIG_LIBDIR="$ANDROID/lib/pkgconfig" \
|
||||||
|
CGO_LDFLAGS="-L$ANDROID/sysroot/usr/lib" \
|
||||||
|
CGO_CFLAGS="-I$ANDROID/sysroot/usr/include --sysroot=$ANDROID/sysroot" \
|
||||||
|
CC="$ANDROID/bin/arm-linux-androideabi-clang" CXX="$ANDROID/bin/arm-linux-androideabi-clang++" \
|
||||||
|
CGO_ENABLED=1 GOOS=android GOARCH=arm go build -o build/cam2ip.android.arm7 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip/cmd/cam2ip
|
||||||
|
|||||||
@@ -26,7 +26,10 @@ type Server struct {
|
|||||||
FrameWidth float64
|
FrameWidth float64
|
||||||
FrameHeight float64
|
FrameHeight float64
|
||||||
|
|
||||||
NoWebGL bool
|
Rotate int
|
||||||
|
|
||||||
|
NoWebGL bool
|
||||||
|
Timestamp bool
|
||||||
|
|
||||||
FileName string
|
FileName string
|
||||||
|
|
||||||
@@ -47,23 +50,20 @@ func (s *Server) ListenAndServe() error {
|
|||||||
basic = auth.NewBasicAuthenticator(realm, auth.HtpasswdFileProvider(s.Htpasswd))
|
basic = auth.NewBasicAuthenticator(realm, auth.HtpasswdFileProvider(s.Htpasswd))
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Handle("/html", newAuthHandler(handlers.NewHTML(s.Bind, s.FrameWidth, s.FrameHeight, s.NoWebGL), basic))
|
http.Handle("/html", newAuthHandler(handlers.NewHTML(s.FrameWidth, s.FrameHeight, s.NoWebGL), basic))
|
||||||
http.Handle("/jpeg", newAuthHandler(handlers.NewJPEG(s.Reader), basic))
|
http.Handle("/jpeg", newAuthHandler(handlers.NewJPEG(s.Reader), basic))
|
||||||
http.Handle("/mjpeg", newAuthHandler(handlers.NewMJPEG(s.Reader, s.Delay), basic))
|
http.Handle("/mjpeg", newAuthHandler(handlers.NewMJPEG(s.Reader, s.Delay), basic))
|
||||||
|
http.Handle("/socket", newAuthHandler(handlers.NewSocket(s.Reader, s.Delay), basic))
|
||||||
http.Handle("/socket", handlers.NewSocket(s.Reader, s.Delay))
|
|
||||||
|
|
||||||
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.Handle("/", newAuthHandler(handlers.NewIndex(), basic))
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
})
|
|
||||||
|
|
||||||
srv := &http.Server{}
|
srv := &http.Server{}
|
||||||
|
|
||||||
listener, err := net.Listen("tcp4", s.Bind)
|
listener, err := net.Listen("tcp", s.Bind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
// Package video.
|
|
||||||
package video
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"image"
|
|
||||||
|
|
||||||
"github.com/lazywei/go-opencv/opencv"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Video represents video.
|
|
||||||
type Video struct {
|
|
||||||
video *opencv.Capture
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns new Video for given path.
|
|
||||||
func New(filename string) (video *Video, err error) {
|
|
||||||
video = &Video{}
|
|
||||||
|
|
||||||
video.video = opencv.NewFileCapture(filename)
|
|
||||||
if video.video == nil {
|
|
||||||
err = fmt.Errorf("video: can not open video %s", filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read reads next frame from video and returns image.
|
|
||||||
func (v *Video) Read() (img image.Image, err error) {
|
|
||||||
if v.video.GrabFrame() {
|
|
||||||
frame := v.video.RetrieveFrame(1)
|
|
||||||
img = frame.ToImage()
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("video: can not grab frame")
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close closes video.
|
|
||||||
func (v *Video) Close() (err error) {
|
|
||||||
if v.video == nil {
|
|
||||||
err = fmt.Errorf("video: video is not opened")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
v.video.Release()
|
|
||||||
v.video = nil
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
package video
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"image/jpeg"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestVideo(t *testing.T) {
|
|
||||||
video, err := New("test.mp4")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer video.Close()
|
|
||||||
|
|
||||||
tmpdir, err := ioutil.TempDir(os.TempDir(), "cam2ip")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
var i int
|
|
||||||
var n int = 10
|
|
||||||
|
|
||||||
timeout := time.After(time.Duration(n) * time.Second)
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-timeout:
|
|
||||||
//fmt.Printf("Fps: %d\n", i/n)
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
img, err := video.Read()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Create(filepath.Join(tmpdir, fmt.Sprintf("%03d.jpg", i)))
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = jpeg.Encode(file, img, &jpeg.Options{Quality: 75})
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = file.Close()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user