Search in sources :

Example 1 with YuvImage

use of android.graphics.YuvImage in project meatspace-android by RomainPiel.

the class PreviewHelper method onPreviewFrame.

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
    if (capturing) {
        long now = System.currentTimeMillis();
        float duration = now - lastTick;
        t += duration;
        if (onCaptureListener != null) {
            onCaptureListener.onCaptureProgress(((float) t) / Constants.CAPTURE_DURATION);
        }
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = parameters.getPreviewSize();
        YuvImage image = new YuvImage(data, parameters.getPreviewFormat(), size.width, size.height, null);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        image.compressToJpeg(new Rect(0, 0, image.getWidth(), image.getHeight()), 90, output);
        Bitmap bitmap = BitmapFactory.decodeByteArray(output.toByteArray(), 0, output.size());
        boolean realSized = angle % 180 == 0;
        float ratio = Constants.CAPTURE_WIDTH / Constants.CAPTURE_HEIGHT;
        float srcWidth = realSized ? image.getWidth() : (float) image.getHeight() / ratio;
        float srcHeight = realSized ? (float) image.getWidth() / ratio : image.getHeight();
        float scaleFactor = realSized ? Constants.CAPTURE_WIDTH / image.getWidth() : Constants.CAPTURE_WIDTH / image.getHeight();
        Matrix matrix = new Matrix();
        matrix.postRotate(isFrontCamera ? -angle : angle);
        matrix.postScale(isFrontCamera ? -scaleFactor : scaleFactor, scaleFactor);
        int startX = (int) (image.getWidth() - srcWidth) / 2;
        int startY = (int) (image.getHeight() - srcHeight) / 2;
        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, startX, startY, (int) srcWidth, (int) srcHeight, matrix, true);
        gifEncoder.setDelay((int) (duration / Constants.CAPTURE_ACCELERATION));
        gifEncoder.addFrame(rotatedBitmap);
        rotatedBitmap.recycle();
        bitmap.recycle();
        lastTick = now;
    }
}
Also used : Rect(android.graphics.Rect) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) YuvImage(android.graphics.YuvImage) Camera(android.hardware.Camera) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with YuvImage

use of android.graphics.YuvImage in project Signal-Android by WhisperSystems.

the class BitmapUtil method createFromNV21.

public static byte[] createFromNV21(@NonNull final byte[] data, final int width, final int height, int rotation, final Rect croppingRect, final boolean flipHorizontal) throws IOException {
    byte[] rotated = rotateNV21(data, width, height, rotation, flipHorizontal);
    final int rotatedWidth = rotation % 180 > 0 ? height : width;
    final int rotatedHeight = rotation % 180 > 0 ? width : height;
    YuvImage previewImage = new YuvImage(rotated, ImageFormat.NV21, rotatedWidth, rotatedHeight, null);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    previewImage.compressToJpeg(croppingRect, 80, outputStream);
    byte[] bytes = outputStream.toByteArray();
    outputStream.close();
    return bytes;
}
Also used : YuvImage(android.graphics.YuvImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with YuvImage

use of android.graphics.YuvImage in project PhotoNoter by yydcdut.

the class CameraServicePresenterImpl method decodeNV21.

private byte[] decodeNV21(byte[] data, int width, int height) {
    YuvImage yuv = new YuvImage(data, ImageFormat.NV21, width, height, null);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    yuv.compressToJpeg(new Rect(0, 0, width, height), 100, bos);
    byte[] bytes = bos.toByteArray();
    FilePathUtils.closeStream(bos);
    return bytes;
}
Also used : Rect(android.graphics.Rect) YuvImage(android.graphics.YuvImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with YuvImage

use of android.graphics.YuvImage in project PhotoNoter by yydcdut.

the class SandBoxServicePresenterImpl method decodeNV21.

private byte[] decodeNV21(byte[] data, int width, int height) {
    YuvImage yuv = new YuvImage(data, ImageFormat.NV21, width, height, null);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    yuv.compressToJpeg(new Rect(0, 0, width, height), 100, bos);
    byte[] bytes = bos.toByteArray();
    FilePathUtils.closeStream(bos);
    return bytes;
}
Also used : Rect(android.graphics.Rect) YuvImage(android.graphics.YuvImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with YuvImage

use of android.graphics.YuvImage in project JustAndroid by chinaltz.

the class AbImageUtil method yuv2Bitmap.

/**
	 * 将yuv格式转换成bitmap
	 * ImageFormat.NV21 || format == ImageFormat.YUY2
	 * @param data yuv 数据
	 * @param width
	 * @param height
	 * @return RGB565 format bitmap
	 */
public static Bitmap yuv2Bitmap(byte[] data, int width, int height) {
    final YuvImage image = new YuvImage(data, ImageFormat.NV21, width, height, null);
    ByteArrayOutputStream os = new ByteArrayOutputStream(data.length);
    if (!image.compressToJpeg(new Rect(0, 0, width, height), 100, os)) {
        return null;
    }
    byte[] tmp = os.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
    return bitmap;
}
Also used : Rect(android.graphics.Rect) Bitmap(android.graphics.Bitmap) YuvImage(android.graphics.YuvImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

YuvImage (android.graphics.YuvImage)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Rect (android.graphics.Rect)6 Bitmap (android.graphics.Bitmap)3 Matrix (android.graphics.Matrix)2 Camera (android.hardware.Camera)2 BitmapFactory (android.graphics.BitmapFactory)1 IOException (java.io.IOException)1