Add fast camera mode

pull/1/head
Dylan McIntyre 8 years ago
parent 5d934abacb
commit f7498f0c36
  1. 2
      camerakit/build.gradle
  2. 39
      camerakit/src/main/java/com/flurgle/camerakit/Camera1.java
  3. 37
      camerakit/src/main/java/com/flurgle/camerakit/utils/YuvUtils.java
  4. 2
      demo/src/main/res/layout/activity_main.xml

@ -3,7 +3,7 @@ apply plugin: 'com.android.library'
ext {
PUBLISH_GROUP_ID = 'com.flurgle'
PUBLISH_ARTIFACT_ID = 'camerakit'
PUBLISH_VERSION = '0.9.5'
PUBLISH_VERSION = '0.9.6'
}
android {

@ -1,5 +1,7 @@
package com.flurgle.camerakit;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
@ -8,7 +10,9 @@ import android.support.v4.util.SparseArrayCompat;
import android.view.SurfaceHolder;
import com.flurgle.camerakit.utils.Size;
import com.flurgle.camerakit.utils.YuvUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
@ -162,7 +166,42 @@ public class Camera1 extends CameraViewImpl {
@Override
void captureStill() {
mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
int rotation = mCameraInfo.orientation;
byte[] rotatedData = YuvUtils.rotateNV21(data, width, height, rotation);
int postWidth;
int postHeight;
switch (rotation) {
case 90:
case 270:
postWidth = height;
postHeight = width;
break;
case 0:
case 180:
default:
postWidth = width;
postHeight = height;
break;
}
YuvImage yuv = new YuvImage(rotatedData, parameters.getPreviewFormat(), postWidth, postHeight, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, postWidth, postHeight), 50, out);
byte[] bytes = out.toByteArray();
getCameraListener().onPictureTaken(bytes);
}
});
}
@Override

@ -51,5 +51,42 @@ public class YuvUtils {
return outputStream.toByteArray();
}
public static byte[] rotateNV21(final byte[] yuv, final int width, final int height, final int rotation) {
if (rotation == 0) return yuv;
if (rotation % 90 != 0 || rotation < 0 || rotation > 270) {
throw new IllegalArgumentException("0 <= rotation < 360, rotation % 90 == 0");
}
final byte[] output = new byte[yuv.length];
final int frameSize = width * height;
final boolean swap = rotation % 180 != 0;
final boolean xflip = rotation % 270 != 0;
final boolean yflip = rotation >= 180;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
final int yIn = j * width + i;
final int uIn = frameSize + (j >> 1) * width + (i & ~1);
final int vIn = uIn + 1;
final int wOut = swap ? height : width;
final int hOut = swap ? width : height;
final int iSwapped = swap ? j : i;
final int jSwapped = swap ? i : j;
final int iOut = xflip ? wOut - iSwapped - 1 : iSwapped;
final int jOut = yflip ? hOut - jSwapped - 1 : jSwapped;
final int yOut = jOut * wOut + iOut;
final int uOut = frameSize + (jOut >> 1) * wOut + (iOut & ~1);
final int vOut = uOut + 1;
output[yOut] = (byte) (0xff & yuv[yIn]);
output[uOut] = (byte) (0xff & yuv[uIn]);
output[vOut] = (byte) (0xff & yuv[vIn]);
}
}
return output;
}
}

@ -21,7 +21,7 @@
app:ckCropOutput="true"
app:ckFacing="back"
app:ckFlash="off"
app:ckPictureMode="quality" />
app:ckPictureMode="speed" />
<LinearLayout
android:layout_width="match_parent"

Loading…
Cancel
Save