看看下面的代码。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main( String[] args ) throws IOException {
Image img = ImageIO.read( new File( "image.jpeg" ) );
new JFrame(){
{
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 800, 600 );
setLocationRelativeTo( null );
add( new JPanel(){
@Override
protected void paintComponent( Graphics g ) {
super.paintComponent( g );
int imgWidth = img.getWidth( null );
int imgHeight = img.getHeight( null );
int lines = 4;
int columns = 6;
int count = 1;
for ( int i = 0; i < lines; i++ ) {
for ( int j = 0; j < columns; j++ ) {
g.drawImage( newBrightness( img, 1f/(lines*columns)*count ), imgWidth * j, imgHeight * i, null );
count++;
}
}
}
});
}
}.setVisible( true );
}
public static Image newBrightness( Image source, float brightnessPercentage ) {
BufferedImage bi = new BufferedImage(
source.getWidth( null ),
source.getHeight( null ),
BufferedImage.TYPE_INT_ARGB );
int[] pixel = { 0, 0, 0, 0 };
float[] hsbvals = { 0, 0, 0 };
bi.getGraphics().drawImage( source, 0, 0, null );
// recalculare every pixel, changing the brightness
for ( int i = 0; i < bi.getHeight(); i++ ) {
for ( int j = 0; j < bi.getWidth(); j++ ) {
// get the pixel data
bi.getRaster().getPixel( j, i, pixel );
// converts its data to hsb to change brightness
Color.RGBtoHSB( pixel[0], pixel[1], pixel[2], hsbvals );
// create a new color with the changed brightness
Color c = new Color( Color.HSBtoRGB( hsbvals[0], hsbvals[1], hsbvals[2] * brightnessPercentage ) );
// set the new pixel
bi.getRaster().setPixel( j, i, new int[]{ c.getRed(), c.getGreen(), c.getBlue(), pixel[3] } );
}
}
return bi;
}
}
它将读取图像并创建一个矩阵,其中包含具有新亮度的新图像。这是我的个人资料图片的结果。
编辑:
现在它也支持 alpha。在之前的代码中,新像素的 alpha 分量固定为 255。我改为使用原始像素的 alpha 分量(pixel[3])。
编辑 2:
每个像素都有一个从 0 到 1 变化的亮度分量。如果该分量外推值 1,则该像素将具有“奇怪”的颜色。您想要看起来比原来更亮的东西,因此,您需要验证新的亮度值是否外推为 1。上面的示例将执行此操作。您将有一个滑块来控制将计算到新像素亮度分量的最大百分比。如果此值超过最大值 (1),则将使用最大值。我希望现在它终于可以帮助你了:D
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ChangeImageBrightnessExample2 {
public static void main( String[] args ) throws IOException {
new ChangeImageBrightnessExample2().createUI();
}
public void createUI() throws IOException {
Image img = ImageIO.read( new File( "image.jpeg" ) );
new JFrame(){
{
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 800, 600 );
setLocationRelativeTo( null );
CustomPanel panel = new CustomPanel();
panel.setImage( img );
JSlider slider = new JSlider( 0, 400, 100 );
slider.setMinorTickSpacing( 10);
slider.setMajorTickSpacing( 50 );
slider.setPaintLabels( true );
slider.setPaintTicks( true );
slider.setSnapToTicks( true );
slider.addChangeListener( new ChangeListener() {
@Override
public void stateChanged( ChangeEvent evt ) {
JSlider s = ((JSlider) evt.getSource());
if ( s.getValueIsAdjusting() ) {
panel.setMaximumBrightnessPercentage( s.getValue()/100f );
panel.repaint();
}
}
});
add( panel, BorderLayout.CENTER );
add( slider, BorderLayout.SOUTH );
}
}.setVisible( true );
}
public static Image newBrightness( Image source, float brightnessPercentage ) {
BufferedImage bi = new BufferedImage(
source.getWidth( null ),
source.getHeight( null ),
BufferedImage.TYPE_INT_ARGB );
int[] pixel = { 0, 0, 0, 0 };
float[] hsbvals = { 0, 0, 0 };
bi.getGraphics().drawImage( source, 0, 0, null );
// recalculare every pixel, changing the brightness
for ( int i = 0; i < bi.getHeight(); i++ ) {
for ( int j = 0; j < bi.getWidth(); j++ ) {
// get the pixel data
bi.getRaster().getPixel( j, i, pixel );
// converts its data to hsb to change brightness
Color.RGBtoHSB( pixel[0], pixel[1], pixel[2], hsbvals );
// calculates the brightness component.
float newBrightness = hsbvals[2] * brightnessPercentage;
if ( newBrightness > 1f ) {
newBrightness = 1f;
}
// create a new color with the new brightness
Color c = new Color( Color.HSBtoRGB( hsbvals[0], hsbvals[1], newBrightness ) );
// set the new pixel
bi.getRaster().setPixel( j, i, new int[]{ c.getRed(), c.getGreen(), c.getBlue(), pixel[3] } );
}
}
return bi;
}
private class CustomPanel extends JPanel {
private float maximumBrightnessPercentage = 1f;
private Image image;
@Override
protected void paintComponent( Graphics g ) {
super.paintComponent( g );
int imgWidth = image.getWidth( null );
int imgHeight = image.getHeight( null );
int lines = 4;
int columns = 6;
int count = 1;
for ( int i = 0; i < lines; i++ ) {
for ( int j = 0; j < columns; j++ ) {
float newBrightness = maximumBrightnessPercentage/(lines*columns)*count;
g.drawImage( newBrightness( image, newBrightness ), imgWidth * j, imgHeight * i, null );
g.drawString( String.format( "%.2f%%", newBrightness*100 ), imgWidth * j, imgHeight * i + 10 );
count++;
}
}
}
public void setMaximumBrightnessPercentage( float maximumBrightnessPercentage ) {
this.maximumBrightnessPercentage = maximumBrightnessPercentage;
}
public void setImage( Image image ) {
this.image = image;
}
}
}
请看下面的图片。
我想现在你会明白的。如果没有,我会放弃:D