public static File mergePicture() throws IOException, ClassNotFoundException { MapInfo mapInfo = Download.readPictureInfo(); System.out.println(mapInfo == null); CenterProviter p = new CenterProviter(mapInfo.getC1(), mapInfo.getC2()); BufferedImage writer = new BufferedImage(256 * p.getMaxX(), 256 * p.getMaxY(), BufferedImage.TYPE_INT_RGB); for (int i = 0; i < p.getMaxX(); i++) { for (int j = 0; j < p.getMaxY(); j++) { BufferedImage reader = ImageIO.read(new File(path + "\\" + i + "_" + j + ".png")); for (int ii = 0; ii < 256; ii++) { for (int jj = 0; jj < 256; jj++) { int rgb = reader.getRGB(ii, jj); int x = 256 * i + ii; int y = 256 * j + jj; try { writer.setRGB(256 * i + ii, 256 * j + jj, rgb); } catch (Exception e) { System.out.println("x=" + x + " y=" + y); throw new IOException(); } } } } } File file = new File(path + "\\new.png"); file.createNewFile(); ImageIO.write(writer, "png", file); return file; }
public void run() { // Create a file channel for the file try { _parent.addToActive(this); file = new RandomAccessFile(Globals.ourHome + outFile, "rwd"); channel = file.getChannel(); // Create a segment downloader for each segment and run them simultaneously for (int i = 0; i < nSeg; i++) { segDownloads.add(new SegDownloader(this, peers.get(i % peers.size()) + "/" + path, i)); (new Thread(segDownloads.get(segDownloads.size() - 1))).start(); } // Wait for a download thread to either finish or fail, and update our bookkeeping try { while (!done) { // Get a new segment downloader off the stopped list. SegDownloader s = bstopped.take(); // Give a new path to every download thread that failed. if (s.status() == Dstatus.FAILED) { synchronized (s) { s.dlPath = getNewPath(); removeStopped(s); s.notify(); } } // Check if that segment finishes off the download. else if (s.status() == Dstatus.FINISHED) { if (nSeg == doneSegs) { done = true; percentDone = 100; System.out.println("Download " + Globals.ourHome + outFile + " Finished!"); // "Notify" the waiters that we're done. while (waiters > 0 && waitToken.take()) { waiters--; } } } else throw new RuntimeException("Impossible"); } } catch (InterruptedException e) { status = Dstatus.FAILED; } } catch (IOException e) { Log.info("Could not open file for download!"); status = Dstatus.FAILED; } catch (InterruptedException e) { // TODO Auto-generated catch block status = Dstatus.FAILED; } }