我在单色显示屏上绘制联系人图片,但效果不佳,显然只应用 threshold 抖动(请参阅此处比较:dithering algorithms)。如何实现有序或其他(更好)抖动结果?
这是我使用的代码:
// RGB_565 is most suitable for monochrome display
Bitmap b = Bitmap.createBitmap(desW, desH, Bitmap.Config.RGB_565);
// Set the density to default to avoid scaling.
b.setDensity(DisplayMetrics.DENSITY_DEFAULT);
Canvas c = new Canvas(b);
c.drawBitmap(photo, source, destination, new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG));
我尝试设置不同的位图配置,但不会改变任何内容。
答案 0 :(得分:0)
好的,这里的答案对我有用:How to convert a 24 Bit PNG to 3 Bit PNG using Floyd–Steinberg dithering?
我刚刚将BufferedImage
替换为Bitmap
,使用getRGB(x, y)
调用方法getPixel(x,y)
,并将辅助类实现替换为:
static class C3 {
int r, g, b;
public C3(int c) {
this.r = Color.red(r);
this.g = Color.green(c);
this.b = Color.blue(c);
}
public C3(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
public C3 add(C3 o) {
return new C3(r + o.r, g + o.g, b + o.b);
}
public C3 sub(C3 o) {
return new C3(r - o.r, g - o.g, b - o.b);
}
public C3 mul(double d) {
return new C3((int) (d * r), (int) (d * g), (int) (d * b));
}
public int diff(C3 o) {
return Math.abs(r - o.r) + Math.abs(g - o.g) + Math.abs(b - o.b);
}
public int toRGB() {
return Color.rgb(clamp(r), clamp(g), clamp(b));
}
public int clamp(int c) {
return Math.max(0, Math.min(255, c));
}
}