2 privatesoul privatesoul 于 2013.06.06 11:44 提问

import com.sun.image.codec.jpeg.JPEGCodec;Eclipse编译无法通过

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
这两个包在编译期间无法通过,设置了forbidden reference(access rules) 为warning
但是编译还是没有办法通过,完整代码如下:

package com.demo.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

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

public class ImageUtil {
    private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' };
    private static final int SIZE = 4;
    private static final int LINES = 20;
    private static final int WIDTH = 100;
    private static final int HEIGHT = 60;
    private static final int FONT_SIZE = 40;

    public static Map<String, BufferedImage> createImage() {
        StringBuffer sb = new StringBuffer();
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_BGR);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.LIGHT_GRAY);
        graphics.fillRect(0, 0, WIDTH, HEIGHT);
        Random rm = new Random();

        // 随机字符
        for (int i = 1; i <= SIZE; i++) {
            int r = rm.nextInt(chars.length);
            graphics.setColor(getRandomColor());
            graphics.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
            graphics.drawString(chars[r] + "", (i - 1) * WIDTH / SIZE,
                    HEIGHT / 2);
            sb.append(chars[i]);
        }
        // 干扰线
        for (int i = 1; i < LINES; i++) {
            graphics.setColor(getRandomColor());
            graphics.drawLine(rm.nextInt(WIDTH), rm.nextInt(HEIGHT),
                    rm.nextInt(WIDTH), rm.nextInt(HEIGHT));
        }
        Map<String, BufferedImage> map = new HashMap<String, BufferedImage>();
        map.put(sb.toString(), image);
        return map;
    }

    public static Color getRandomColor() {
        Random rm = new Random();
        Color color = new Color(rm.nextInt(256), rm.nextInt(256),
                rm.nextInt(256));
        return color;
    }

    public static InputStream getInputStream(BufferedImage image)
            throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
        encoder.encode(image);
        byte[] imageBts = bos.toByteArray();
        InputStream in = new ByteArrayInputStream(imageBts);
        return in;
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!