gpt4 book ai didi

android - 使用搜索栏增加或减少图像亮度效果非常慢

转载 作者:行者123 更新时间:2023-11-30 00:53:19 24 4
gpt4 key购买 nike

我正在使用带有搜索栏的亮度代码。在 imageview 中显示亮度需要很长时间。

下面我贴出了代码:

BrightActivity.java:

import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class BrightActivity extends Activity {


SeekBar seekbarbrightness;

Bitmap largeIcon;

ImageView ivDisImage;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.bright_activity);



seekbarbrightness = (SeekBar) findViewById(R.id.seekBar1);
ivDisImage = (ImageView)findViewById(R.id.imageView1);

largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.lhota);



seekbarbrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {


@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {

// TODO Auto-generated method stub

Bitmap bitmap=doBrightness(largeIcon, progress);
ivDisImage.setImageBitmap(bitmap);
ivDisImage.setColorFilter(brightIt(100));

}
});


}


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;
}

public static ColorMatrixColorFilter brightIt(int fb) {
ColorMatrix cmB = new ColorMatrix();
cmB.set(new float[] {
1, 0, 0, 0, fb,
0, 1, 0, 0, fb,
0, 0, 1, 0, fb,
0, 0, 0, 1, 0 });

ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.set(cmB);
//Canvas c = new Canvas(b2);
//Paint paint = new Paint();
ColorMatrixColorFilter f = new ColorMatrixColorFilter(colorMatrix);
//paint.setColorFilter(f);
return f;
}



}

bright_activity.java:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Brightness"
android:textAppearance="?android:attr/textAppearanceLarge" />

<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="20dp" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/seekBar1"
android:layout_marginTop="18dp"
android:src="@drawable/lhota" />

</RelativeLayout>

任何人都知道如何使用搜索栏更快地增加和减少图像的亮度。

最佳答案

简单的方法是在 imageView 上放置一个黑色覆盖层,然后使用 seekbar 简单且省时的解决方案映射覆盖层的透明度。

或者你可以只使用下面的代码来增强位图的亮度

/**
*
* @param bmp input bitmap
* @param contrast 0..10 1 is default
* @param brightness -255..255 0 is default
* @return new bitmap
*/
public static Bitmap changeBitmapContrastBrightness(Bitmap bmp, float contrast, float brightness)
{
ColorMatrix cm = new ColorMatrix(new float[]
{
contrast, 0, 0, 0, brightness,
0, contrast, 0, 0, brightness,
0, 0, contrast, 0, brightness,
0, 0, 0, 1, 0
});

Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

Canvas canvas = new Canvas(ret);

Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bmp, 0, 0, paint);

return ret;
}

感谢这个答案change the contrast and brightness of the bitmap

关于android - 使用搜索栏增加或减少图像亮度效果非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40583564/

24 4 0