gpt4 book ai didi

java - 在 Java/android Canvas 上创建具有深度/3D 外观的形状

转载 作者:行者123 更新时间:2023-12-04 20:13:13 27 4
gpt4 key购买 nike

请看下图。 Flat 是输入,输出应该是显示深度的底部图像或直线和曲线的 3D 外观。尝试了 EmbossMaskFilter,但由于闪电,它没有提供相同的输出。

我列出了生成所需输出的 ​​C# 代码。 问题是我如何在 java/android 中实现类似的东西,特别是当我们需要快速处理 Canvas 绘图时对于移动设备

enter image description here

 Bitmap modifiedPiecePicture = (Bitmap)piecePicture.Clone();
ImageUtilities.EdgeDetectHorizontal(modifiedPiecePicture);
ImageUtilities.EdgeDetectVertical(modifiedPiecePicture);
piecePicture = ImageUtilities.AlphaBlendMatrix(modifiedPiecePicture, piecePicture, 200);

public static bool EdgeDetectHorizontal(Bitmap b)
{
Bitmap bmTemp = (Bitmap)b.Clone();

// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmData2 = bmTemp.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
System.IntPtr Scan02 = bmData2.Scan0;

unsafe
{
byte* p = (byte*)(void*)Scan0;
byte* p2 = (byte*)(void*)Scan02;

int nOffset = stride - b.Width * 3;
int nWidth = b.Width * 3;

int nPixel = 0;

p += stride;
p2 += stride;

for (int y = 1; y < b.Height - 1; ++y)
{
p += 9;
p2 += 9;

for (int x = 9; x < nWidth - 9; ++x)
{
nPixel = ((p2 + stride - 9)[0] +
(p2 + stride - 6)[0] +
(p2 + stride - 3)[0] +
(p2 + stride)[0] +
(p2 + stride + 3)[0] +
(p2 + stride + 6)[0] +
(p2 + stride + 9)[0] -
(p2 - stride - 9)[0] -
(p2 - stride - 6)[0] -
(p2 - stride - 3)[0] -
(p2 - stride)[0] -
(p2 - stride + 3)[0] -
(p2 - stride + 6)[0] -
(p2 - stride + 9)[0]);

if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;

(p + stride)[0] = (byte)nPixel;

++p;
++p2;
}

p += 9 + nOffset;
p2 += 9 + nOffset;
}
}

b.UnlockBits(bmData);
bmTemp.UnlockBits(bmData2);

return true;
}

public static bool EdgeDetectVertical(Bitmap b)
{
Bitmap bmTemp = (Bitmap)b.Clone();

// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmData2 = bmTemp.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
System.IntPtr Scan02 = bmData2.Scan0;

unsafe
{
byte* p = (byte*)(void*)Scan0;
byte* p2 = (byte*)(void*)Scan02;

int nOffset = stride - b.Width * 3;
int nWidth = b.Width * 3;

int nPixel = 0;

int nStride2 = stride * 2;
int nStride3 = stride * 3;

p += nStride3;
p2 += nStride3;

for (int y = 3; y < b.Height - 3; ++y)
{
p += 3;
p2 += 3;

for (int x = 3; x < nWidth - 3; ++x)
{
nPixel = ((p2 + nStride3 + 3)[0] +
(p2 + nStride2 + 3)[0] +
(p2 + stride + 3)[0] +
(p2 + 3)[0] +
(p2 - stride + 3)[0] +
(p2 - nStride2 + 3)[0] +
(p2 - nStride3 + 3)[0] -
(p2 + nStride3 - 3)[0] -
(p2 + nStride2 - 3)[0] -
(p2 + stride - 3)[0] -
(p2 - 3)[0] -
(p2 - stride - 3)[0] -
(p2 - nStride2 - 3)[0] -
(p2 - nStride3 - 3)[0]);

if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;

p[0] = (byte)nPixel;

++p;
++p2;
}

p += 3 + nOffset;
p2 += 3 + nOffset;
}
}

b.UnlockBits(bmData);
bmTemp.UnlockBits(bmData2);

return true;
}

public static Bitmap AlphaBlendMatrix(Bitmap destImage, Bitmap srcImage, byte alpha)
{
Bitmap alphaBlendedImage = (Bitmap)destImage.Clone();

// for the matrix the range is 0.0 - 1.0
float alphaNorm = (float)alpha / 255.0F;

// just change the alpha
ColorMatrix matrix = new ColorMatrix(new float[][]{
new float[] {1F, 0, 0, 0, 0},
new float[] {0, 1F, 0, 0, 0},
new float[] {0, 0, 1F, 0, 0},
new float[] {0, 0, 0, alphaNorm, 0},
new float[] {0, 0, 0, 0, 1F}});

ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(matrix);

int offsetX = (destImage.Width - srcImage.Width) / 2;
int offsetY = (destImage.Height - srcImage.Height) / 2;

using (Graphics g = Graphics.FromImage(alphaBlendedImage))
{
g.CompositingMode = CompositingMode.SourceOver;
g.CompositingQuality = CompositingQuality.HighQuality;

// Scaled image (stretched)
//g.DrawImage(srcImage,
// new Rectangle(0, 0, destImage.Width, destImage.Height),
// 0,
// 0,
// srcImage.Width,
// srcImage.Height,
// GraphicsUnit.Pixel,
// imageAttributes);

// No scaling
g.DrawImage(srcImage,
new Rectangle(offsetX, offsetY, srcImage.Width, srcImage.Height),
0,
0,
srcImage.Width,
srcImage.Height,
GraphicsUnit.Pixel,
imageAttributes);
}

return alphaBlendedImage;
}

最佳答案

快速查看您的代码会发现典型的图像处理类型行为。我建议查看 RenderScript这是 Android 的高性能计算框架。虽然您仍然需要 Java 端来显示您的位图,但您的边缘检测代码将全部在 RenderScript 中。基本上你会让框架处理并行性 - http://android-developers.blogspot.com/2012/01/levels-in-renderscript.html

如果您在旧设备上需要它 - 它可以在 Android Support Library 中找到.

观看有关以下主题的 Google IO 视频可能会有所帮助:https://youtu.be/uzBw6AWCBpU

关于java - 在 Java/android Canvas 上创建具有深度/3D 外观的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com