YUV旋转之后转bitmap

public Bitmap getPreviewBitmap() {
    long time1 = System.currentTimeMillis();

    byte[] newBytes;
    if (Integer.parseInt(mCameraId) == Camera.CameraInfo.CAMERA_FACING_BACK) {
        newBytes = CameraUtil.rotateYUVDegree90(mBytes, mPreviewBitmapWidth, mPreviewBitmapHeight);
    } else {
        newBytes = CameraUtil.rotateYUVDegree270AndMirror(mBytes, mPreviewBitmapWidth, mPreviewBitmapHeight);
    }

    if (mYUVType == null) {
        mYUVType = new Type.Builder(mRenderScript, Element.U8(mRenderScript)).setX(newBytes.length);
        mInputAllocation = Allocation.createTyped(mRenderScript, mYUVType.create(), Allocation.USAGE_SCRIPT);

        mRGBAType = new Type.Builder(mRenderScript, Element.RGBA_8888(mRenderScript)).setX(mPreviewBitmapHeight).setY(mPreviewBitmapWidth);
        mOutputAllocation = Allocation.createTyped(mRenderScript, mRGBAType.create(), Allocation.USAGE_SCRIPT);
    }

    mInputAllocation.copyFrom(newBytes);
    mScriptIntrinsicYuvToRGB.setInput(mInputAllocation);
    mScriptIntrinsicYuvToRGB.forEach(mOutputAllocation);
    Bitmap bmpout = Bitmap.createBitmap(mPreviewBitmapHeight, mPreviewBitmapWidth, Bitmap.Config.ARGB_8888);
    mOutputAllocation.copyTo(bmpout);

    long time2 = System.currentTimeMillis();
    LogHelper.d(TAG, "onPreviewFrame.deltaTime = " + (time2 - time1));

    return bmpout;
}


public static byte[] rotateYUVDegree90(byte[] data, int imageWidth, int imageHeight) {
    byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
    // Rotate the Y luma
    int i = 0;
    for (int x = 0; x < imageWidth; x++) {
        for (int y = imageHeight - 1; y >= 0; y--) {
            yuv[i] = data[y * imageWidth + x];
            i++;
        }
    }
    // Rotate the U and V color components
    i = imageWidth * imageHeight * 3 / 2 - 1;
    for (int x = imageWidth - 1; x > 0; x = x - 2) {
        for (int y = 0; y < imageHeight / 2; y++) {
            yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
            i--;
            yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
            i--;
        }
    }
    return yuv;
}

public static byte[] rotateYUVDegree270AndMirror(byte[] data, int imageWidth, int imageHeight) {
    byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
    // Rotate and mirror the Y luma
    int i = 0;
    int maxY = 0;
    for (int x = imageWidth - 1; x >= 0; x--) {
        maxY = imageWidth * (imageHeight - 1) + x * 2;
        for (int y = 0; y < imageHeight; y++) {
            yuv[i] = data[maxY - (y * imageWidth + x)];
            i++;
        }
    }
    // Rotate and mirror the U and V color components
    int uvSize = imageWidth * imageHeight;
    i = uvSize;
    int maxUV = 0;
    for (int x = imageWidth - 1; x > 0; x = x - 2) {
        maxUV = imageWidth * (imageHeight / 2 - 1) + x * 2 + uvSize;
        for (int y = 0; y < imageHeight / 2; y++) {
            yuv[i] = data[maxUV - 2 - (y * imageWidth + x - 1)];
            i++;
            yuv[i] = data[maxUV - (y * imageWidth + x)];
            i++;
        }
    }
    return yuv;
}

private byte[] rotateYUVDegree270(byte[] data, int imageWidth, int imageHeight) {
    byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
    // Rotate the Y luma
    int i = 0;
    for (int x = imageWidth - 1; x >= 0; x--) {
        for (int y = 0; y < imageHeight; y++) {
            yuv[i] = data[y * imageWidth + x];
            i++;
        }
    }// Rotate the U and V color components
    i = imageWidth * imageHeight;
    for (int x = imageWidth - 1; x > 0; x = x - 2) {
        for (int y = 0; y < imageHeight / 2; y++) {
            yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
            i++;
            yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
            i++;
        }
    }
    return yuv;
}

你可能感兴趣的:(YUV旋转之后转bitmap)