「java像素矩阵」java像素类

博主:adminadmin 2023-03-18 21:27:06 311

本篇文章给大家谈谈java像素矩阵,以及java像素类对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java如何获得bmp图片的dpi值,记得有个水平分辨率和垂直分辨率

1、读取一个bmp文件,把bmp的所有像素用rgbArray存储起来。

2、然后取其中一个像素点(x0,y0),把它构造成一个Color对象。

3、构造一个类型一样的BufferedImage imgOut,把像素矩阵rgbArray写到BufferedImage。

4、把imgOut写入文件

这个Color对象有getRed,getBlue,getBlack方法,可以分别获取这个像素在三个颜色分量上的灰度值。

如何使用java打开图像,然后以对图像的像素矩阵进行处理,然后以相同的质量因子保存为新图像

/** A demonstration of various image processing filters */

public class ImageOps extends JPanel {

static final int WIDTH = 600, HEIGHT = 675; // Size of our example

public String getName() {

return "Image Processing";

}

public int getWidth() {

return WIDTH;

}

public int getHeight() {

return HEIGHT;

}

Image image;

/** This constructor loads the image we will manipulate */

public ImageOps() {

image = new javax.swing.ImageIcon("C:\\Sunset.jpg").getImage();

}

// These arrays of bytes are used by the LookupImageOp image filters below

static byte[] brightenTable = new byte[256];

static byte[] thresholdTable = new byte[256];

static { // Initialize the arrays

for (int i = 0; i 256; i++) {

brightenTable[i] = (byte) (Math.sqrt(i / 255.0) * 255);

thresholdTable[i] = (byte) ((i 225) ? 0 : i);

}

}

// This AffineTransform is used by one of the image filters below

static AffineTransform mirrorTransform;

static { // Create and initialize the AffineTransform

mirrorTransform = AffineTransform.getTranslateInstance(127, 0);

mirrorTransform.scale(-1.0, 1.0); // flip horizontally

}

// These are the labels we'll display for each of the filtered images

static String[] filterNames = new String[]{"Original", "Gray Scale",

"Negative", "Brighten (linear)", "Brighten (sqrt)", "Threshold",

"Blur", "Sharpen", "Edge Detect", "Mirror", "Rotate (center)",

"Rotate (lower left)"};

// The following BufferedImageOp image filter objects perform

// different types of image processing operations.

static BufferedImageOp[] filters = new BufferedImageOp[]{

// 1) No filter here. We'll display the original image

null,

// 2) Convert to Grayscale color space

new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null),

// 3) Image negative. Multiply each color value by -1.0 and add 255

new RescaleOp(-1.0f, 255f, null),

// 4) Brighten using a linear formula that increases all color

// values

new RescaleOp(1.25f, 0, null),

// 5) Brighten using the lookup table defined above

new LookupOp(new ByteLookupTable(0, brightenTable), null),

// 6) Threshold using the lookup table defined above

new LookupOp(new ByteLookupTable(0, thresholdTable), null),

// 7) Blur by "convolving" the image with a matrix

new ConvolveOp(new Kernel(3, 3, new float[]{.1111f, .1111f,

.1111f, .1111f, .1111f, .1111f, .1111f, .1111f, .1111f,})),

// 8) Sharpen by using a different matrix

new ConvolveOp(new Kernel(3, 3, new float[]{0.0f, -0.75f, 0.0f,

-0.75f, 4.0f, -0.75f, 0.0f, -0.75f, 0.0f})),

// 9) Edge detect using yet another matrix

new ConvolveOp(new Kernel(3, 3, new float[]{0.0f, -0.75f, 0.0f,

-0.75f, 3.0f, -0.75f, 0.0f, -0.75f, 0.0f})),

// 10) Compute a mirror image using the transform defined above

new AffineTransformOp(mirrorTransform,

AffineTransformOp.TYPE_BILINEAR),

// 11) Rotate the image 180 degrees about its center point

new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,

64, 95), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),

// 12) Rotate the image 15 degrees about the bottom left

new AffineTransformOp(AffineTransform.getRotateInstance(

Math.PI / 12, 0, 190),

AffineTransformOp.TYPE_NEAREST_NEIGHBOR),};

/** Draw the example */

public void paint(Graphics g1) {

Graphics2D g = (Graphics2D) g1;

// Create a BufferedImage big enough to hold the Image loaded

// in the constructor. Then copy that image into the new

// BufferedImage object so that we can process it.

BufferedImage bimage = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_RGB);

Graphics2D ig = bimage.createGraphics();

ig.drawImage(image, 0, 0, this); // copy the image

// Set some default graphics attributes

g.setFont(new Font("SansSerif", Font.BOLD, 12)); // 12pt bold text

g.setColor(Color.green); // Draw in green

g.translate(10, 10); // Set some margins

// Loop through the filters

for (int i = 0; i filters.length; i++) {

// If the filter is null, draw the original image, otherwise,

// draw the image as processed by the filter

if (filters[i] == null) {

g.drawImage(bimage, 0, 0, this);

} else {

g.drawImage(filters[i].filter(bimage, null), 0, 0, this);

}

g.drawString(filterNames[i], 0, 205); // Label the image

g.translate(137, 0); // Move over

if (i % 4 == 3) {

g.translate(-137 * 4, 215); // Move down after 4

}

}

}

public static void main(String[] a) {

JFrame f = new JFrame();

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

f.setContentPane(new ImageOps());

f.pack();

f.setVisible(true);

}

}

更多例子请到java2s.com

Java中如何将图像读为矩阵形式,在如何返回图像形式

首先要先将图像解压缩为位图形式,提取位图中的数据之后就是矩阵形式,然后程序对需要处理的像素点进行处理,完毕之后跟据处理后的数据生成一个新位图,最后再将这个新位图保存为你需要的格式。大体思路就是这样,你可以借助一些开源的开发包来做解压缩和压缩这两步,自己的程序只关注数据处理即可。

关于java像素矩阵和java像素类的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。