/** * 图片格式转换 */ private final int colorFormatI420 = 1; private final int colorFormatNV21 = 2; private byte[] data = new byte[0]; private byte[] rowData = new byte[0]; private byte[] getDataFromImage(Image image, int colorFormat) { Rect crop = image.getCropRect(); int format = image.getFormat(); int width = crop.width(); int height = crop.height(); Image.Plane[] planes = image.getPlanes(); if (data.length == 0) { data = new byte[width * height * ImageFormat.getBitsPerPixel(format) / 8]; } if (rowData.length == 0) { rowData = new byte[planes[0].getRowStride()]; } int channelOffset = 0; int outputStride = 1; for (int i = 0; i < planes.length; i++) { switch (i) { case 0: channelOffset = 0; outputStride = 1; break; case 1: if (colorFormat == colorFormatI420) { channelOffset = width * height; outputStride = 1; } else if (colorFormat == colorFormatNV21) { channelOffset = width * height + 1; outputStride = 2; } break; case 2: if (colorFormat == colorFormatI420) { channelOffset = (int) (width * height * 1.25); outputStride = 1; } else if (colorFormat == colorFormatNV21) { channelOffset = width * height; outputStride = 2; } break; default: } ByteBuffer buffer = planes[i].getBuffer(); int rowStride = planes[i].getRowStride(); int pixelStride = planes[i].getPixelStride(); int shift = (i == 0) ? 0 : 1; int w = width >> shift; int h = height >> shift; buffer.position(rowStride * (crop.top >> shift) + pixelStride * (crop.left >> shift)); for (int row = 0; row < h; row++) { int length; if (pixelStride == 1 && outputStride == 1) { length = w; buffer.get(data, channelOffset, length); channelOffset += length; } else { length = (w - 1) * pixelStride + 1; buffer.get(rowData, 0, length); for (int col = 0; col < w; col++) { data[channelOffset] = rowData[col * pixelStride]; channelOffset += outputStride; } } if (row < h - 1) { buffer.position(buffer.position() + rowStride - length); } } } return data; } byte[] frameData; ByteBuffer bufferY; ByteBuffer bufferV; /** * 图片格式yuv转换nv21 */ private void yuvConvert(Image image) { bufferY = image.getPlanes()[0].getBuffer(); bufferV = image.getPlanes()[2].getBuffer(); byte[] y = new byte[bufferY.remaining()]; byte[] v = new byte[bufferV.remaining()]; bufferY.get(y, 0, bufferY.remaining()); bufferV.get(v, 0, bufferV.remaining()); frameData = byteMerger(y, v); }
android camera2 image 中获得的yuv数据的格式转换
猜你喜欢
转载自blog.csdn.net/haoxuezhe1988/article/details/90603731
今日推荐
周排行