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

java image 处理

阅读更多
package com.c.elmer.util.image;

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抯 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 Image {
    private BufferedImage bufferedImage;
    private String fileName;
   
    /**
     * Constructor - loads from an image file.
     * @param imageFile
     */
    public Image(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 Image(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);
       
        int widthSkip =  oldWidth / newWidth;
        int heightSkip = oldHeight / newHeight;
       
        if(widthSkip == 0) widthSkip=1;
        if(heightSkip == 0) heightSkip=1;
       
        for (int x = 0; x < oldWidth; x += widthSkip) {
            for (int y = 0; y < oldHeight; y += heightSkip) {
                int rgb = bufferedImage.getRGB(x, y);
              
                if(x/widthSkip < newWidth && y/heightSkip < newHeight){
                    result.setRGB(x/widthSkip, y/heightSkip, rgb);
                }
            }
        }
       
       
        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(Image image2){
        combineWithPicture(image2.getAsBufferedImage(), 2, null);
    }
    public void combineWithPicture(Image image2, int jump){
            combineWithPicture(image2.getAsBufferedImage(), jump, null);
    }
   
    public void combineWithPicture(Image image2, Color ignoreColor){
        combineWithPicture(image2.getAsBufferedImage(), 2, ignoreColor);
    }
    public void combineWithPicture(Image 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 image1  = new Image("e:/123.jpg");
        image1.resize(10);
        image1.multiply(5, 5, 11111);
        image1.saveAs("e:/multiply+color.jpg");
       
       
        Image image22  = new Image("e:/123.jpg");
        Image image2  = new Image("e:/345.jpg");
        //image.resize(50);
        //image2.resize(50);
        //image.affineTransform(0, 0.3);
        //image2.affineTransform(0, -0.3);
        image22.combineWithPicture(image2);
        image22.saveAs("e:/affineTransformAndCombine2.jpg");
      
      
        Image image33 = new Image("e:/123.jpg");
        image33.resize(50);
        image33.affineTransform(0.0, 0.5);
        image33.saveAs("e:/affineTransform11.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);
     
      */
       
        Image image  = new Image("e:/123.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("e:/emphesizeTrick.jpg");
       
       


        Image image6  = new Image("e:/123.jpg");

        image6.emphasize(100, 20, 200, 200);

        image6.saveAs("e:/p1Emphesized6.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");
        */
    }
}
分享到:
评论

相关推荐

    Java图像处理类库 Java Image Filters

    Java图像处理类库,如Java Image Filters,是Java平台上的一个重要工具,用于对数字图像进行各种操作和修改。这些类库通常包含了一系列的类和方法,可以实现图像的过滤、调整、裁剪、旋转、合成等多种功能,为开发者...

    java图片处理工具类JAR包 java-image-scalingjar

    java图片处理工具类JAR包 java-image-scalingjar

    基于java的开发源码-图像处理类库 Java Image Filters.zip

    基于java的开发源码-图像处理类库 Java Image Filters.zip 基于java的开发源码-图像处理类库 Java Image Filters.zip 基于java的开发源码-图像处理类库 Java Image Filters.zip 基于java的开发源码-图像处理类库 ...

    Java Image转BufferedImage

    `Image`和`BufferedImage`是Java AWT(Abstract Window Toolkit)和Swing库中的两个重要类,它们分别用于处理图像的不同方面。本篇文章将详细探讨如何将`Image`对象转换为`BufferedImage`,以及这样做的原因和好处。...

    java image 图片处理

    在Java编程语言中,对图像进行处理是一项常见且实用的功能需求。本文将详细介绍如何利用`javax.imageio`包中的类和方法实现从不同来源(如文件、输入流或URL)读取图像,并将其显示在屏幕上。同时,我们还将探讨如何...

    java imageProcess 图像处理程序模板

    Java图像处理程序模板是用于开发图像操作和分析应用的基础框架,它可以帮助开发者快速构建功能丰富的图像处理项目。这个模板可能包含了一系列预定义的类和方法,涵盖了常见的图像处理任务,如图像读取、显示、保存、...

    java image

    - `java.awt.image.renderable.RenderableImage`接口用于处理可渲染图像,适应不同输出设备。 在实际应用中,Java图像处理广泛应用于GUI设计、数据分析、图像识别、游戏开发等领域。例如,开发者可以利用Java图像...

    Java图像处理类库 Java Image Filters.7z

    "Java图像处理类库 Java Image Filters.7z"这个压缩包可能包含了多个用于处理图像的Java库和示例代码,这些库可以帮助开发者快速实现各种图像处理功能。 1. **Java Advanced Imaging (JAI)**: JAI是由Sun ...

    java源码:Java图像处理类库 Java Image Filters.zip

    这个名为"Java Image Filters"的类库提供了丰富的功能,用于在Java环境中对图像进行各种操作和处理。下面将详细介绍这个源码库的核心知识点。 1. **Java图像处理基础**: Java提供了`java.awt.image`和`javax....

    java资源Java图像处理类库JavaImageFilters

    java资源Java图像处理类库 Java Image Filters提取方式是百度网盘分享地址

    JAVA ImageJ 支持图片格式转换,图片放大缩小等,功能非常强大,缩略图高清晰,有实例代码

    ImageJ是一款开源的Java图像处理软件,它在IT领域中被广泛用于科研和教育,尤其在生物医学成像方面有着显著的应用。这个压缩包提供的资源能够帮助开发者利用ImageJ进行图片处理,包括格式转换、图片的放大与缩小,...

    ImageJ java中文开发教程

    ImageJ是一个非常受欢迎的开源图像处理软件,它基于Java语言开发,具有强大的图像处理功能,并且支持通过插件进行二次开发,从而扩展图像处理能力。ImageJ适用于科研、医学影像分析、机器视觉、地理信息系统等多个...

    FFT.rar_java fft_java image f_java 图像_java 图像处理_java图像处理

    本资源“FFT.rar”显然是一个关于Java实现FFT变换以及图像处理的项目,其中包含了必要的代码和可能的示例。下面将详细讨论FFT的基本概念、Java实现以及在图像处理中的应用。 一、快速傅里叶变换(FFT) 1. FFT概述...

    java-image-process.rar_java_java image process

    本项目名为"java-image-process",显然这是一个基于Java的图像处理应用程序,它提供了对图像进行各种操作的能力。通过这个程序,我们可以实现图像的加载、显示、保存,以及一系列如滤镜、色彩调整等高级功能。 在...

    基于Java的图像处理类库 Java Image Filters.zip

    总的来说,"Java Image Filters" 类库为Java开发者提供了一个强大的工具集,简化了图像处理的过程。通过深入研究源代码、文档和示例,开发者可以充分利用这个库来创建具有专业级图像效果的应用程序。无论是在桌面...

    基于java的图像处理类库 Java Image Filters.zip

    "基于Java的图像处理类库 Java Image Filters.zip" 提供了一个工具集,用于在Java环境中对图像进行各种操作。这个类库可能包含了多种滤镜和效果,可以方便开发者快速实现图像的美化、分析或识别等功能。 在这个...

    转换Image数据为byte数组

    在IT领域,尤其是在Java编程中,处理图像数据是常见的需求之一。当涉及到网络传输或存储时,将图像数据转换为字节数组(byte array)成为了一种实用且高效的手段。下面,我们将深入探讨如何在Java中实现图像数据与...

    基于Java的实例源码-图像处理类库 Java Image Filters.zip

    【标题】"基于Java的实例源码-图像处理类库 Java Image Filters.zip" 提供的是一个用Java语言实现的图像处理类库。这个类库包含了各种图像滤镜和处理功能,可以帮助开发者在Java应用程序中轻松地对图像进行操作和...

    java高清处理图片

    ### Java高清处理图片:利用ImageMagick提升图片质量和效率 #### 背景与需求 在实际项目开发中,图片处理是一项常见的需求,包括但不限于图片的压缩、尺寸调整以及添加水印等操作。传统的Java库虽然能够实现这些...

Global site tag (gtag.js) - Google Analytics