这个是一段透明化处理的代码,里面有获取像素的代码存在!希望对你有所帮助!
pixels将是像素,int pixels[] = (int[]) pgrgetPixels();这里将写入到数组中了!
/
使中的某一种颜色透明
@param image
Image
@param RGB16
String 十六进制的六位颜色值字符串
@param isFiltrate
boolean
@return Image
/
public static Image setTranImage(Image image, String RGB16,
boolean isFiltrate)
{
int width = imagegetWidth(null);
int height = imagegetHeight(null);
Image abufferedimage = new BufferedImage(width, height, 2);
Graphics g = abufferedimagegetGraphics();
gdrawImage(image, 0, 0, width, height, 0, 0, width, height, null);
gdispose();
// 透明化处理
PixelGrabber pgr = new PixelGrabber(abufferedimage, 0, 0, -1, -1, true);
try
{
pgrgrabPixels();
}
catch (InterruptedException ex)
{
exgetStackTrace();
}
int pixels[] = (int[]) pgrgetPixels();
if (isFiltrate && RGB16length() == 6)
{
// 循环像素
for (int i = 0; i < pixelslength; i++)
{
// 去色
if (((pixels[i] & 0x00ff0000) >> 16 == IntegerparseInt(
RGB16substring(0, 2), 16)
&& (pixels[i] & 0x0000ff00) >> 8 == IntegerparseInt(
RGB16substring(2, 4), 16) && (pixels[i] & 0x000000ff) == Integer
parseInt(RGB16substring(4, 6), 16)))
{
// 透明化
pixels[i] = 0;
}
}
}
ImageProducer ip = new MemoryImageSource(pgrgetWidth(),
pgrgetHeight(), pixels, 0, pgrgetWidth());
return toolkitcreateImage(ip);
}
rgb三个参数的值为0-255,对应就是00-FF(这个是16进制的),所以可以直接从#FFFFFF得到rgb的值为:int r = 0xff ; int g = 0xff ; int b = 0xff ;(0x<零x>表示16进制晓得的吧)
要看清是RGB、还是BGR、、、、另外java中的byte再转成int时是会使用补数的(即是看到是负的)
int R = (pixel>>16) & 0xff;
int G = (pixel>>8) & 0xff;
int B = (pixel & 0xff);
TYPE_INT_RGB等的值时表示类型的常量,其实就是一个整数。而getType()方法是图像类的方法,获取到图像类型,并返回表示对应类型的那个数。这些都是基于java类库的。其他的程序依照其类库,可能相同,也可能不同。比如你说的TYPE_INT_RGB是1表示Java中,1这个数表示RGB,别的语言可以用2、3这些数或者double,String等其他类型去表示,同样的,geType这个方法是根据语言不通,方法名有所差异的。建议查找对应语言的API。
通过BufferedImage bi = ImageIOread(new File("路径"));读取路径
再通过文件源,以及坐标获取RGB值
public static int[] getRGB(BufferedImage image, int x, int y) {
int[] rgb = null;
if (image != null && x < imagegetWidth() && y < imagegetHeight()) {
rgb = new int[3];
int pixel = imagegetRGB(x, y);
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
}
return rgb;
}
注
image 源图像。
x 图像上指定像素位置的 x 坐标。
y 图像上指定像素位置的 y 坐标。
(要定位25次,慢慢来吧)
黑色的rgb是0,0,0 ,白色的rgb 255, 251, 240(建议用photoshop或者其他软件,先获取这样比较精确),然后再进行逻辑判断就Ok了。
主要包
import javaawtColor;
import javaawtimageBufferedImage;
import javaioFile;
import javaioIOException;
import javaximageioImageIO;
准确的说是可以。
JAVA可以读入一个到内存保存为字节数组,再从数组中找到对应的位置下标,以数组内容判断RGB颜色。不过一般人是不会这么做的。
要看你是在什么项目以什么目的需求来取颜色。大部分方案都是以特殊技巧来实现你所提出的问题。
打个比方,如果是我自己上传的,只是显示给别人看的,那么我会把不同部位自定义编码,不同编码对应不同颜色。然后在显示时对设置热区就行了
楼上的基本正确,
问题一:
int[] rgb = new int[3];最好用二维数组
int[] rgb = new int[3][widthheight]
问题二:
rgb[0] = (pixel & 0xff0000 ) >> 16 ;
rgb[1] = (pixel & 0xff00 ) >> 8 ;
rgb[2] = (pixel & 0xff );
会把数组内的值覆盖,获得就是最后像素点的RGB值;
我写了一个希望可以帮助到你
package imageReadAndWrite;
import javaawtimageBufferedImage;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import comsunimagecodecjpegJPEGCodec;
import comsunimagecodecjpegJPEGImageDecoder;
import comsunimagecodecjpegJPEGImageEncoder;
/
JPG File reader/writer Uses native comsun libraries (which may deprecate at
any time)
@author PhoenixZJG
@version 10
/
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 {
thisfilename = 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 = JPEGCodeccreateJPEGEncoder(fOut);
BufferedImage image = new BufferedImage(width, height,
BufferedImageTYPE_INT_RGB);
imagesetRGB(0, 0, width, height, data, 0, width);
jpeg_encodeencode(image);
fOutclose();
}
/
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 = JPEGCodeccreateJPEGDecoder(fIn);
BufferedImage image = jpeg_decodedecodeAsBufferedImage();
width = imagegetWidth();
height = imagegetHeight();
int[] rgbdata = new int[width height];
imagegetRGB(0, 0, width, height, rgbdata, 0, width);
ints = rgbdata;
bytes = new byte[rgbdatalength];
doubles = new double[rgbdatalength];
imageRGB = new int[3][rgbdatalength];
for (int i = 0; i < byteslength; 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 怎么提取Image对象的像素数据全部的内容,包括:java 怎么提取Image对象的像素数据、java 从#FFFFFF 得到颜色、Java实现根据分量计算rgb值,并分离rgb分量等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)