Java 合并BuffereImage会留下黑色边框
对于一个类项目,我们需要使用一个实现Sobel算法的程序并跨多个线程运行它,作为熟悉线程的练习。就线程而言(据我所知),我的程序运行正常,但是每当我使用多个线程实现它时,我会看到一个黑色边框沿着分割区域运行,其中缓冲区似乎没有写入目标BuffereImage 以下是负责线程化和写入文件的方法的一些代码:Java 合并BuffereImage会留下黑色边框,java,multithreading,bufferedimage,Java,Multithreading,Bufferedimage,对于一个类项目,我们需要使用一个实现Sobel算法的程序并跨多个线程运行它,作为熟悉线程的练习。就线程而言(据我所知),我的程序运行正常,但是每当我使用多个线程实现它时,我会看到一个黑色边框沿着分割区域运行,其中缓冲区似乎没有写入目标BuffereImage 以下是负责线程化和写入文件的方法的一些代码: public void processImage(int threads) { try{ File imgFile = new File(bmpFile);
public void processImage(int threads)
{
try{
File imgFile = new File(bmpFile);
image = ImageIO.read(imgFile);
w = image.getWidth();
wThr = (image.getWidth()/threads);
h = image.getHeight();
inData = new int[wThr*h];
BufferedImage[] pieces = new BufferedImage[threads];
//instantiate array used for storing pieces of image
for (int i=0; i<threads; i++){
pieces[i] = new BufferedImage(wThr, h, BufferedImage.TYPE_BYTE_GRAY);
}
wThr = pieces[0].getWidth();
h = pieces[0].getHeight();
//instantiate target image
combined = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
//split into threads, each one taking a division of the image and running algorithm
Thread[] threadList = new Thread[threads];
for (int i = 0; i < threadList.length; i++) {
image.getRaster().getPixels((i*wThr), 0, wThr, h, inData);
threadList[i] = new Pr1();
threadList[i].start();
try{
threadList[i].join();
}catch (InterruptedException ie) {}
//Write images to pieces and draw pieces individually onto target image
pieces[i].getRaster().setPixels(0, 0, wThr, h, outData);
Graphics2D g = combined.createGraphics();
g.drawImage(pieces[i], i*(wThr), 0, null);
g.dispose();
}
outFile = new File("1.bmp");
ImageIO.write(combined, "BMP", outFile);
}
catch(IOException e)
{
// Handle the exception here!
}
}
public void processImage(int线程)
{
试一试{
文件imgFile=新文件(bmpFile);
image=ImageIO.read(imgFile);
w=image.getWidth();
wThr=(image.getWidth()/threads);
h=image.getHeight();
inData=新整数[wThr*h];
BuffereImage[]个=新的BuffereImage[线程];
//实例化用于存储图像片段的数组
对于(int i=0;iWaw!如果275Kb图像的大小是问题的10%(甚至20%),我可能会将其直接编辑到问题中!为了更快地获得更好的帮助,请发布一个指向小(以字节为单位)的.hot链接图像或在运行时生成一个。我想知道原始图像是否为1001像素宽。您需要计算整数除法wThr
,并且不考虑余数。原始图像是1000x1000,因此,据我所知,它不应该基于一个2线程示例删除任何像素,因为它将导致偶数除法。是否存在som为什么整数除法会引起问题?那你怎么说它是多线程?!!你启动了一个线程,并试图在启动后加入它!所以这和单个线程一样。也许会有帮助。