图片中格局的互转ITeye - AG环亚娱乐集团

图片中格局的互转ITeye

2019-01-11 06:05:12 | 作者: 智晖 | 标签: 图画,格局,输出 | 浏览: 2729

ios = ImageIO.createImageOutputStream(baos); write(ios, null, "jpg", bi, null, 0.75f); } catch (IOException e) throw new AppException("【getImgSize】获取图片大小发作过错:" + e.getMessage()); } finally if (null != ios) ios.close(); if (null != baos) baos.close(); } catch (IOException e) throw new AppException("【getImgSize】封闭流发作过错:" + e.getMessage()); byte[] jpegData = baos.toByteArray(); size = jpegData.length / 1024.00; return size; * 编码输出图画。 * 向图画文件中增加图画缩略图和设置图画紧缩质量需求依据详细图画格局。 * @param out 输出流。 * @param listener 编码输出进展监听器。 * @param formatName 包括格局非正式称号的 String,例如"jpg"。 * @param image 图画。 * @param thumbnails 缩略图集。 * @param quality 紧缩质量。 * @throws java.io.IOException public void write(ImageOutputStream out, IIOWriteProgressListener listener, String formatName, BufferedImage image, List thumbnails, float quality) throws IOException if (out == null) throw new IllegalArgumentException("OutputStream must be non null"); if (formatName == null) throw new IllegalArgumentException("FormatName must be non null"); if (image == null) throw new IllegalArgumentException("Image must be non null"); // 获得适宜的 ImageWriter。 Iterator writers = ImageIO.getImageWritersByFormatName(formatName); if (writers == null || !writers.hasNext()) throw new IllegalStateException("No " + formatName + " writers!"); ImageWriter writer = (ImageWriter) writers.next(); ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(image); IIOMetadata metadata = writer.getDefaultImageMetadata(imageType, null); IIOImage iioImage = new IIOImage(image, thumbnails, metadata); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality); writer.setOutput(out); writer.addIIOWriteProgressListener(listener); writer.write(null, iioImage, param); writer.dispose(); public byte[] sacleImageToBytes(double percent) throws IOException InputStream in = null; BufferedInputStream bis = null; ByteBuffer sb = null; byte[] photo = null; double _width = this.scaleWidth * percent; double _height = this.scaleHeight * percent; double rate1 = this.scaleWidth / _width * 1.0; double rate2 = this.scaleHeight / _height * 1.0; // 依据缩放比率大的进行缩放操控 double rate = rate1 rate2 ? rate1 : rate2; _width = (int)(this.scaleWidth / rate); _height = (int)(this.scaleHeight / rate); try { in = sacleImage(_width, _height, "jpg"); sb = ByteBuffer.allocate(in.available()); int buffersize = 1024; photo = new byte[buffersize]; // 缓存 int bytesRead = 0; // 读取数据暂时变量 bis = new BufferedInputStream(in); while ((bytesRead = bis.read(photo, 0, buffersize)) != -1) { sb.put(photo, 0, bytesRead); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(in != null){ in.close(); if(bis != null){ bis.close(); return sb.array(); public InputStream sacleImage(double _width, double _height, String imgType) throws IOException double widthRatio = 1.0; double heightRatio = 1.0; if (this.getImgWidth() != _width) widthRatio = _width / this.getImgWidth(); if (this.getImgHeight() != _height) heightRatio = _height / this.getImgHeight(); byte[] bytes = null; if(widthRatio==1.0 heightRatio==1.0){ bytes = InputStreamToByte(this.fis); }else{ Image tempImg = bi.getScaledInstance(new Double(_width).intValue(), new Double(_height).intValue(), BufferedImage.SCALE_SMOOTH); ByteArrayOutputStream bos = new ByteArrayOutputStream(); AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(widthRatio, heightRatio), null); tempImg = op.filter(bi, null); ImageIO.write((BufferedImage) tempImg, imgType, bos); bytes = bos.toByteArray(); if (bos != null) bos.close(); return new ByteArrayInputStream(bytes); private byte[] InputStreamToByte(InputStream is) throws IOException { ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) { bytestream.write(ch); byte imgdata[] = bytestream.toByteArray(); bytestream.close(); return imgdata; }

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表AG环亚娱乐集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章