现在的位置: 首页 > 综合 > 正文

FileChannal文件上传以及对文件像素进行压缩

2014年10月17日 ⁄ 综合 ⁄ 共 5405字 ⁄ 字号 评论关闭

/**
  * 这是文件的上传
  *
  * @param documentRecord
  *            这是源文件
  * @param filesPath
  *            这是存放的路径
  * @param fileName
  *            这是文件名
  */
 public static void uploadDocumentFile(File documentRecord,
   String filesPath, String fileName) {
  FileInputStream is = null;
  FileOutputStream os = null;
  FileChannel fcin = null;
  FileChannel fcout = null;
  try {
   File outDir = new File(filesPath);
   if (!outDir.exists()) {
    outDir.mkdirs();// 穿件文件夹
   }
   File outFile = new File(outDir + "/" + fileName);// 文件生成的地址
   if (!outFile.exists()) {
    outFile.createNewFile();
   }
   is = new FileInputStream(documentRecord);
   os = new FileOutputStream(outFile);
   fcin = is.getChannel();// 这是nio对文件流读取 采取管道方式
   fcout = os.getChannel();
   ByteBuffer buffer = ByteBuffer.allocate(buffere);
   int num = 0;
   while ((num = fcin.read(buffer)) != -1) {
    buffer.flip();
    while (buffer.hasRemaining()) {
     fcout.write(buffer);
    }
    buffer.clear();
   }
   // 进行压缩图片 。。。。。。。。。。。。begin 20130807 第一个方案
   // // 加载原始图像
   // BufferedImage buf = ImageIO.read(outFile);
   // // 原始的大小
   // int w = buf.getWidth();
   // int h = buf.getHeight();
   // // 创建宽高各位原来1/2的图片
   // int hints = Image.SCALE_SMOOTH;
   // Image img = buf.getScaledInstance(w / 2, h / 2, hints);
   // // 将img转换为BufferedImage
   // int type = buf.getType();
   // if (type == BufferedImage.TYPE_BYTE_BINARY)
   // type = BufferedImage.TYPE_BYTE_GRAY;
   // BufferedImage ret = new BufferedImage(w / 2, h / 2, type);
   // ret.createGraphics().drawImage(img, 0, 0, null);
   //
   // // 保存为jpg备用
   // ImageIO.write(ret, "tif", outFile);
   // BufferedImage buf = ImageIO.read(outFile); 第二种加压方案
   // //原始的大小
   // int w = buf.getWidth();
   // int h = buf.getHeight();
   // //创建宽高各位原来1/2的图片
   // int hints = Image.SCALE_SMOOTH;
   // Image img = buf.getScaledInstance(w/2, h/2, hints);
   // //将img转换为BufferedImage
   // int type = buf.getType();
   // if(type==BufferedImage.TYPE_BYTE_BINARY)
   // type = BufferedImage.TYPE_BYTE_GRAY;
   // BufferedImage ret = new BufferedImage(w/2, h/2,type);
   // ret.createGraphics().drawImage(img, 0, 0, null);
   // //将灰度图处理为二值图
   // BufferedImage ret2 = new BufferedImage(w/2, h/2,buf.getType());
   // WritableRaster dr = ret2.getRaster();
   // for(int i=0;i<ret.getWidth();i++){
   // for(int j=0;j<ret.getHeight();j++){
   // int v = ret.getRGB(i, j);
   // Color rgb = new Color(v);
   // if(rgb.getRed()==255)
   // dr.setPixel(i, j, new int[]{255});
   // else
   // dr.setPixel(i, j, new int[]{0});
   // }
   // }
   // //保存为jpg备用,此jpg的像素宽高为原有tif图像的1/2
   // OutputStream out = new FileOutputStream(outFile);//将压缩文件输出。。
   // TIFFEncodeParam param = new TIFFEncodeParam();
   // param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
   // ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out,
   // param);
   // encoder.encode(ret2);
   // out.close();

   // 加载原始图像tif图
   BufferedImage buf = ImageIO.read(outFile);
   if(buf!=null){
    // 原始图片的大小
    int w = buf.getWidth();
    int h = buf.getHeight();
    // 创建宽高各位原来1/3的图片
    BufferedImage ret = new BufferedImage(w / 2, h / 2, buf.getType());
    if (ret != null) {
     WritableRaster dr = ret.getRaster();
     WritableRaster sr = buf.getRaster();
     int[] iArray = new int[4];
     int[] sArray = new int[4];
     for (int i = 1; i < ret.getWidth() - 1; i++) {
      for (int j = 1; j < ret.getHeight() - 1; j++) {
       // 原图片中的中心点
       int x = 2 * i;
       int y = 2 * j;
       sArray[0] = 0;
       // 取周围的9个点,中心点权重为11,其余为2
       sr.getPixel(x - 1, y - 1, iArray);
       sArray[0] += iArray[0] * 2;
       sr.getPixel(x, y - 1, iArray);
       sArray[0] += iArray[0] * 2;
       sr.getPixel(x + 1, y - 1, iArray);
       sArray[0] += iArray[0] * 2;
       sr.getPixel(x - 1, y, iArray);
       sArray[0] += iArray[0] * 2;
       sr.getPixel(x, y, iArray);
       sArray[0] += iArray[0] * 11;
       sr.getPixel(x + 1, y, iArray);
       sArray[0] += iArray[0] * 2;
       sr.getPixel(x - 1, y + 1, iArray);
       sArray[0] += iArray[0] * 2;
       sr.getPixel(x, y + 1, iArray);
       sArray[0] += iArray[0] * 2;
       sr.getPixel(x + 1, y + 1, iArray);
       sArray[0] += iArray[0] * 2;
       // 取像素浓度的均值,大于2认为是黑色
       if (sArray[0] / 9 > 2)
        dr.setPixel(i, j, new int[] { 1 });
       else
        dr.setPixel(i, j, new int[] { 0 });
      }
     }
     // 保存为tif备用,此tif的像素宽高为原有tif图像的1/2,分辨率也为原来的1/2,即150dpi
     OutputStream out = new FileOutputStream(outFile);// 将压缩后的图片覆盖原来的图片
     TIFFEncodeParam param = new TIFFEncodeParam();
     // 使用GROUP4方式压缩
     param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
     // 设置图片的新分辨率
     TIFFField[] extras = new TIFFField[2];
     extras[0] = new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION,
       TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] {
         { (long) 150, 1 }, { 0, 0 } });
     extras[1] = new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION,
       TIFFField.TIFF_RATIONAL, 1, (Object) new long[][] {
         { (long) 150, 1 }, { 0, 0 } });
     param.setExtraFields(extras);
     ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF",
       out, param);

     if (encoder != null) {
      encoder.encode(ret);
      System.out.println("压缩之后的tif文件为:"
        + outFile.getAbsolutePath());
     } else {
      logger
        .warn("FileUtil的uploadDocument()方法的encoder返回的值为null,文件压缩失败");
     }
     out.close();
   }else{
    logger.warn("ImageIo产出的buffer为null");
   }
   

   } else {
    logger
      .warn("FileUtil的uploadDocument()方法的BufferedImage返回的值为null,文件压缩失败");
   }

   // 进行压缩图片 。。。。。。。。。。。。end 20130807
  } catch (FileNotFoundException e) {
   logger.error("找不到对应的文件,异常结果为:", e);
   e.printStackTrace();
  } catch (IOException e) {
   logger.error("上传失败,发生IO异常。。。:", e);
   e.printStackTrace();
  } finally {
   try {
    if (fcout != null) {
     fcout.close();
    }

    if (fcin != null) {
     fcin.close();
    }
    if (os != null) {
     os.close();
    }
    if (is != null) {
     is.close();
    }

   } catch (IOException e) {
    e.printStackTrace();
   }

  }

 }

抱歉!评论已关闭.