import com.sun.image.codec.jpeg.JPEGCodec不通过 找不到包

在Eclipse中处理图片,需要引入两个包:
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
报错:
Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:\ Java \jre1.6.0_07\lib\rt.jar


此时解决办法:
Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package Cls; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class MyVerifyCode extends HttpServlet { private static MyVerifyCode instance; private final String ATTRIBUTE_NAME = "verifycode"; // ��֤���ַ������� private final int CODE_LENGTH =4; // ��֤��߶����� private static final int Height =30; // ������֤���Ƿ���Ҫ��תЧ�� private boolean ROTATE_FLAG = true; //���������Ƿ���Ҫ��� private boolean FONT_FLAG=false; //�����������Ĭ������ private String font=""; //��������Ĵ�С,Ĭ��Ϊ15 private int fontsize=15; private final String RAND_RANGE = "abcdefhgkmnpqrstuvwxyz" + "ABCDEFGHGKLMNPQRSTUVWXYZ" + "2345678"; // private Random rand=new Random(); public static Random rand = new Random(); private final char[] CHARS = RAND_RANGE.toCharArray(); // ���캯�� public MyVerifyCode() { } public static MyVerifyCode getInstance() { if (instance == null) instance = new MyVerifyCode(); return instance; } // �������ַ� private String getRandString() { StringBuffer vcode = new StringBuffer(); for (int i = 0; i < CODE_LENGTH; i++) vcode.append(CHARS[rand.nextInt(CHARS.length)]); return vcode.toString(); } // ��������� public static float getRandomJiao() { float jiao = 0.0f; if (rand.nextFloat()<=0.5) { jiao = rand.nextFloat()-0.3f; } else { jiao = -rand.nextFloat()+0.3f; } return jiao; } // ��������ɫ private Color getRandColor(int ll, int ul) { if (ll > 255) ll = 255; if (ll < 1) ll = 1; if (ul > 255) ul = 255; if (ul < 1) ul = 1; if (ul == ll) ul = ll + 1; int r = rand.nextInt(ul - ll) + ll; int g = rand.nextInt(ul - ll) + ll; int b = rand.nextInt(ul - ll) + ll; Color color = new Color(r, g, b); return color; } private BufferedImage getImage(HttpServletRequest request) { //根据设置的高度计算事宜的fontsize int FontSize=Height*75/96; //根据设置的字体及字的个数计算出适宜的宽度 int width = CODE_LENGTH * FontSize*4/5 + 10; //根据字体大小设置X轴的位移量 int X_=FontSize/2; BufferedImage image = new BufferedImage(width,Height, BufferedImage.TYPE_INT_RGB); // ��ȡͼ�������� Graphics graphics = image.getGraphics(); Graphics2D g2 = (Graphics2D) graphics; g2.setColor(getRandColor(100, 255)); g2.setColor(Color.WHITE); g2.fillRect(0, 0, width, Height); g2.translate(0, Height*2/3); double oldrot = 0; String vcode=this.getRandString(); // 把验证码存入session request.getSession(true).setAttribute("codes", vcode); for (int i = 0; i < CODE_LENGTH; i++) { g2.setFont(new Font("Times New Roman", Font.HANGING_BASELINE, FontSize)); double rot = getRandomJiao(); // 旋转画笔 if (ROTATE_FLAG) { g2.rotate(-oldrot); g2.translate(X_+rand.nextInt(8), 0); oldrot = rot; g2.rotate(rot); } String temp = vcode.substring(i, i + 1); g2.setColor(getRandColor(1, 100)); g2.drawString(temp, 0, 0); } // ͼ����Ч g2.dispose(); return image; } public void printImage(HttpServletRequest request, HttpServletResponse response) { // ��ContentType��Ϊ"image/jpeg"���������ʶ��ͼ���ʽ�� response.setContentType("image/jpeg"); // ����ҳ�治���� response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 2000); // ��������֤�� String verifyCode = this.getRandString(); // �����֤���ͼ����� BufferedImage bi = this.getImage(request); try { // ���Servlet����� ServletOutputStream outStream = response.getOutputStream(); // ������������ͼ����ݱ���ΪJPEG������ı����� JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream); // ��ͼ����ݽ��б��� encoder.encode(bi); // ǿ�н���������������뵽ҳ�� outStream.flush(); // �ر������ outStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } public void doGet(HttpServletRequest request, HttpServletResponse response) { printImage(request, response); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值