12 个解决方案
#1
ImageIO.write 可以写bmp么?
#2
怎么没人回答? BufferedImage保存成bmp是 bmp的位深度(1,16,24)如何设置?
#3
用ImageIO.write()保存为jpg或者png都行 就是保存为bmp的时候会变成一个空文件 不知道为啥 疑惑中。。。
#4
// 图片的内存映像
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
#5
// 图片的内存映像
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
#6
保存为png、jpg之类的倒是很简单的
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Demo {
public static void main(String[] args) {
int w=400;
BufferedImage im=new BufferedImage(w, w, BufferedImage.TYPE_INT_ARGB);
for(int i=0;i<w;i++){
for(int j=0;j<w;j++){
int m=(int)Math.sqrt((i*j+i*i+j*j))%256;
int rgb=(m<<16)+(m<<8)+(m)+(0xff000000);
//System.out.println(Integer.toHexString(rgb));
im.setRGB(i, j, rgb);
}
}
File f=new File("D:/test.png");
try {
ImageIO.write(im, "png",f);
im.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#7
hehe 就是可惜我就是需要bmp格式啊 这问题整的人头疼啊。,,真正做起来东西才感觉到java某个角度来讲确实有点乏力哈,,只能自己写个东西实现jpeg到bmp啦。。
#8
最奇怪的就是imageIO里说是可以读写bmp的 实际操作的时候也没报错 只不过写出来的是个0kb的空文件 莫非是方法不对。。
#9
BMP24位 写出类
我自己用的
我自己用的
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BMPWriter {
public static void write(BufferedImage img, File out) {
int width = img.getWidth();
int tripleWidth = width * 3;
int height = img.getHeight();
int fullTriWidth = tripleWidth % 4 == 0 ? tripleWidth
: 4 * ((tripleWidth / 4) + 1);
int[] px = new int[width * height];
px = img.getRGB(0, 0, width, height, px, 0, width);
byte[] rgbs = new byte[tripleWidth * height];
int index = 0;
// r, g, b数组
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
int pixel = px[i * width + j];
rgbs[index++] = (byte) pixel;
rgbs[index++] = (byte) (pixel >>> 8);
rgbs[index++] = (byte) (pixel >>> 16);
}
}
// 补齐扫描行长度为4的倍数
byte[] fullrgbs = new byte[fullTriWidth * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < fullTriWidth; j++) {
if (j < tripleWidth) {
fullrgbs[fullTriWidth * i + j] = rgbs[i * tripleWidth + j];
} else {
fullrgbs[fullTriWidth * i + j] = 0;
}
}
}
index = 0;
int fheader = 14;
int infoheader = 40;
int board = 0;
int offset = fheader + infoheader + board;
int length = width * height * 3 + offset;
short frame = 1;
short deep = 24;
int fbl = 3800;
DataOutputStream dos = null;
try {
FileOutputStream fos = new FileOutputStream(out);
dos = new DataOutputStream(fos);
dos.write('B');
dos.write('M');// 1格式头
wInt(dos, length);// 2-3文件大小
wInt(dos, 0);// 4-5保留
wInt(dos, offset);// 6-7偏移量
wInt(dos, infoheader);// 8-9头信息
wInt(dos, width);// 10-11宽
wInt(dos, height);// 12-13高
wShort(dos, frame);// 14 = 1帧数
wShort(dos, deep);// 15 = 24位数
wInt(dos, 0);// 16-17压缩
wInt(dos, 4);// 18-19 size
wInt(dos, fbl);// 20-21水平分辨率
wInt(dos, fbl);// 22-23垂直分辨率
wInt(dos, 0);// 24-25颜色索引 0为所有
wInt(dos, 0);// 26-27重要颜色索引 0为所有
// wInt(0);//28-35
// wInt(0);
// wInt(0);
// wInt(0);//28-35彩色板
dos.write(fullrgbs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void wInt(DataOutputStream dos, int i) throws IOException {
dos.write(i);
dos.write(i >> 8);
dos.write(i >> 16);
dos.write(i >> 24);
}
private static void wShort(DataOutputStream dos, short i)
throws IOException {
dos.write(i);
dos.write(i >> 8);
}
}
#10
BMP24位 写出类
我自己用的
我自己用的
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BMPWriter {
public static void write(BufferedImage img, File out) {
int width = img.getWidth();
int tripleWidth = width * 3;
int height = img.getHeight();
int fullTriWidth = tripleWidth % 4 == 0 ? tripleWidth
: 4 * ((tripleWidth / 4) + 1);
int[] px = new int[width * height];
px = img.getRGB(0, 0, width, height, px, 0, width);
byte[] rgbs = new byte[tripleWidth * height];
int index = 0;
// r, g, b数组
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
int pixel = px[i * width + j];
rgbs[index++] = (byte) pixel;
rgbs[index++] = (byte) (pixel >>> 8);
rgbs[index++] = (byte) (pixel >>> 16);
}
}
// 补齐扫描行长度为4的倍数
byte[] fullrgbs = new byte[fullTriWidth * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < fullTriWidth; j++) {
if (j < tripleWidth) {
fullrgbs[fullTriWidth * i + j] = rgbs[i * tripleWidth + j];
} else {
fullrgbs[fullTriWidth * i + j] = 0;
}
}
}
index = 0;
int fheader = 14;
int infoheader = 40;
int board = 0;
int offset = fheader + infoheader + board;
int length = width * height * 3 + offset;
short frame = 1;
short deep = 24;
int fbl = 3800;
DataOutputStream dos = null;
try {
FileOutputStream fos = new FileOutputStream(out);
dos = new DataOutputStream(fos);
dos.write('B');
dos.write('M');// 1格式头
wInt(dos, length);// 2-3文件大小
wInt(dos, 0);// 4-5保留
wInt(dos, offset);// 6-7偏移量
wInt(dos, infoheader);// 8-9头信息
wInt(dos, width);// 10-11宽
wInt(dos, height);// 12-13高
wShort(dos, frame);// 14 = 1帧数
wShort(dos, deep);// 15 = 24位数
wInt(dos, 0);// 16-17压缩
wInt(dos, 4);// 18-19 size
wInt(dos, fbl);// 20-21水平分辨率
wInt(dos, fbl);// 22-23垂直分辨率
wInt(dos, 0);// 24-25颜色索引 0为所有
wInt(dos, 0);// 26-27重要颜色索引 0为所有
// wInt(0);//28-35
// wInt(0);
// wInt(0);
// wInt(0);//28-35彩色板
dos.write(fullrgbs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void wInt(DataOutputStream dos, int i) throws IOException {
dos.write(i);
dos.write(i >> 8);
dos.write(i >> 16);
dos.write(i >> 24);
}
private static void wShort(DataOutputStream dos, short i)
throws IOException {
dos.write(i);
dos.write(i >> 8);
}
}
#11
呵呵 谢谢(avina2009) bmp位图读写 我也有,我想要的是 jpeg转1位bmp 的东西
#12
jpg转bmp,Toolkit.getImage()读进来,draw在一张bufferedImage 上,再保存成BMP就可以了啊。
#1
ImageIO.write 可以写bmp么?
#2
怎么没人回答? BufferedImage保存成bmp是 bmp的位深度(1,16,24)如何设置?
#3
用ImageIO.write()保存为jpg或者png都行 就是保存为bmp的时候会变成一个空文件 不知道为啥 疑惑中。。。
#4
// 图片的内存映像
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
#5
// 图片的内存映像
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
#6
保存为png、jpg之类的倒是很简单的
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Demo {
public static void main(String[] args) {
int w=400;
BufferedImage im=new BufferedImage(w, w, BufferedImage.TYPE_INT_ARGB);
for(int i=0;i<w;i++){
for(int j=0;j<w;j++){
int m=(int)Math.sqrt((i*j+i*i+j*j))%256;
int rgb=(m<<16)+(m<<8)+(m)+(0xff000000);
//System.out.println(Integer.toHexString(rgb));
im.setRGB(i, j, rgb);
}
}
File f=new File("D:/test.png");
try {
ImageIO.write(im, "png",f);
im.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#7
hehe 就是可惜我就是需要bmp格式啊 这问题整的人头疼啊。,,真正做起来东西才感觉到java某个角度来讲确实有点乏力哈,,只能自己写个东西实现jpeg到bmp啦。。
#8
最奇怪的就是imageIO里说是可以读写bmp的 实际操作的时候也没报错 只不过写出来的是个0kb的空文件 莫非是方法不对。。
#9
BMP24位 写出类
我自己用的
我自己用的
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BMPWriter {
public static void write(BufferedImage img, File out) {
int width = img.getWidth();
int tripleWidth = width * 3;
int height = img.getHeight();
int fullTriWidth = tripleWidth % 4 == 0 ? tripleWidth
: 4 * ((tripleWidth / 4) + 1);
int[] px = new int[width * height];
px = img.getRGB(0, 0, width, height, px, 0, width);
byte[] rgbs = new byte[tripleWidth * height];
int index = 0;
// r, g, b数组
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
int pixel = px[i * width + j];
rgbs[index++] = (byte) pixel;
rgbs[index++] = (byte) (pixel >>> 8);
rgbs[index++] = (byte) (pixel >>> 16);
}
}
// 补齐扫描行长度为4的倍数
byte[] fullrgbs = new byte[fullTriWidth * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < fullTriWidth; j++) {
if (j < tripleWidth) {
fullrgbs[fullTriWidth * i + j] = rgbs[i * tripleWidth + j];
} else {
fullrgbs[fullTriWidth * i + j] = 0;
}
}
}
index = 0;
int fheader = 14;
int infoheader = 40;
int board = 0;
int offset = fheader + infoheader + board;
int length = width * height * 3 + offset;
short frame = 1;
short deep = 24;
int fbl = 3800;
DataOutputStream dos = null;
try {
FileOutputStream fos = new FileOutputStream(out);
dos = new DataOutputStream(fos);
dos.write('B');
dos.write('M');// 1格式头
wInt(dos, length);// 2-3文件大小
wInt(dos, 0);// 4-5保留
wInt(dos, offset);// 6-7偏移量
wInt(dos, infoheader);// 8-9头信息
wInt(dos, width);// 10-11宽
wInt(dos, height);// 12-13高
wShort(dos, frame);// 14 = 1帧数
wShort(dos, deep);// 15 = 24位数
wInt(dos, 0);// 16-17压缩
wInt(dos, 4);// 18-19 size
wInt(dos, fbl);// 20-21水平分辨率
wInt(dos, fbl);// 22-23垂直分辨率
wInt(dos, 0);// 24-25颜色索引 0为所有
wInt(dos, 0);// 26-27重要颜色索引 0为所有
// wInt(0);//28-35
// wInt(0);
// wInt(0);
// wInt(0);//28-35彩色板
dos.write(fullrgbs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void wInt(DataOutputStream dos, int i) throws IOException {
dos.write(i);
dos.write(i >> 8);
dos.write(i >> 16);
dos.write(i >> 24);
}
private static void wShort(DataOutputStream dos, short i)
throws IOException {
dos.write(i);
dos.write(i >> 8);
}
}
#10
BMP24位 写出类
我自己用的
我自己用的
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BMPWriter {
public static void write(BufferedImage img, File out) {
int width = img.getWidth();
int tripleWidth = width * 3;
int height = img.getHeight();
int fullTriWidth = tripleWidth % 4 == 0 ? tripleWidth
: 4 * ((tripleWidth / 4) + 1);
int[] px = new int[width * height];
px = img.getRGB(0, 0, width, height, px, 0, width);
byte[] rgbs = new byte[tripleWidth * height];
int index = 0;
// r, g, b数组
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
int pixel = px[i * width + j];
rgbs[index++] = (byte) pixel;
rgbs[index++] = (byte) (pixel >>> 8);
rgbs[index++] = (byte) (pixel >>> 16);
}
}
// 补齐扫描行长度为4的倍数
byte[] fullrgbs = new byte[fullTriWidth * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < fullTriWidth; j++) {
if (j < tripleWidth) {
fullrgbs[fullTriWidth * i + j] = rgbs[i * tripleWidth + j];
} else {
fullrgbs[fullTriWidth * i + j] = 0;
}
}
}
index = 0;
int fheader = 14;
int infoheader = 40;
int board = 0;
int offset = fheader + infoheader + board;
int length = width * height * 3 + offset;
short frame = 1;
short deep = 24;
int fbl = 3800;
DataOutputStream dos = null;
try {
FileOutputStream fos = new FileOutputStream(out);
dos = new DataOutputStream(fos);
dos.write('B');
dos.write('M');// 1格式头
wInt(dos, length);// 2-3文件大小
wInt(dos, 0);// 4-5保留
wInt(dos, offset);// 6-7偏移量
wInt(dos, infoheader);// 8-9头信息
wInt(dos, width);// 10-11宽
wInt(dos, height);// 12-13高
wShort(dos, frame);// 14 = 1帧数
wShort(dos, deep);// 15 = 24位数
wInt(dos, 0);// 16-17压缩
wInt(dos, 4);// 18-19 size
wInt(dos, fbl);// 20-21水平分辨率
wInt(dos, fbl);// 22-23垂直分辨率
wInt(dos, 0);// 24-25颜色索引 0为所有
wInt(dos, 0);// 26-27重要颜色索引 0为所有
// wInt(0);//28-35
// wInt(0);
// wInt(0);
// wInt(0);//28-35彩色板
dos.write(fullrgbs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void wInt(DataOutputStream dos, int i) throws IOException {
dos.write(i);
dos.write(i >> 8);
dos.write(i >> 16);
dos.write(i >> 24);
}
private static void wShort(DataOutputStream dos, short i)
throws IOException {
dos.write(i);
dos.write(i >> 8);
}
}
#11
呵呵 谢谢(avina2009) bmp位图读写 我也有,我想要的是 jpeg转1位bmp 的东西
#12
jpg转bmp,Toolkit.getImage()读进来,draw在一张bufferedImage 上,再保存成BMP就可以了啊。