我通过BufferedImage对象把JPG图象数据读入内存,并用自己定义的ImageAPI.convert(bufimg)方法把图象数据存放在一维数组byte[] pic里,然后我用自己定义的ImageAPI.convert(bufimg)再对该一维数组进行处理得到处理好的图象数据byte[] data。现在,我不知道如何把data用JPEG算法压缩一下,变成和原来直接从JPG图象里取出来的一样。请各位达人帮忙啊!
代码如下:
BufferedImage bufimg=null;
try{
bufimg=ImageIO.read(new File(imageFile));
}catch(IOException e){System.out.println(e.getMessage());}
byte[] pic=ImageAPI.convert(bufimg); ///转换为一维数组
byte[] data=ExpandAPI.transform(pic);///处理图象,得到byte[] data希望把data用JPEG算法处理过后,效果和下面从图象文件中取出来的byte[] file一样
RandomAccessFile raFile;byte[] file=null;
raFile = new RandomAccessFile(imageFile, "r");
raFile.readFully(file, 0, (int) raFile.length());
代码如下:
BufferedImage bufimg=null;
try{
bufimg=ImageIO.read(new File(imageFile));
}catch(IOException e){System.out.println(e.getMessage());}
byte[] pic=ImageAPI.convert(bufimg); ///转换为一维数组
byte[] data=ExpandAPI.transform(pic);///处理图象,得到byte[] data希望把data用JPEG算法处理过后,效果和下面从图象文件中取出来的byte[] file一样
RandomAccessFile raFile;byte[] file=null;
raFile = new RandomAccessFile(imageFile, "r");
raFile.readFully(file, 0, (int) raFile.length());
import java.io.*;
public class ImageDemo {
public ImageDemo() {
}
public static void main(String[] args) {
try{
java.io.RandomAccessFile raFile = new java.io.RandomAccessFile(new File(
"c:\\yan.jpg"), "rw");
System.out.println(raFile.length());
byte [] bytes=new byte[(int)raFile.length()];
raFile.readFully(bytes, 0, (int) raFile.length());
bytes[2222]=1;
FileOutputStream fos=new FileOutputStream(new File("C:\\a.jpg"));
fos.write(bytes);
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}}
经过处理后byte[] data里存放着没有经过压缩的图象数据(好比是.bmp图片里读取的数据一样)。
我要做的是把data数组里的数据用JPEG算法压缩一下,得到压缩后的数据,并保存在byte[]里。但不要保存成文件形式。