请看下面的图片。Flat是输入,输出应该是底部的图片,显示直线和曲线的深度或3D外观。尝试了EmbossMaskFilter,但由于光线问题,它不能给出相同的输出。
我列出了生成所需输出的c#代码。问题是如何在Java / Android中实现类似的功能,特别是在移动设备上需要快速处理画布绘制。
我列出了生成所需输出的c#代码。问题是如何在Java / Android中实现类似的功能,特别是在移动设备上需要快速处理画布绘制。
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;
}