【问题标题】:how to implement brightness at image in android?如何在android中实现图像亮度?
【发布时间】:2014-04-07 14:51:42
【问题描述】:

在我的应用程序中,我有一个搜索栏,用户可以通过滑动它来增加或减少图像的亮度。我已经完成了这项工作,但问题是它的显示速度非常慢,滑动搜索栏后大约需要 3-4 秒才能显示图像效果。下面是我实现的代码,谁能告诉我应该怎么做才能使这种效果在图像上平滑。

public static Bitmap doBrightness(Bitmap src, int value) {
  // image size
  int width = src.getWidth();
  int height = src.getHeight();
  // create output bitmap
  Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
  // color information
  int A, R, G, B;
  int pixel;

  // scan through all pixels
  for (int x = 0; x < width; ++x) {
   for (int y = 0; y < height; ++y) {
    // get pixel color
    pixel = src.getPixel(x, y);
    A = Color.alpha(pixel);
    R = Color.red(pixel);
    G = Color.green(pixel);
    B = Color.blue(pixel);

    // increase/decrease each channel
    R += value;
    if (R > 255) {
     R = 255;
    } else if (R < 0) {
     R = 0;
    }

    G += value;
    if (G > 255) {
     G = 255;
    } else if (G < 0) {
     G = 0;
    }

    B += value;
    if (B > 255) {
     B = 255;
    } else if (B < 0) {
     B = 0;
    }

    // apply new pixel color to output bitmap
    bmOut.setPixel(x, y, Color.argb(A, R, G, B));
   }
  }

  // return final image
  return bmOut;
 }

【问题讨论】:

  • 您是否尝试过创建 AsyncTask 以在不同的线程中执行这些计算?我认为您不应该在 UI 线程上执行此操作。
  • 是的,当我执行 AsyncTask 来执行此操作时,我必须等到我的异步任务执行此操作,我只是想让用户在增加或减少亮度时不应该等待

标签: android image effect brightness


【解决方案1】:

您在 Java 代码的单个线程中遍历图像中的每个像素,并使用 Color 方法将颜色分解为其组成部分。这将是在RenderScript 中做某事的好人选。 RS 会将操作卸载到 DSP 或 GPU(如果您的设备支持)或在 CPU 上并行化。有关 RenderScript 的基本用法和背景,请参阅 this talk

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 2012-09-01
    • 2011-06-20
    • 1970-01-01
    相关资源
    最近更新 更多