Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多重阅读示例:图片处理中的数据分解_Java_Multithreading_Parallel Processing_Decomposition - Fatal编程技术网

Java 多重阅读示例:图片处理中的数据分解

Java 多重阅读示例:图片处理中的数据分解,java,multithreading,parallel-processing,decomposition,Java,Multithreading,Parallel Processing,Decomposition,目前,我正在为一个使用基本多线程的java程序编写一个示例。我想展示数据合成原理在类似单色滤光片的图像处理应用程序中的优势。我将图片加载到缓冲区中,然后生成四个线程,它们都计算图像的不同部分 图像分为四个切片,根据我的预期,仅计算时间就降低了(四核的速度提高了3到4倍)。 问题是我试图通过给每个线程一个缓冲区来避免竞争条件,在缓冲区中保存计算出的图片。执行后,每个线程将图片的一部分保存在jpg中 我这里的问题是,是否有一种模式可以用来再次将不同的切片保存为一张图片,同时避免共享可变状态变量的危险

目前,我正在为一个使用基本多线程的java程序编写一个示例。我想展示数据合成原理在类似单色滤光片的图像处理应用程序中的优势。我将图片加载到缓冲区中,然后生成四个线程,它们都计算图像的不同部分

图像分为四个切片,根据我的预期,仅计算时间就降低了(四核的速度提高了3到4倍)。 问题是我试图通过给每个线程一个缓冲区来避免竞争条件,在缓冲区中保存计算出的图片。执行后,每个线程将图片的一部分保存在jpg中

我这里的问题是,是否有一种模式可以用来再次将不同的切片保存为一张图片,同时避免共享可变状态变量的危险。 如果我在我的处理类中使用静态变量来重新组装图片,我得到的时间甚至比单线程解决方案还要糟糕。 如何使多线程提供的效率在本例中显现出来

这是我的多线程应用程序代码:

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;


public class Main {
  static int threadCount=4;
  public static void main(String[] args) {
    // TODO Auto-generated method stub
      BufferedImage original=null;
      BufferedImage greyscale=null;

      //loading Picture into Buffer
    try{
           original = ImageIO.read(new File("Desert.jpg"));
         }
    catch (IOException e) 
    {
            e.printStackTrace();

    }


        ArrayList<Thread> threads = new ArrayList<Thread>();


        //Creating Threads, each Thread gets a Copy of the Image
        for( int i=0; i < threadCount; i++)

        {
            final int threadNumber =i;

            threads.add(new Thread(new Processor2(deepCopy(original),threadNumber,threadCount)));}

        //threads gets started
        long start = System.currentTimeMillis();
        for( int i=0; i < threadCount; i++){
            threads.get(i).start();
        }

        for( int i=0; i < threadCount; i++){
            try {
                threads.get(i).join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        long end = System.currentTimeMillis();


        long seconds = (end-start);
         System.out.println("Dauer: "+seconds+" ms.");

}

//Method that copies the Image
static BufferedImage deepCopy(BufferedImage bi) {
 ColorModel cm = bi.getColorModel();
 boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
 WritableRaster raster = bi.copyData(null);
 return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
 }


}
导入java.awt.image.buffereImage;
导入java.awt.image.ColorModel;
导入java.awt.image.WritableRaster;
导入java.io.File;
导入java.io.IOException;
导入java.util.ArrayList;
导入javax.imageio.imageio;
公共班机{
静态int threadCount=4;
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
BuffereImage original=null;
BuffereImage灰度=null;
//将图片加载到缓冲区
试一试{
original=ImageIO.read(新文件(“Desert.jpg”);
}
捕获(IOE异常)
{
e、 printStackTrace();
}
ArrayList线程=新的ArrayList();
//创建线程时,每个线程都会获得图像的副本
对于(int i=0;i
生成的线程执行以下代码:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class Processor2 implements Runnable  {




 BufferedImage greyscale;
 BufferedImage original;

int threadNumber, threadCount;

//The Constructor gets the Image, the total number of Threads and the own Threadnumber.
Processor2(BufferedImage image, int x, int y){
    this.original=image;

    threadNumber=x;
    threadCount=y;
}
Object lock =new Object();
@Override

// The run Method goes through the pixel of the assignes slide of the picture, renders it monochromatic and then saves it in an ImageBuffer. This image is saved after the loop has reached all pixels.
public void run() {
    // TODO Auto-generated method stub
int part =original.getWidth()/threadCount;
    greyscale = new BufferedImage(part,     original.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
    int x=0;
    try{for(int i=threadNumber*part; i<part*(threadNumber+1); i++) 
    {
        for(int j=0; j<original.getHeight(); j++)
        {

            // Get pixels
            int rgb = original.getRGB(i, j);
            int a = (rgb>>24)&0xff;
            int r = (rgb >> 16) & 0xFF;
            int g = (rgb >> 8) & 0xFF;
            int b = (rgb & 0xFF);

            int avg = (r + g + b) / 3;
            int gray = colorToRGB(a, avg, avg, avg);
            greyscale.setRGB(x, j, gray);

           } x++;
}}
    finally{
    saveImage(greyscale, threadNumber);
        }
}


public void start(){

}

private static int colorToRGB(int alpha, int red, int green, int blue) {
    int newPixel = 0;
    newPixel += alpha;
    newPixel = newPixel << 8;
    newPixel += red; newPixel = newPixel << 8;
    newPixel += green; newPixel = newPixel << 8;
    newPixel += blue;

    return newPixel;
}

static void saveImage(BufferedImage r,int threadNumber){

    try {
        ImageIO.write(r, "png",new    File("blackwhiteimageshared"+threadNumber+".png") );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }

 }
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
公共类Processor2实现Runnable{
缓冲图像灰度;
缓冲图像原件;
int threadNumber,threadCount;
//构造函数获取映像、线程总数和自己的线程数。
处理器2(缓冲图像图像,整数x,整数y){
这个。原始=图像;
螺纹编号=x;
线程数=y;
}
对象锁=新对象();
@凌驾
//run方法遍历图片分配幻灯片的像素,将其渲染为单色,然后将其保存在ImageBuffer中。循环到达所有像素后,将保存此图像。
公开募捐{
//TODO自动生成的方法存根
int part=original.getWidth()/threadCount;
greyscale=new BufferedImage(part,original.getHeight(),BufferedImage.TYPE_BYTE_GRAY);
int x=0;
尝试{for(inti=threadNumber*part;i24)&0xff;
int r=(rgb>>16)和0xFF;
int g=(rgb>>8)&0xFF;
intb=(rgb&0xFF);
国际平均值=(r+g+b)/3;
int gray=色度GB(a,平均值,平均值,平均值);
灰度.setRGB(x,j,灰色);
}x++;
}}
最后{
保存图像(灰度、螺纹编号);
}
}
公开作废开始(){
}
专用静态int-colorToRGB(int-alpha、int-red、int-green、int-blue){
int newPixel=0;
新像素+=α;
newPixel=newPixel