java 中 bufferedimge 如何实现无损生成 jpg文件

[code="java"]
public static boolean cutImage(Image srcImage, MyRectangle rect, String destPath) {
int x = rect.x;
int y = rect.y;
int w = rect.width;
int h = rect.height;
if (h < 1 || w < 1) {
return true;
}
int [] pixels = ((BufferedImage)srcImage).getRGB(x, y, w, h, null, 0, w);
DataBuffer dataBuffer = new DataBufferInt(pixels, w*h);
WritableRaster raster = Raster.createPackedRaster (dataBuffer, w, h,w, new int [] { 0xFF0000, 0xFF00, 0xFF }, null );
DirectColorModel directColorModel = new DirectColorModel(24, 0xFF0000, 0xFF00, 0xFF);

    BufferedImage image = new BufferedImage(directColorModel, raster, true , null );
    try {
        ImageWriter writer = null;

// float quality = CommonGlobalUtil.quality;
//这里将适量设置为1.0 一样会有失真
float quality = 1.0f;
String formatName = "JPEG";
if(rect.isIsCustomSet()){
quality = rect.getQuatity();
formatName = rect.getFormat();
}
Iterator iter = ImageIO.getImageWritersByFormatName(formatName);
if (iter.hasNext()) {
writer = (ImageWriter) iter.next();
}
if (writer == null) {
logger.error("writer=null");
return false;
}

        IIOImage iioImage = new IIOImage(image, null, null);
        ImageWriteParam param = writer.getDefaultWriteParam();
        if(formatName.equalsIgnoreCase("JPEG")) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionQuality(quality);
        }
        File file = new File(destPath);
        if (!file.getParentFile().exists()) {
            boolean b = file.getParentFile().mkdirs();
        }

        ImageOutputStream outputStream = ImageIO.createImageOutputStream(file);
        writer.setOutput(outputStream);
        writer.write(null, iioImage, param);
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

[/code]

3个回答

[quote]Not a Sun raster file. [/quote] 你下的包还有其他dll文件吗,试试放到system32目录下再运行.

[code="java"]package com.cayden.video01;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

class ImagePanel extends Panel {

private static final long serialVersionUID = 1L;

public Image myimg = null;

public ImagePanel() {

    setLayout(null);

    setSize(240, 180);

}

public void setImage(Image img) {

    this.myimg = img;

    repaint();

}

//显示图片

public void paint(Graphics g) {

    if (myimg != null) {

        g.drawImage(myimg, 0, 0, this);

    }

}



//保存图片

public void saveImage(Image image, String path) {

    //图片缓存

    int width = image.getWidth(null);

    int height = image.getHeight(null);

    BufferedImage bi = new BufferedImage(width, height,

            BufferedImage.TYPE_INT_RGB);

    Graphics2D g2 = bi.createGraphics();

    g2.drawImage(image, null, null);

    //写入文件

    try {

        //打开文件

        FileOutputStream fos = new FileOutputStream(path);

        //JPG编码

        JPEGImageEncoder je = JPEGCodec.createJPEGEncoder(fos);

        JPEGEncodeParam jp = je.getDefaultJPEGEncodeParam(bi);

        jp.setQuality(1f, false);

        je.setJPEGEncodeParam(jp);

        je.encode(bi);

        fos.close();

    } catch (Exception e) {

        e.printStackTrace();

    }

}

}[/code]

这个是对图片处理
下面是点击拍照的方法.
[code="java"]/**
* 拍照
* @param evt
*/
private void captureButtonActionPerformed(java.awt.event.ActionEvent evt){
imagePanel=new ImagePanel();
FrameGrabbingControl fgc = (FrameGrabbingControl) playhelper.getPlayer()

      .getControl("javax.media.control.FrameGrabbingControl");

      Buffer buffer = fgc.grabFrame();

      BufferToImage bufferToImage = new BufferToImage(

              (VideoFormat) buffer.getFormat());

      Image image = bufferToImage.createImage(buffer);

      imagePanel.setImage(image);
      String filepath="D:/app/"+UUID.randomUUID().toString()+".jpg";
      imagePanel.saveImage(image, filepath);
      JOptionPane.showMessageDialog(this, 
               "已经保存在"+filepath, "提示", JOptionPane.INFORMATION_MESSAGE);
}[/code]

[quote]Not a Sun raster file.

An unexpected error has been detected by Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d98be55, pid=11744, tid=4864 [/quote] 这个问题有时候也与系统有关,有时候就是因为有些文件没有放到system32下面造成的.我们也经常遇到个问题.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐