Search in sources :

Example 6 with YuvImage

use of android.graphics.YuvImage in project zxing-android-embedded by journeyapps.

the class SourceData method getBitmap.

private Bitmap getBitmap(Rect cropRect, int scaleFactor) {
    if (isRotated()) {
        //noinspection SuspiciousNameCombination
        cropRect = new Rect(cropRect.top, cropRect.left, cropRect.bottom, cropRect.right);
    }
    // TODO: there should be a way to do this without JPEG compression / decompression cycle.
    YuvImage img = new YuvImage(data, imageFormat, dataWidth, dataHeight, null);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    img.compressToJpeg(cropRect, 90, buffer);
    byte[] jpegData = buffer.toByteArray();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = scaleFactor;
    Bitmap bitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, options);
    // Rotate if required
    if (rotation != 0) {
        Matrix imageMatrix = new Matrix();
        imageMatrix.postRotate(rotation);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), imageMatrix, false);
    }
    return bitmap;
}
Also used : Rect(android.graphics.Rect) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) YuvImage(android.graphics.YuvImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BitmapFactory(android.graphics.BitmapFactory)

Example 7 with YuvImage

use of android.graphics.YuvImage in project CameraKit-Android by flurgle.

the class ProcessStillTask method run.

@Override
public void run() {
    Camera.Parameters parameters = camera.getParameters();
    int width = parameters.getPreviewSize().width;
    int height = parameters.getPreviewSize().height;
    int rotation = cameraInfo.orientation;
    byte[] rotatedData = new Rotation(data, width, height, rotation).getYuv();
    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);
    onStillProcessedListener.onStillProcessed(yuv);
}
Also used : YuvImage(android.graphics.YuvImage) Camera(android.hardware.Camera)

Example 8 with YuvImage

use of android.graphics.YuvImage in project android_packages_apps_Camera by CyanogenMod.

the class PanoramaModule method generateFinalMosaic.

/**
     * Generate the final mosaic image.
     *
     * @param highRes flag to indicate whether we want to get a high-res version.
     * @return a MosaicJpeg with its isValid flag set to true if successful; null if the generation
     *         process is cancelled; and a MosaicJpeg with its isValid flag set to false if there
     *         is an error in generating the final mosaic.
     */
public MosaicJpeg generateFinalMosaic(boolean highRes) {
    int mosaicReturnCode = mMosaicFrameProcessor.createMosaic(highRes);
    if (mosaicReturnCode == Mosaic.MOSAIC_RET_CANCELLED) {
        return null;
    } else if (mosaicReturnCode == Mosaic.MOSAIC_RET_ERROR) {
        return new MosaicJpeg();
    }
    byte[] imageData = mMosaicFrameProcessor.getFinalMosaicNV21();
    if (imageData == null) {
        Log.e(TAG, "getFinalMosaicNV21() returned null.");
        return new MosaicJpeg();
    }
    int len = imageData.length - 8;
    int width = (imageData[len + 0] << 24) + ((imageData[len + 1] & 0xFF) << 16) + ((imageData[len + 2] & 0xFF) << 8) + (imageData[len + 3] & 0xFF);
    int height = (imageData[len + 4] << 24) + ((imageData[len + 5] & 0xFF) << 16) + ((imageData[len + 6] & 0xFF) << 8) + (imageData[len + 7] & 0xFF);
    Log.v(TAG, "ImLength = " + (len) + ", W = " + width + ", H = " + height);
    if (width <= 0 || height <= 0) {
        // TODO: pop up an error message indicating that the final result is not generated.
        Log.e(TAG, "width|height <= 0!!, len = " + (len) + ", W = " + width + ", H = " + height);
        return new MosaicJpeg();
    }
    YuvImage yuvimage = new YuvImage(imageData, ImageFormat.NV21, width, height, null);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(0, 0, width, height), 100, out);
    try {
        out.close();
    } catch (Exception e) {
        Log.e(TAG, "Exception in storing final mosaic", e);
        return new MosaicJpeg();
    }
    return new MosaicJpeg(out.toByteArray(), width, height);
}
Also used : Rect(android.graphics.Rect) YuvImage(android.graphics.YuvImage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

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