Android 位图对比实现
Android Bitmap Contrast Implementation
我想创建一个应用程序,将对比度应用于图像,然后在 ImageView 中显示该图像。我找到了 this 示例代码,但它似乎无法正常工作。应用对比度后,它只会让一切都变成绿色。这是我的程序中的内容:
public class ImageImprovementActivity extends ActionBarActivity {
private ImageView imageView;
private Button buttonLoad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_improvement);
buttonLoad = (Button) findViewById(R.id.buttonLoad);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(getImage());
buttonLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageBitmap(applyContrast(((BitmapDrawable)imageView.getDrawable()).getBitmap(), 0.3));
}
});
}
private Bitmap getImage() {
final File imgFile = new File(Environment.getExternalStorageDirectory() + "/testImage2.jpg" );
if (imgFile.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
return bmp;
}
return null;
}
private Bitmap applyContrast(Bitmap image, double contrastVal) {
final int width = image.getWidth();
final int height = image.getHeight();
final Bitmap contrastedImage = Bitmap.createBitmap(width, height, image.getConfig());
int A, R, G, B;
int pixel;
double contrast = Math.pow((100 + contrastVal) / 100, 2);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixel = image.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
R = truncate(R);
G = Color.green(pixel);
G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
G = truncate(R);
B = Color.blue(pixel);
B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
B = truncate(B);
Log.i("ImageImprove", A + " " + R + " " + " " + G + " " + B);
contrastedImage.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return contrastedImage;
}
private int truncate(int value) {
if (value < 0) {
return 0;
} else if (value > 255) {
return 255;
}
return value;
}
}
您知道问题出在哪里吗?另外,如果你有另一个例子,请post它,它可能会有用。
编辑
此外,无论我在 applyContrast(Bitmap image, double contrastVal)
中为 contrastVal
赋予什么值,结果似乎每次都是相同的
编辑2
抱歉,实际上更改 contrastVal
时存在明显差异。图像仍然是绿色的..
编辑3
我正在添加一些图片,以便您更清楚地了解问题所在。
这是原图:
这是在使用 contrastVal 1 应用对比之后:
您的代码有一点错误(复制错误?)。变化
G = truncate(R);
至
G = truncate(G);
我想创建一个应用程序,将对比度应用于图像,然后在 ImageView 中显示该图像。我找到了 this 示例代码,但它似乎无法正常工作。应用对比度后,它只会让一切都变成绿色。这是我的程序中的内容:
public class ImageImprovementActivity extends ActionBarActivity {
private ImageView imageView;
private Button buttonLoad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_improvement);
buttonLoad = (Button) findViewById(R.id.buttonLoad);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(getImage());
buttonLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageBitmap(applyContrast(((BitmapDrawable)imageView.getDrawable()).getBitmap(), 0.3));
}
});
}
private Bitmap getImage() {
final File imgFile = new File(Environment.getExternalStorageDirectory() + "/testImage2.jpg" );
if (imgFile.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
return bmp;
}
return null;
}
private Bitmap applyContrast(Bitmap image, double contrastVal) {
final int width = image.getWidth();
final int height = image.getHeight();
final Bitmap contrastedImage = Bitmap.createBitmap(width, height, image.getConfig());
int A, R, G, B;
int pixel;
double contrast = Math.pow((100 + contrastVal) / 100, 2);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixel = image.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
R = truncate(R);
G = Color.green(pixel);
G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
G = truncate(R);
B = Color.blue(pixel);
B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
B = truncate(B);
Log.i("ImageImprove", A + " " + R + " " + " " + G + " " + B);
contrastedImage.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return contrastedImage;
}
private int truncate(int value) {
if (value < 0) {
return 0;
} else if (value > 255) {
return 255;
}
return value;
}
}
您知道问题出在哪里吗?另外,如果你有另一个例子,请post它,它可能会有用。
编辑
此外,无论我在 applyContrast(Bitmap image, double contrastVal)
contrastVal
赋予什么值,结果似乎每次都是相同的
编辑2
抱歉,实际上更改 contrastVal
时存在明显差异。图像仍然是绿色的..
编辑3 我正在添加一些图片,以便您更清楚地了解问题所在。
这是原图:
这是在使用 contrastVal 1 应用对比之后:
您的代码有一点错误(复制错误?)。变化
G = truncate(R);
至
G = truncate(G);