web123456

java to achieve the picture reverse color processing code example

Effect Comparison

original figure

process of color reversal

original figure

process of color reversal

code implementation

  1. import ;
  2. import ;
  3. import .*;
  4. public class ImageColor {
  5. /**
  6. * @Description: Anti-color
  7. * @param imgPath Image path
  8. * @param fileUrl Output image path
  9. * @throws
  10. */
  11. public static void inverse(String imgPath, String fileUrl){
  12. try {
  13. FileInputStream fileInputStream = new FileInputStream(imgPath);
  14. BufferedImage image = (fileInputStream);
  15. // Generate character images
  16. int w = ();
  17. int h = ();
  18. BufferedImage imageBuffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);;
  19. // Drawing characters
  20. for (int y = 0; y < h; y++) {
  21. for (int x = 0; x< w; x++) {
  22. int rgb = (x, y);
  23. int R = (rgb & 0xff0000) >> 16;
  24. int G = (rgb & 0x00ff00) >> 8;
  25. int B = rgb & 0x0000ff;
  26. int newPixel=colorToRGB(255-R,255-G,255-B);
  27. (x,y,newPixel);
  28. }
  29. }
  30. (imageBuffer, "png", new File(fileUrl)); // Output image
  31. } catch (Exception e) {
  32. ();
  33. }
  34. }
  35. /**
  36. * @Description: Color to rgb values
  37. * @throws
  38. */
  39. public static int colorToRGB(int red,int green,int blue){
  40. int newPixel=0;
  41. newPixel=newPixel << 8;
  42. newPixel+=red;
  43. newPixel=newPixel << 8;
  44. newPixel+=green;
  45. newPixel=newPixel << 8;
  46. newPixel+=blue;
  47. return newPixel;
  48. }
  49. public static void main(String[] args) throws IOException {
  50. inverse("C:\\Users\\liuya\\Desktop\\","C:\\Users\\liuya\\Desktop\\logo_0.png");
  51. }
  52. }

single class implementation.Modify the codeand the address of the new image to be generated, just run the main method.

Additional knowledge

The three primary colors in computers are the red, green, and blue of light

0xff0000 forRGBSixteen-digit red

0x00ff00 Green for RGB hexadecimal system

0x0000ff is blue in RGB hexadecimal format

Relevant knowledge

.imageis a standard class library for image processing provided by Java, which contains a set of classes and interfaces for representing, manipulating, and processing image data. This class library provides a wealth of functionality for loading, editing, converting, compositing, and other operations on images.

Here's a detailed breakdown of the

  1. Image Representation: Multiple classes are provided to represent image data. One of the most commonly used is the BufferedImage class, which is an Image object with a buffer of image data that allows direct access and modification of the pixel values of an image. Also included are IndexColorModel for representing indexed color models, Raster for representing image data, and ColorSpace for representing color spaces.

  2. Image Loading and Saving: Several classes and methods are provided for loading and saving images. For example, the ImageIO class provides static methods to load an image from a file or input stream and save the image to a file or output stream. With ImageIO, developers can easily read and write to various image formats such as JPEG, PNG, GIF, etc.

  3. Image Editing and Drawing : Provides classes and methods for image editing and drawing.The Graphics class is an abstract base class that provides basic drawing operations such as drawing lines, filling rectangles, drawing text, and so on. With the Graphics class, you can draw and edit on an image, such as drawing geometric shapes, adding text, drawing images, and so on.

  4. Color Model and Adjustment: provides color model related classes and methods for processing color information and adjustment of an image.ColorModel class is an abstract base class representing the color model of an image, which defines operations such as getting and setting pixel values, color space conversion, and so on. With ColorModel, operations such as color space conversion, color adjustment, pixel reading and writing can be performed.

  5. Image Filters and Transformations: Provides a number of classes and interfaces for filter and transformation operations. For example, ConvolveOp class can be applied to the convolution matrix to achieve the blurring of the image, sharpening and other effects. AffineTransform class can be used for image translation, rotation, scaling and other geometric transformation operations. These classes and interfaces can be used to perform a variety of filters and transformation operations on the image to achieve special effects processing and transformation of the image.

  6. Advanced Image Processing: Provides some advanced image processing functions. For example, the RescaleOp class can be used to adjust the brightness and contrast of an image; the LookupOp class can be used to color map an image through a lookup table; the BandCombineOp class can merge different bands of multiple images. These classes can help developers achieve more complex image processing algorithms and effects.

  7. Image encoding and decoding: A number of classes and methods are provided to support image encoding and decoding. For example, ImageWriter and ImageReader are abstract classes that define the basic operations of image encoding and decoding. With these classes, developers can implement custom image encoders and decoders to extend support for different image formats.

  8. Image Scaling and Cropping: A number of classes and methods are provided for scaling and cropping images. For example, the AffineTransformOp class can be used to scale, rotate, and translate an image according to a specified affine transform matrix.The CropImageFilter class can be used to crop a specified region of an image. These classes can help developers to achieve image resizing and extraction of localized regions.

To summarize , is a standard class library for image processing provided by Java , which provides a wealth of features for image loading, editing, conversion, compositing and other operations . Developers can use this class library to read, process and save data in a variety of image formats , and apply filters, adjustments , drawing and other operations to achieve more complex image processing needs . Provides developers with a set of powerful and flexible tools , making image processing in Java applications become more convenient and efficient .