`
tianyalinfeng
  • 浏览: 442551 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

图片处理EasyImage

    博客分类:
  • Java
 
阅读更多

import java.awt.Color;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * EasyImage lets you do all the basic image operations - converting, cropping,
 * resizing, rotating, flipping… Plus it let’s you do some really cool affects.
 * All is done super easily. Combining operations can produce some very cool
 * results.
 *
 * Operations: Open image. Save image Convert image Re-size image Crop image
 * Convert to black and white image Rotate image Flip image Add color to image
 * Create image with multiple instance of the original Combining 2 images
 * together Emphasize parts of the image Affine transform image
 *
 *
 * @author Avi Yehuda
 *
 */
public class EasyImage {
 private BufferedImage bufferedImage;
 private String fileName;

 /**
  * Constructor - loads from an image file.
  *
  * @param imageFile
  */
 public EasyImage(File imageFile) {
  try {
   bufferedImage = ImageIO.read(imageFile);
   fileName = imageFile.getAbsolutePath();
  } catch (Exception e) {
   e.printStackTrace();
   bufferedImage = null;
   imageFile = null;
  }
 }

 /**
  * Constructor - loads from an image file.
  *
  * @param imageFilePath
  */
 public EasyImage(String imageFilePath) {
  this(new File(imageFilePath));
 }

 /**
  * Return image as java.awt.image.BufferedImage
  *
  * @return image as java.awt.image.BufferedImage
  */
 public BufferedImage getAsBufferedImage() {
  return bufferedImage;
 }

 /**
  * Save the image as a new image file. Can also convert the image according
  * to file type.
  *
  * @param fileName
  */
 public void saveAs(String fileName) {
  saveImage(new File(fileName));
  this.fileName = fileName;
 }

 /**
  * Saves the image to the original file.
  */
 public void save() {
  saveImage(new File(fileName));
 }

 /**
  * Resizing the image by percentage of the original.
  *
  * @param percentOfOriginal
  */
 public void resize(int percentOfOriginal) {
  int newWidth = bufferedImage.getWidth() * percentOfOriginal / 100;
  int newHeight = bufferedImage.getHeight() * percentOfOriginal / 100;
  resize(newWidth, newHeight);
 }

 /**
  * Resizing the image by width and height.
  *
  * @param newWidth
  * @param newHeight
  */
 public void resize(int newWidth, int newHeight) {

  int oldWidth = bufferedImage.getWidth();
  int oldHeight = bufferedImage.getHeight();

  if (newWidth == -1 || newHeight == -1) {
   if (newWidth == -1) {
    if (newHeight == -1) {
     return;
    }

    newWidth = newHeight * oldWidth / oldHeight;
   } else {
    newHeight = newWidth * oldHeight / oldWidth;
   }
  }

  BufferedImage result = new BufferedImage(newWidth, newHeight,
    BufferedImage.TYPE_INT_BGR);

  double widthSkip = new Double(oldWidth - newWidth)
    / new Double(newWidth);
  double heightSkip = new Double(oldHeight - newHeight)
    / new Double(newHeight);

  double widthCounter = 0;
  double heightCounter = 0;

  int newY = 0;

  boolean isNewImageWidthSmaller = widthSkip > 0;
  boolean isNewImageHeightSmaller = heightSkip > 0;

  for (int y = 0; y < oldHeight && newY < newHeight; y++) {

   if (isNewImageHeightSmaller && heightCounter > 1) { // new image
                // suppose to be
                // smaller -
                // skip row
    heightCounter -= 1;
   } else if (heightCounter < -1) { // new image suppose to be
            // bigger - duplicate row
    heightCounter += 1;

    if (y > 1)
     y = y - 2;
    else
     y = y - 1;
   } else {

    heightCounter += heightSkip;

    int newX = 0;

    for (int x = 0; x < oldWidth && newX < newWidth; x++) {

     if (isNewImageWidthSmaller && widthCounter > 1) { // new
                  // image
                  // suppose
                  // to be
                  // smaller
                  // -
                  // skip
                  // column
      widthCounter -= 1;
     } else if (widthCounter < -1) { // new image suppose to be
             // bigger - duplicate pixel
      widthCounter += 1;

      if (x > 1)
       x = x - 2;
      else
       x = x - 1;
     } else {

      int rgb = bufferedImage.getRGB(x, y);
      result.setRGB(newX, newY, rgb);

      newX++;

      widthCounter += widthSkip;
     }

    }

    newY++;
   }

  }

  bufferedImage = result;
 }

 /**
  * Add color to the RGB of the pixel
  *
  * @param numToAdd
  */
 public void addPixelColor(int numToAdd) {
  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
    int rgb = bufferedImage.getRGB(x, y);
    bufferedImage.setRGB(x, y, rgb + numToAdd);
   }
  }
 }

 /**
  * Covert image to black and white.
  */
 public void convertToBlackAndWhite() {
  ColorSpace gray_space = ColorSpace.getInstance(ColorSpace.CS_GRAY);
  ColorConvertOp convert_to_gray_op = new ColorConvertOp(gray_space, null);
  convert_to_gray_op.filter(bufferedImage, bufferedImage);
 }

 /**
  * Rotates image 90 degrees to the left.
  */
 public void rotateLeft() {

  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  BufferedImage result = new BufferedImage(height, width,
    bufferedImage.TYPE_INT_BGR);

  for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
    int rgb = bufferedImage.getRGB(x, y);
    result.setRGB(y, x, rgb);
   }
  }

  bufferedImage = result;

 }

 /**
  * Rotates image 90 degrees to the right.
  */
 public void rotateRight() {
  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  BufferedImage result = new BufferedImage(height, width,
    bufferedImage.TYPE_INT_BGR);

  for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
    int rgb = bufferedImage.getRGB(x, y);
    result.setRGB(height - y - 1, x, rgb);
   }
  }

  bufferedImage = result;

 }

 /**
  * Rotates image 180 degrees.
  */
 public void rotate180() {
  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  BufferedImage result = new BufferedImage(width, height,
    bufferedImage.TYPE_INT_BGR);

  for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
    int rgb = bufferedImage.getRGB(x, y);
    result.setRGB(width - x - 1, height - y - 1, rgb);
   }
  }

  bufferedImage = result;

 }

 /**
  * Flips the image horizontally
  */
 public void flipHorizontally() {
  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  BufferedImage result = new BufferedImage(width, height,
    bufferedImage.TYPE_INT_BGR);

  for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
    int rgb = bufferedImage.getRGB(x, y);
    result.setRGB(width - x - 1, y, rgb);
   }
  }

  bufferedImage = result;

 }

 /**
  * Flips the image vertically.
  */
 public void flipVertically() {
  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  BufferedImage result = new BufferedImage(width, height,
    bufferedImage.TYPE_INT_BGR);

  for (int x = 0; x < width; x++) {
   for (int y = 0; y < height; y++) {
    int rgb = bufferedImage.getRGB(x, y);
    result.setRGB(x, height - y - 1, rgb);
   }
  }

  bufferedImage = result;

 }

 /**
  * Multiply the image.
  *
  * @param timesToMultiplyVertically
  * @param timesToMultiplyHorizantelly
  */
 public void multiply(int timesToMultiplyVertically,
   int timesToMultiplyHorizantelly) {
  multiply(timesToMultiplyVertically, timesToMultiplyHorizantelly, 0);
 }

 /**
  * Multiply the image and also add color each of the multiplied images.
  *
  * @param timesToMultiplyVertically
  * @param timesToMultiplyHorizantelly
  */
 public void multiply(int timesToMultiplyVertically,
   int timesToMultiplyHorizantelly, int colorToHenhancePerPixel) {
  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  BufferedImage result = new BufferedImage(width
    * timesToMultiplyVertically, height
    * timesToMultiplyHorizantelly, bufferedImage.TYPE_INT_BGR);

  for (int xx = 0; xx < timesToMultiplyVertically; xx++) {
   for (int yy = 0; yy < timesToMultiplyHorizantelly; yy++) {
    for (int x = 0; x < width; x++) {
     for (int y = 0; y < height; y++) {
      int rgb = bufferedImage.getRGB(x, y);
      result.setRGB(width * xx + x, height * yy + y, rgb
        + colorToHenhancePerPixel * (yy + xx));

     }
    }
   }
  }

  bufferedImage = result;
 }

 /**
  * Combines the image with another image in an equal presence to both;
  *
  * @param newImagePath -
  *            image to combine with
  */
 public void combineWithPicture(String newImagePath) {
  combineWithPicture(newImagePath, 2);
 }

 /**
  * Combines the image with another image. jump = 2 means that every two
  * pixels the new image is replaced. This makes the 2 images equal in
  * presence. If jump=3 than every 3rd pixel is replaced by the new image. As
  * the jump is higher this is how much the new image has less presence.
  *
  * @param newImagePath
  * @param jump
  */
 public void combineWithPicture(String newImagePath, int jump) {
  try {
   BufferedImage bufferedImage2 = ImageIO.read(new File(newImagePath));
   combineWithPicture(bufferedImage2, jump, null);
  } catch (Exception e) {
   e.printStackTrace();
   throw new RuntimeException(e);
  }
 }

 public void combineWithPicture(EasyImage image2) {
  combineWithPicture(image2.getAsBufferedImage(), 2, null);
 }

 public void combineWithPicture(EasyImage image2, int jump) {
  combineWithPicture(image2.getAsBufferedImage(), jump, null);
 }

 public void combineWithPicture(EasyImage image2, Color ignoreColor) {
  combineWithPicture(image2.getAsBufferedImage(), 2, ignoreColor);
 }

 public void combineWithPicture(EasyImage image2, int jump, Color ignoreColor) {
  combineWithPicture(image2.getAsBufferedImage(), jump, ignoreColor);
 }

 /**
  * Combines the image with another image. jump = 2 means that every two
  * pixels the new image is replaced. This makes the 2 images equal in
  * presence. If jump=3 than every 3rd pixel is replaced by the new image. As
  * the jump is higher this is how much the new image has less presence.
  *
  * ignoreColor is a color in the new image that will not be copied - this is
  * good where there is a background which you do not want to copy.
  *
  * @param bufferedImage2
  * @param jump
  * @param ignoreColor
  */
 private void combineWithPicture(BufferedImage bufferedImage2, int jump,
   Color ignoreColor) {
  checkJump(jump);

  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  int width2 = bufferedImage2.getWidth();
  int height2 = bufferedImage2.getHeight();

  int ignoreColorRgb = -1;

  if (ignoreColor != null) {
   ignoreColorRgb = ignoreColor.getRGB();
  }

  for (int y = 0; y < height; y++) {
   for (int x = y % jump; x < width; x += jump) {
    if (x >= width2 || y >= height2) {
     continue;
    }

    int rgb = bufferedImage2.getRGB(x, y);

    if (rgb != ignoreColorRgb) {
     bufferedImage.setRGB(x, y, rgb);
    }
   }
  }

 }

 public void crop(int startX, int startY, int endX, int endY) {
  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  if (startX == -1) {
   startX = 0;
  }

  if (startY == -1) {
   startY = 0;
  }

  if (endX == -1) {
   endX = width - 1;
  }

  if (endY == -1) {
   endY = height - 1;
  }

  BufferedImage result = new BufferedImage(endX - startX + 1, endY
    - startY + 1, bufferedImage.TYPE_INT_BGR);

  for (int y = startY; y < endY; y++) {
   for (int x = startX; x < endX; x++) {
    int rgb = bufferedImage.getRGB(x, y);
    result.setRGB(x - startX, y - startY, rgb);
   }
  }
  bufferedImage = result;
 }

 private void saveImage(File file) {
  try {
   ImageIO.write(bufferedImage, getFileType(file), file);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 public void emphasize(int startX, int startY, int endX, int endY) {
  emphasize(startX, startY, endX, endY, Color.BLACK, 3);
 }

 public void emphasize(int startX, int startY, int endX, int endY,
   Color backgroundColor) {
  emphasize(startX, startY, endX, endY, backgroundColor, 3);
 }

 public void emphasize(int startX, int startY, int endX, int endY, int jump) {
  emphasize(startX, startY, endX, endY, Color.BLACK, jump);
 }

 public void emphasize(int startX, int startY, int endX, int endY,
   Color backgroundColor, int jump) {

  checkJump(jump);

  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  if (startX == -1) {
   startX = 0;
  }

  if (startY == -1) {
   startY = 0;
  }

  if (endX == -1) {
   endX = width - 1;
  }

  if (endY == -1) {
   endY = height - 1;
  }

  for (int y = 0; y < height; y++) {
   for (int x = y % jump; x < width; x += jump) {

    if (y >= startY && y <= endY && x >= startX && x <= endX) {
     continue;
    }

    bufferedImage.setRGB(x, y, backgroundColor.getRGB());
   }
  }

 }

 private void checkJump(int jump) {
  if (jump < 1) {
   throw new RuntimeException("Error: jump can not be less than 1");
  }

 }

 public void addColorToImage(Color color, int jump) {
  addColorToImage(color.getRGB(), jump);
 }

 public void addColorToImage(int rgb, int jump) {
  checkJump(jump);

  int width = bufferedImage.getWidth();
  int height = bufferedImage.getHeight();

  for (int y = 0; y < height; y++) {
   for (int x = y % jump; x < width; x += jump) {
    bufferedImage.setRGB(x, y, rgb);
   }
  }
 }

 public void affineTransform(double fShxFactor, double fShyFactor) {

  try {
   AffineTransform shearer = AffineTransform.getShearInstance(
     fShxFactor, fShyFactor);
   AffineTransformOp shear_op = new AffineTransformOp(shearer, null);
   bufferedImage = shear_op.filter(bufferedImage, null);
  } catch (Exception e) {
   System.out.println("Shearing exception = " + e);
  }
 }

 private String getFileType(File file) {
  String fileName = file.getName();
  int idx = fileName.lastIndexOf(".");
  if (idx == -1) {
   throw new RuntimeException("Invalid file name");
  }

  return fileName.substring(idx + 1);
 }

 public int getWidth() {
  return bufferedImage.getWidth();
 }

 public int getHeight() {
  return bufferedImage.getHeight();
 }

 public static void main(String[] args) {
  /*
   * Image image = new Image("c:/pics/p1.jpg"); image.resize(10);
   * image.multiply(5, 5, 11111);
   * image.saveAs("c:/pics/multiply+color.jpg");
   */
  /*
   * Image image = new Image("c:/pics/israel_flag.gif"); Image image2 =
   * new Image("c:/pics/palestine_flag.gif"); //image.resize(50);
   * //image2.resize(50); //image.affineTransform(0, 0.3);
   * //image2.affineTransform(0, -0.3); image.combineWithPicture(image2);
   * image.saveAs("c:/pics/affineTransformAndCombine2.jpg");
   */
  /*
   * Image image = new Image("c:/pics/p1.jpg"); image.resize(50);
   * image.affineTransform(0.0, 0.5);
   * image.saveAs("c:/pics/affineTransform.jpg");
   */
  /*
   * Image image = new Image("c:/pics/heart.gif"); image.multiply(20, 20);
   * Image image2 = new Image("c:/pics/p6.jpg"); image2.crop(800, 0, -1,
   * -1); image2.resize(50);
   * image2.combineWithPicture(image,3,Color.white);
   * image2.saveAs("c:/pics/combineWithPictureWithoutBackground.jpg"); /*
   * image.resize(5); image.multiply(20, 20);
   * image.combineWithPicture("c:/p2.jpg");
   * //image.addColorToImage(Color.yellow, 3);
   * //image.addColorToImage(Color.red, 5);
   * //image.combineWithPicture("c:/p2.jpg",3);
   *
   */

  EasyImage image = new EasyImage("c:/pics/p1.jpg");
  int width = image.getWidth();
  int height = image.getHeight();
  for (int i = 0, c = 0; i < height; c++, i += 50) {
   int x = width / 2 - i;
   int y = height / 2 - i;

   image.emphasize(x, y, width - 1 - x, height - 1 - y, Color.BLACK,
     12 - c / 4);
  }
  image.saveAs("c:/pics/emphesizeTrick.jpg");
  // */
  // image.saveAs("c:/xxx.jpg");
  /*
   * Image image = new Image("c:/pics/p1.jpg");
   * image.addColorToImage(Color.red, 5);
   * image.saveAs("c:/pics/addColorToImage.jpg");
   */
 }
}

分享到:
评论

相关推荐

    EasyImage 简单实用可以大批量图片处理软件

    EasyImage是一款专为用户设计的简单易用的批量图片处理软件。它提供了丰富的功能,能够满足用户在日常工作中对大量图片进行快速编辑和优化的需求。无论是个人还是专业摄影师,甚至是小型企业,都能从这款软件中受益...

    Easy Image Modifier(批量修改图片大小)v4.7.2绿色版

    在实际应用中,用户只需将待处理的图片放入同一目录下,然后通过Easy Image Modifier选择该目录,软件会自动读取并列出所有图片。用户可以设置统一的输出尺寸,如宽度、高度或者按比例缩放,也可以自定义每张图片的...

    简单图床-EasyImage2.0源码 图片网站上传源码源码资源下载整理.zip

    EasyImage2.0很可能支持多种图片格式的上传,如JPEG、PNG、GIF等,并且具备一定的图片处理能力,比如缩放、裁剪等,以便优化存储和展示效果。 【源码资源】下载整理.zip文件可能包含以下组件: 1. **服务器端代码**...

    easy-image-modifier 图片批量处理

    这个工具简化了图片处理的过程,提高了工作效率。 在IT行业中,图片处理是一项常见的任务,无论是在网页设计、移动应用开发还是数据分析中,我们经常需要调整图片的大小、格式、颜色或添加水印等。"easy-image-...

    Laravel开发-easyimage

    `easyimage`是Laravel的一个扩展包,它为开发者提供了一个简单易用的接口来处理这些常见的图像操作。这个扩展库使得在Laravel项目中集成图像处理功能变得更加便捷。 `easyimage`库的主要特性包括: 1. **基本操作*...

    Easy Image Resizer v1.3

    只需选择包含待处理图片的文件夹,设置好目标尺寸和格式,点击开始,程序就会自动完成所有工作。同时,该软件可能还提供了预设模板,如社交媒体的推荐尺寸,让使用者一键调整到适合Instagram、Facebook等平台的规格...

    图片批量大小修改器(Easy Image Modifier) v4.8.rar

    Easy Image Modifier是一个简单的图像处理工具,使您可以从一个简单的使用界调整图像面,支持拖放。您可以通过像素或百分比(按比例或不按比例)所需的图像尺寸,指定一个新的文件格式,也可选择在输入的图像转换为...

    wordpress相册插件easy-image-gallery

    **WordPress相册插件Easy Image Gallery深度解析** 在WordPress网站中,展示图片和创建美观的相册是增强用户体验的关键部分。Easy Image Gallery是一款强大的WordPress相册插件,它旨在简化这个过程,为用户提供一...

    Easy Image Preview jQuery鼠标移上显示大缩略图功能

    "Easy Image Preview jQuery鼠标移上显示大缩略图功能"是一个优秀的解决方案,尤其适用于产品展示、摄影网站或任何需要图片预览的场景。这个插件允许用户在鼠标悬停在小图上时,即时显示一个放大版的预览图,增强了...

    jQuery图片预览插件

    这要求插件具备处理JSON对象的能力,从数据中提取图片URL,并根据需要创建对应的HTML结构。例如,jQuery lightbox可以接收一个包含图片URL和其他信息(如标题、描述)的JSON数组,生成相应的预览窗口。 在实际项目...

    easyImage

    "easyImage"是一个用于图像处理的工具或库,可能主要用于前端开发中,为用户提供便捷的图片操作功能。虽然没有提供详细的标签,但我们可以从名称推测它可能是轻量级的,并且易于使用的。"easyImage-master"这个...

    easy editor images 好的图片处理软件

    今天,我们要介绍的主角就是“Easy Photo Editor”,它是一款易用且功能全面的图片编辑工具,能够满足用户在像素级别的细致调整需求。 Easy Photo Editor的核心特点在于其简洁的用户界面和丰富的编辑功能。初学者...

    jquery图片浏览js插件

    1. **图片元素的选择**:使用jQuery选择器(如`$("#image-container img")`)找到页面中所有的图片元素。 2. **事件绑定**:为这些图片元素绑定`click`事件,当用户点击图片时触发预览模式。 3. **预览窗口创建**...

    EasyImageResize

    它支持直接从Windows资源管理器中拖放图片到程序界面,这一设计极大地简化了图片导入的步骤,让用户无需离开当前的工作环境就能轻松处理图片。这种交互方式对于经常处理大量图片的用户来说,无疑节省了大量的时间,...

    一个很方便从相册或者相机获取图片的库.zip

    5. **异步处理**:考虑到读取大图或处理图片可能耗时,EasyImage可能采用异步处理机制,避免阻塞主线程,保证应用流畅运行。 6. **兼容性**:优秀的开源库通常会考虑不同版本的操作系统和设备兼容性,确保在较旧的...

    图片和拍照选择图片

    总的来说,EasyImage和UCrop是Android开发者在处理图片相关任务时的得力助手,它们为图片选择和剪裁提供了便捷的解决方案,使得开发者可以更专注于应用的核心功能开发,提升整体用户体验。通过合理利用这两个库,你...

    EIGHT: Easy Image Gallery-开源

    **八:Easy Image Gallery 开源图像画廊系统详解** 在数字化时代,图片分享与展示成为了一个重要的需求。开源软件在这一领域提供了丰富的解决方案,其中"八:Easy Image Gallery"(简称EIGHT)就是一个以Perl CGI...

    一个简单有效的实现图片Image Resizing and Cropping 缩放及裁剪控件代码

    在IT领域,图片处理是一项非常常见的任务,尤其是在网页设计、移动应用开发以及图像处理软件中。本篇文章将详细探讨如何实现一个简单而有效的图片缩放和裁剪控制,以满足这些场景的需求。"一个简单有效的实现图片...

    摄影大师都在用的图片无损压缩软件EasyImageModifier.rar

    如果其中还参杂着不需要处理的照片,那么筛选功能可以去除它们,选择“副档名”,就可以排除某类图片格式。你也可以手动移除,添加图片后,你是可以预览他们的,所以移除也非常方便。 有点小瑕疵就是,中文只有繁体...

Global site tag (gtag.js) - Google Analytics