java bmp 变色_java怎么实现将 bmp图片黑底白字转换为白底黑字?将白色设置为透明色,谢谢...

该博客介绍了如何使用Java处理BMP图像,将黑底白字的图片转换为白底黑字,并将白色设置为透明色。通过读取图像像素,反转颜色,再设置透明度,实现图像转换效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

展开全部

代码如下:package com.baidu.demo019;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class App extends JFrame {

private static final long serialVersionUID = 1L;

public App() {

32313133353236313431303231363533e58685e5aeb931333365663435

this.setSize(500, 500);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Box box = Box.createVerticalBox();

this.add(box);

// 源图像路径

String imageFile = "images/demo019.bmp";

// 源图像

BufferedImage image1 = getImage(imageFile);

JLabel label1 = new JLabel(new ImageIcon(image1));

JPanel panel1 = new JPanel(new BorderLayout());

panel1.add(label1);

box.add(panel1);

// 转换后的图像

Image image2 = translateImage(image1);

JLabel label2 = new JLabel(new ImageIcon(image2));

JPanel panel2 = new JPanel(new BorderLayout());

panel2.add(label2);

box.add(panel2);

}

BufferedImage getImage(String imageFile) {

BufferedImage image = null;

try {

image = ImageIO.read(new File(imageFile));

} catch (IOException e) {

e.printStackTrace();

}

return image;

}

// 转换图像 黑底白字转换为白底黑字,白色设置为透明色

private Image translateImage(BufferedImage image) {

int width = image.getWidth();

int height = image.getHeight();

BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

for (int i = 0; i 

for (int j = 0; j 

int val = image.getRGB(i, j);

int red = (val >> 16) & 0xff;

int green = (val >> 8) & 0xff;

int blue = val  & 0xff;

red = 255 - red;

green = 255 - green;

blue = 255 - blue;

int alpha = 0xff;

if ((red + green + blue) / 3 >= 0xff) {

alpha = 0x00;

}

int pixel = (alpha <

target.setRGB(i, j, pixel);

}

}

return target;

}

public static void main(String[] args) {

new App().setVisible(true);

}

}

运行结果:

c515ad494789d043b3ecc7fd9d5cdaff.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值