我正在寻找以编程方式更改背景图像(PNG)的色调。这怎么能在Android上完成?Android:如何更改图像的色调?
3
A
回答
2
链接的柱具有一些好的想法,但是用于ColorFilter的矩阵数学可以是(a)中复合矫枉过正,和(b)中所得到的颜色引入可察觉的变化。
在此处修改janin给出的解决方案 - https://stackoverflow.com/a/6222023/1303595 - 我在Photoshop's 'Color' blend mode上根据此版本。这似乎避免因PorterDuff.Mode.Multiply图像变暗,而且运作非常良好的色彩着色去饱和/人造黑&白图像不失大的反差。
/*
* Going for perceptual intent, rather than strict hue-only change.
* This variant based on Photoshop's 'Color' blending mode should look
* better for tinting greyscale images and applying an all-over color
* without tweaking the contrast (much)
* Final color = Target.Hue, Target.Saturation, Source.Luma
* Drawback is that the back-and-forth color conversion introduces some
* error each time.
*/
public void changeHue (Bitmap bitmap, int hue, int width, int height) {
if (bitmap == null) { return; }
if ((hue < 0) || (hue > 360)) { return; }
int size = width * height;
int[] all_pixels = new int [size];
int top = 0;
int left = 0;
int offset = 0;
int stride = width;
bitmap.getPixels (all_pixels, offset, stride, top, left, width, height);
int pixel = 0;
int alpha = 0;
float[] hsv = new float[3];
for (int i=0; i < size; i++) {
pixel = all_pixels [i];
alpha = Color.alpha (pixel);
Color.colorToHSV (pixel, hsv);
// You could specify target color including Saturation for
// more precise results
hsv [0] = hue;
hsv [1] = 1.0f;
all_pixels [i] = Color.HSVToColor (alpha, hsv);
}
bitmap.setPixels (all_pixels, offset, stride, top, left, width, height);
}
4
我测试了接受的答案,不幸的是它返回了错误的结果。我发现,从here修改这个代码的正常工作:
// hue-range: [0, 360] -> Default = 0
public static Bitmap hue(Bitmap bitmap, float hue) {
Bitmap newBitmap = bitmap.copy(bitmap.getConfig(), true);
final int width = newBitmap.getWidth();
final int height = newBitmap.getHeight();
float [] hsv = new float[3];
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int pixel = newBitmap.getPixel(x,y);
Color.colorToHSV(pixel,hsv);
hsv[0] = hue;
newBitmap.setPixel(x,y,Color.HSVToColor(Color.alpha(pixel),hsv));
}
}
bitmap.recycle();
bitmap = null;
return newBitmap;
}
+0
此代码有效,但速度很慢。任何想法优化? – 2018-02-25 18:31:34
相关问题
- 1. 如何更改图像中红色的默认色调?
- 2. Xcode更改图像的色调
- 3. Android - 更改位图的像素颜色
- 4. 更改图像的颜色android
- 5. Android在更改图像时更改了像素的颜色
- 6. 我如何更改/调换图像中的颜色?
- 7. 图像悬停如何更改颜色
- 8. 如何将图像的颜色更改为android中的黑白图像
- 9. 如何更改Android上的ImageView图像
- 10. Android如何根据背景图像更改文本颜色
- 11. 如何选择调色板上的颜色来更改图像的一部分?
- 12. 根据滑块图像的色调更改菜单颜色
- 13. 更改ImagePlus图像颜色
- 14. 更改图像颜色
- 15. Java更改图像颜色
- 16. 如何将图像的背景颜色更改为绿色?
- 17. 如何将Radwindow加载图像的颜色更改为蓝色
- 18. 通过调色板着色图像Android
- 19. iOS/Objective-C:更改图像视图的色调
- 20. 使用调色板更改图像按钮颜色
- 21. 连续改变图像的色调
- 22. C#中的图像色调修改#
- 23. 色调的图像不想改变
- 24. 更改图片onclick并更改画布上的图像颜色
- 25. 如何更改Mapbox上rightCalloutAccessory的色调?
- 26. 如何更改XNA中的色调?
- 27. 如何更改可调焦的颜色?
- 28. 如何反复更改ImageView的色调?
- 29. 如何更改Android中的seekbar颜色?
- 30. 如何更改fab:tit_title颜色的FloatingActionButton android
检查这个帖子http://stackoverflow.com/questions/4354939/understanding-the-use-of-colormatrix-and-colormatrixcolorfilter-to-modify -a-draw – JRaymond 2012-04-20 23:07:06