Skip to content

Commit ba6f0bc

Browse files
committed
working
Signed-off-by: Umair Khan <[email protected]>
1 parent b13ad07 commit ba6f0bc

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

src/in/omerjerk/processing/video/android/Capture.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.io.IOException;
44
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import javax.print.attribute.Size2DSyntax;
58

69
import android.content.Context;
710
import android.hardware.Camera;
@@ -24,6 +27,7 @@ public class Capture extends PImage implements PConstants {
2427
private PApplet context;
2528

2629
private Camera mCamera;
30+
private Camera.Parameters parameters;
2731
private Size previewSize;
2832

2933
private static ArrayList<String> camerasList = new ArrayList<String>();
@@ -58,11 +62,17 @@ private void createPreviewWindow() {
5862
params.gravity = Gravity.TOP | Gravity.LEFT;
5963
params.height = 1;
6064
params.width = 1;
61-
65+
6266
try {
6367
mCamera = Camera.open(selectedCamera);
64-
previewSize = mCamera.getParameters().getPreviewSize();
65-
init(previewSize.width, previewSize.height, ARGB);
68+
parameters = mCamera.getParameters();
69+
setMinimumPreviewSize();
70+
mCamera.setParameters(parameters);
71+
previewSize = parameters.getPreviewSize();
72+
init(previewSize.height, previewSize.width, ARGB);
73+
74+
log("Width = " + previewSize.width);
75+
log("height = " + previewSize.height);
6676
final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
6777
context.runOnUiThread(new Runnable() {
6878
@Override
@@ -102,8 +112,9 @@ public String[] list() {
102112

103113
@Override
104114
public void onPreviewFrame(byte[] frame, Camera camera) {
105-
log("preview frame received");
106115
pixels = Utils.convertYUV420_NV21toRGB8888(frame, previewSize.width, previewSize.height);
116+
pixels = Utils.rotateRGBDegree90(pixels, previewSize.width, previewSize.height);
117+
updatePixels();
107118
}
108119
};
109120

@@ -168,4 +179,24 @@ public void surfaceDestroyed(SurfaceHolder holder) {
168179
// do nothing
169180
}
170181
}
182+
183+
private void setMinimumPreviewSize() {
184+
List<Camera.Size> sizes = mCamera.getParameters().getSupportedPreviewSizes();
185+
/*
186+
Camera.Size minSize = null;
187+
for (Camera.Size size : sizes) {
188+
log("Size = " + size.width + " height = " + size.height);
189+
if (minSize == null) {
190+
minSize = size;
191+
continue;
192+
}
193+
if (minSize.width > size.width) {
194+
minSize = size;
195+
}
196+
}*/
197+
Camera.Size minSize = sizes.get(sizes.size() - 8);
198+
log("minimum width = " + minSize.width + " height = " + minSize.height);
199+
// parameters.setPictureSize(minSize.height, minSize.width);
200+
parameters.setPreviewSize(minSize.width, minSize.height);
201+
}
171202
}

src/in/omerjerk/processing/video/android/Utils.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static int[] convertYUV420_NV21toRGB8888(byte [] data, int width, int hei
3636
if (i!=0 && (i+2)%width==0)
3737
i+=width;
3838
}
39+
40+
3941

4042
return pixels;
4143
}
@@ -51,4 +53,41 @@ private static int convertYUVtoRGB(int y, int u, int v) {
5153
b = b>255? 255 : b<0 ? 0 : b;
5254
return 0xff000000 | (b<<16) | (g<<8) | r;
5355
}
56+
57+
public static byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) {
58+
byte [] yuv = new byte[imageWidth*imageHeight*3/2];
59+
// Rotate the Y luma
60+
int i = 0;
61+
for(int x = 0;x < imageWidth;x++)
62+
{
63+
for(int y = imageHeight-1;y >= 0;y--)
64+
{
65+
yuv[i] = data[y*imageWidth+x];
66+
i++;
67+
}
68+
}
69+
// Rotate the U and V color components
70+
i = imageWidth*imageHeight*3/2-1;
71+
for(int x = imageWidth-1;x > 0;x=x-2)
72+
{
73+
for(int y = 0;y < imageHeight/2;y++)
74+
{
75+
yuv[i] = data[(imageWidth*imageHeight)+(y*imageWidth)+x];
76+
i--;
77+
yuv[i] = data[(imageWidth*imageHeight)+(y*imageWidth)+(x-1)];
78+
i--;
79+
}
80+
}
81+
return yuv;
82+
}
83+
84+
public static int[] rotateRGBDegree90(int[] data, int width, int height) {
85+
int[] rotatedData = new int[data.length];
86+
for (int y = 0; y < height; y++) {
87+
for (int x = 0; x < width; x++)
88+
rotatedData[x * height + height - y - 1] = data[x + y * width];
89+
}
90+
return rotatedData;
91+
}
92+
5493
}

0 commit comments

Comments
 (0)