導航:首頁 > 編程語言 > java獲取圖片像素

java獲取圖片像素

發布時間:2025-04-15 21:22:08

1. 有關java的awt或者swing實現在圖片上添加文字的處理(像素和字元串長度單位轉換的問題)

先用java.awt.Graphics2D.getFont()取得當前正在使用的字體f,java.awt.Graphics2D.getFontRenderContext()取得當前正在使用的渲染上下文frc
然後再用Rectangle2D rect=f.getStringBounds(str,frc)就可以得到你渲染這些文字所佔據的矩形,使用rect.getWidth(), rect,getHeight()就可以知道這個矩形的大小了。
反過來通過像素大小決定字元串的長度沒有現成的方法。如果你的字體是等寬的可以先用上面的方法求出一個字元的大小然後自己算,不然就只能先試試一個字元,再試試兩個字元……直到超過你要的長度就返回的辦法了(看起來效率比較低,不過實際使用的時候不是很影響性能)

2. Java如何讀取BMP的每個像素點,輸出到一個二維數組

樓上的基本正確,
問題一:
int[] rgb = new int[3];最好用二維數組
int[] rgb = new int[3][width*height]
問題二:
rgb[0] = (pixel & 0xff0000 ) >> 16 ;
rgb[1] = (pixel & 0xff00 ) >> 8 ;
rgb[2] = (pixel & 0xff );
會把數組內的值覆蓋,獲得就是最後像素點的RGB值;

我寫了一個希望可以幫助到你

package imageReadAndWrite;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
* JPG File reader/writer. Uses native com.sun libraries (which may deprecate at
* any time)
*
*
* @author PhoenixZJG
* @version 1.0
*/
public class JPGFile implements xxxFile {

private int[] ints = null;
private byte bytes[] = null; // bytes which make up binary PPM image
private double doubles[] = null;
private int[][] imageRGB = null;
private String filename = null; // filename for PPM image
private int height = 0;
private int width = 0;

/**
* Read the PPM File.
*
* @throws FileNotFoundException
* if the directory/image specified is wrong
* @throws IOException
* if there are problems reading the file.
*/
public JPGFile(String filename) throws FileNotFoundException, IOException {
this.filename = filename;
readImage();
}

/**
* Get the height of the PPM image.
*
* @return the height of the image.
*/
public int getHeight() {
return height;
}

/**
* Get the width of the PPM image.
*
* @return the width of the image.
*/
public int getWidth() {
return width;
}

/**
* Get the data as byte array. Data is of any type that has been read from
* the file (usually 8bit RGB)
*
* @return The data of the image.
*/
public byte[] getBytes() {
return bytes;
}

/**
* Get the data as double array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public double[] getDouble() {
return doubles;
}

/**
* Get the data as double array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public int[] getInt() {
return ints;
}

/**
* Get the data as integer array. Data is of any type that has been read from
* the file (usually 8bit RGB put into an 64bit double)
*
* @return The data of the image.
*/
public int[][] getImageRGB() {
return imageRGB;
}

/**
* Write to <code>fn</code> file the <code>data</code> using the
* <code>width, height</code> variables. Data is assumed to be 8bit RGB.
*
* @throws FileNotFoundException
* if the directory/image specified is wrong
* @throws IOException
* if there are problems reading the file.
*/
public static void writeImage(String fn, int[] data, int width, int height)
throws FileNotFoundException, IOException {

FileOutputStream fOut = new FileOutputStream(fn);
JPEGImageEncoder jpeg_encode = JPEGCodec.createJPEGEncoder(fOut);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, data, 0, width);
jpeg_encode.encode(image);
fOut.close();
}

/**
* Read the image from the specified file.
*
* @throws FileNotFoundException
* pretty obvious
* @throws IOException
* filesystem related problems
*/
private void readImage() throws FileNotFoundException, IOException {

FileInputStream fIn = new FileInputStream(filename);
JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);
BufferedImage image = jpeg_decode.decodeAsBufferedImage();

width = image.getWidth();
height = image.getHeight();

int[] rgbdata = new int[width * height];

image.getRGB(0, 0, width, height, rgbdata, 0, width);

ints = rgbdata;
bytes = new byte[rgbdata.length];
doubles = new double[rgbdata.length];
imageRGB = new int[3][rgbdata.length];

for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (rgbdata[i] & 0xFF);
doubles[i] = (double) (rgbdata[i]);
imageRGB[0][i] = (rgbdata[i] & 16711680) >> 16;
imageRGB[1][i] = (rgbdata[i] & 65280) >> 8;
imageRGB[2][i] = (rgbdata[i] & 255);
}
}
}

上述代碼可以復制,粘貼使用,有方法的注視,getImageRGB() 就可以獲得所有像素的RGB值,你就可以在其他方法里處理這個二維數組,得到你想要的平均值了,處理後的值,記得把值逆運算,再轉換到Int[]里,就可以用,writeImage()輸出圖像了。

閱讀全文

與java獲取圖片像素相關的資料

熱點內容
橡膠壓縮永久變形國標 瀏覽:847
dns伺服器地址是一樣的嗎 瀏覽:155
手游單機app什麼兔的軟體 瀏覽:393
蘋果耳機在安卓怎麼查看電量 瀏覽:146
手機usb連接到電腦哪個文件夾 瀏覽:811
有什麼免費聽的電台app 瀏覽:867
電視安裝軟體用什麼app 瀏覽:315
黑底白字pdf 瀏覽:258
pythonantigravity 瀏覽:345
模糊演算法的例題 瀏覽:948
程序員是怎麼下載電影的 瀏覽:481
python函數參數大致分為 瀏覽:467
怎麼看生存伺服器種子 瀏覽:144
roblox伺服器崩潰什麼時候好 瀏覽:37
怎麼把抖音設置成白色安卓 瀏覽:338
源碼無憂app 瀏覽:969
k線圖pdf下載 瀏覽:776
加密貨幣推廣工作 瀏覽:117
什麼app聊天無痕跡 瀏覽:844
dism壓縮 瀏覽:620