- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在下面有一些代码..它将 YUV_420_888 (android camera2 api) 图像格式转换为 ARGB8888 并将其保存为 jpeg ..但是结果几乎完全是绿色的..知道如何让它工作预计??
private ByteBuffer image2ByteBuffer(Image image) {
Image.Plane Y = image.getPlanes()[0];
Image.Plane U = image.getPlanes()[1];
Image.Plane V = image.getPlanes()[2];
int yRowStride = Y.getRowStride();
int yPixelStride = Y.getPixelStride();
int uRowStride = U.getRowStride();
int uPixelStride = U.getPixelStride();
int vRowStride = V.getRowStride();
int vPixelStride = V.getPixelStride();
ByteBuffer YBuffer = Y.getBuffer();
ByteBuffer UBuffer = U.getBuffer();
ByteBuffer VBuffer = V.getBuffer();
YBuffer.rewind();
UBuffer.rewind();
VBuffer.rewind();
int yb, ub, vb;
yb = YBuffer.capacity();
ub = UBuffer.capacity();
vb = VBuffer.capacity();
byte[] ybb, ubb,vbb;
ybb = new byte[yb];
ubb = new byte[ub];
vbb = new byte[vb];
YBuffer.get(ybb);
UBuffer.get(ubb);
VBuffer.get(vbb);
byte[] yData = ybb;
byte[] uData = new byte[yb/4];
byte[] vData = new byte[yb/4];
int index = 0;
for(int i = 0; (i < (640 * 480 * 0.5)) && (index < (640 * 480 * 0.25)); i += uPixelStride){
uData[index] = ubb[i];
vData[index] = vbb[i];
index++;
}
byte[] yuv420sp = new byte[(int)(640 * 480 * 1.5)];
// Y DATA FIRST
for (int i = 0 ; i < 640 * 480; i++)
yuv420sp[i] = yData[i];
//UV INTERLEAVED DATA NEXT
index = 640 * 480;
for (int i = 0; i < (640 * 480 * 0.25); i++) {
yuv420sp[index] = uData[i];
index++;
yuv420sp[index] = vData[i];
index++;
}
int[] decoded = new int[640 * 480];
YUV420Converter.decodeYUV420888(decoded, yuv420sp, 640, 480);
Bitmap bitmap = Bitmap.createBitmap(decoded, 640, 480, Bitmap.Config.ARGB_8888);
ByteBuffer byteBuffer = ByteBuffer.allocate(640 * 480 * 4);
bitmap.copyPixelsToBuffer(byteBuffer);
bitmap.recycle();
return byteBuffer;
}
下一段代码来自图像阅读器
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
ByteBuffer byteBuffer = image2ByteBuffer(image);
saveImage(byteBuffer);
byteBuffer.clear();
image.close();
}
public void saveImage(ByteBuffer imageByteBuffer) {
try {
imageByteBuffer.rewind();
Bitmap imageBitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
imageBitmap.copyPixelsFromBuffer(imageByteBuffer);
String filePath = Environment.getExternalStorageDirectory().getPath()
+ "/pic" + mImagesCount + ".png";
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
//fileOutputStream.write(imageByteArray);
fileOutputStream.flush();
fileOutputStream.close();
imageBitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
一旦我们有了正确格式化的 YUV,用于解码为 ARGB
public static void decodeYUV420888(int[] rgb, byte[] yuv420sp, int width, int height) {
final int frameSize = width * height;
for (int j = 0, yp = 0; j < height; j++) { int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & ((int) yuv420sp[yp])) - 16;
if (y < 0)
y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}
int y1192 = 1192 * y;
int b = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int r = (y1192 + 2066 * u);
if (r < 0) r = 0; else if (r > 262143)
r = 262143;
if (g < 0) g = 0; else if (g > 262143)
g = 262143;
if (b < 0) b = 0; else if (b > 262143)
b = 262143;
rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
}
}
}
最佳答案
显然 API 21 存在问题,但已在 API 22 中解决。
我在以下帖子中找到了这个:yuv_420_888-to-rgb
可以在此处找到关于此主题的另一篇优秀文章:Solving some android problems
希望对您有所帮助!
关于java - YUV_420_888转ARGB变绿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38084923/
通过使用 BufferedImage 对象调用 getRGB(int x, int y),可以获得一个负数。 如何将三个不同的值(红色、绿色和蓝色)转换为这个单一的负数? 最佳答案 使用颜色类: ne
我正在尝试在以下之间创建渐变颜色: White (light to dark) Brown (dark to light Green (dark to ligh) 但我不知道如何实现。 我的尝试很糟糕
在我的 Cloudformation 模板中,我使用蓝绿部署触发器并具有以下任务定义 TaskDefinition: Type: AWS::ECS::TaskDefinition Depend
我似乎无法弄清楚我做错了什么。这是课本上的练习: “在 Java 库中,颜色由 0 到 255 之间的红色、绿色和蓝色分量指定(请参见第 68 页的表 4)。编写一个程序 BrighterDemo,用
我计划从网页中提取(本质上是在获得许可的情况下抓取)一些数据并将其存储在 elasticsearch 中(你知道,用于搜索)。 虽然我有权从网站上抓取数据, 此数据没有 API 或其他结构化来源 它是
我得到了一个本质上是图像的数据集,但是图像中的每个像素都表示为从 -1 到 1 的值。我正在编写一个应用程序,它需要将这些 -1 到 1 灰度值映射到 MATLAB“Jet”色标(红-绿-蓝颜色渐变)
如何在 BufferedImage 中隔离红/绿/蓝 channel :我有以下代码不起作用:` public static BufferedImage isolateChannel(Buffered
背景(可选) 我目前正在从事我们公司从以前的承包商那里得到的一个项目,它在测试覆盖率方面处于非常糟糕的状态——几乎没有测试,代码库非常复杂和脆弱,所以任何小的改变很可能以最复杂和令人惊讶的方式打破一切
我是一名优秀的程序员,十分优秀!