-
Notifications
You must be signed in to change notification settings - Fork 15
/
txtcam.m
130 lines (108 loc) · 3.5 KB
/
txtcam.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <math.h>
#include <signal.h>
@interface myThread:NSThread {
}
@end
@implementation myThread:NSThread
- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection {
@synchronized (self) {
CIImage* image = [CIImage imageWithCVImageBuffer:videoFrame];
CGSize size = image.extent.size;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCIImage: image];
int x;
int y;
float bg;
float fg;
float tmp;
float mod;
char* chr;
printf("\033[0;0H"); // Move cursor to top right
for (y=0; y<size.height; y += 1) {
for (x=0; x<size.width; x++) {
CGFloat b = [[rep colorAtX:x y:y] brightnessComponent];
bg = (b * 23 + 232);
fg = fmin(255, bg+1);
mod = fmodf(bg, 1.0);
if (mod < 0.2) {
chr = " ";
} else if (mod < 0.4) {
chr = "░";
} else if (mod < 0.6) {
chr = "▒";
} else if (mod < 0.8) {
tmp = bg; bg = fg; fg = tmp;
chr = "▒";
} else {
tmp = bg; bg = fg; fg = tmp;
chr = "░";
}
printf("\033[48;5;%im\033[38;5;%im%s", (int)bg, (int)fg, chr);
}
if (y<size.height-1) printf("\n");
}
}
}
- (void)main {
struct winsize max;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *a = [QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo];
QTCaptureDevice *cam = [a objectAtIndex:0];
NSError *err;
if ([cam open:&err] != YES) {
NSLog(@"Error opening camera: %@", err);
}
QTCaptureDeviceInput *in = [[QTCaptureDeviceInput alloc] initWithDevice:cam];
QTCaptureSession *session = [[QTCaptureSession alloc] init];
if ([session addInput:in error:&err] != YES) {
NSLog(@"Error adding input to capture session: %@", err);
}
// Grab decompressed output
QTCaptureVideoPreviewOutput *previewOutput;
previewOutput = [[QTCaptureVideoPreviewOutput alloc] init];
[previewOutput setPixelBufferAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:64], (id)kCVPixelBufferWidthKey,
[NSNumber numberWithInt:48/2], (id)kCVPixelBufferHeightKey,
nil]];
[previewOutput setDelegate:self];
int success = [session addOutput:previewOutput error:&err];
if (!success) {
NSLog(@"Error adding preview output to capture session: %@", err);
exit(1);
}
printf("\033[2J"); // clear screen
printf( "\x1B[?25l"); // disable cursor
[session startRunning];
while(1) {
sleep(1);
// Update preview size based on size of terminal
ioctl(0, TIOCGWINSZ , &max);
[previewOutput setPixelBufferAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:max.ws_col], (id)kCVPixelBufferWidthKey,
[NSNumber numberWithInt:max.ws_row], (id)kCVPixelBufferHeightKey,
nil]];
sleep(1);
}
[pool release];
}
@end
void intHandler() {
printf("\033[0;0H"); // Move cursor to top right
printf("\033[2J"); // clear screen
printf("\x1B[?25h"); // re-enable cursor
exit(1);
}
int main( int argc, const char* argv[])
{
signal(SIGINT, intHandler);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];
[[[myThread alloc] init] start];
[NSApp run];
[pool release];
return 0;
}