-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreviewSurface.java
81 lines (63 loc) · 2.49 KB
/
PreviewSurface.java
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
package com.onlylemi.android.capture;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
public class PreviewSurface extends CameraSurface implements
Camera.PreviewCallback {
private static final String TAG = "PreviewSurface:";
private String ipname = "";
private ColorListener listener;
public PreviewSurface(Context paramContext, AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
}
public void onPreviewFrame(byte[] paramArrayOfByte, Camera paramCamera) {
Size size = paramCamera.getParameters().getPreviewSize();
// use "image.compressToJpeg()" to change image data format from "YUV" to "jpg"
YuvImage image = new YuvImage(paramArrayOfByte, ImageFormat.NV21,
size.width, size.height, null);
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
try {
if (image != null) {
image.compressToJpeg(new Rect(0, 0, size.width, size.height),
80, outstream);
outstream.flush();
//start thread to send image data
Thread th = new SendDataThread(outstream, ipname, 6000);
th.start();
}
} catch (Exception ex) {
Log.e(TAG, "Error:" + ex.getMessage());
}
if (listener != null) {
int color = BitmapFactory.decodeStream(new ByteArrayInputStream(outstream.toByteArray
())).getPixel(size.width / 2, size.height / 2);
listener.onColor(color);
}
}
public void surfaceCreated(SurfaceHolder paramSurfaceHolder) {
super.surfaceCreated(paramSurfaceHolder);
this.camera.setPreviewCallback(this);
}
public void surfaceDestroyed(SurfaceHolder paramSurfaceHolder) {
this.camera.setPreviewCallback(null);
super.surfaceDestroyed(paramSurfaceHolder);
}
public void setIP(String ipname) {
this.ipname = ipname;
}
public void setColorListener(ColorListener listener) {
this.listener = listener;
}
public interface ColorListener {
void onColor(int color);
}
}