使用PixelReader / PixelWriter JavaFX获取Image的一部分

时间:2016-03-17 23:11:26

标签: java image javafx

我正在尝试使用JavaFX获取图像的一部分,但代码在import UIKit class DataViewController: UIViewController { @IBOutlet weak var myImageView: UIImageView! var dataObject: String? override func viewDidLoad() { super.viewDidLoad() autoreleasepool { () -> () in self.myImageView.image = UIImage(contentsOfFile: dataObject!) // with "contentsOfFile:" the images are uncached... } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 时失败。

以下是代码:

y = 1

一切顺利,直到循环中的public static Image crop(Image src, int col, int row) { PixelReader r = src.getPixelReader(); int sx = col * Grid.SIZE; // start x int sy = row * Grid.SIZE; // start y int ex = sx + Grid.SIZE; // end x int ey = sy + Grid.SIZE; // end y int rx = 0; // x to be written int ry = 0; // y to be written System.out.println(sx + ", " + sy + ", " + ex + ", " + ey); WritableImage out = new WritableImage(Grid.SIZE, Grid.SIZE); PixelWriter w = out.getPixelWriter(); for(int y = sy; y < ey; y++, ry++) { for(int x = sx; x < ex; x++, rx++) { int c = r.getArgb(x, y); w.setArgb(rx, ry, c); System.out.println(rx + ", " + ry + ", " + x + ", " + y); } } return out; } 变为1,然后发生这种情况:

y

我不知道出了什么问题。我会提供任何其他信息。

1 个答案:

答案 0 :(得分:1)

您开始获取索引越界异常,因为当您开始每个新行时,您并未将rx重置为0

但是,如果您需要从Grid.SIZE Grid.SIZE src裁剪(col, row),从public static Image crop(Image src, int col, int row) { PixelReader r = src.getPixelReader(); PixelFormat<IntBuffer> pixelFormat = PixelFormat.getIntArgbInstance() ; int[] pixels = new int[Grid.SIZE * Grid.SIZE]; r.getPixels(col * Grid.SIZE, row * Grid.SIZE, Grid.SIZE, Grid.SIZE, pixelFormat, pixels, 0, Grid.SIZE); WritableImage out = new WritableImage(Grid.SIZE, Grid.SIZE); PixelWriter w = out.getPixelWriter(); w.setPixels(0, 0, Grid.SIZE, Grid.SIZE, pixelFormat, pixels, 0, Grid.SIZE); return out ; } 开始,更简单(也可能表现更好)的方法是:

(col+1)*Grid.SIZE > src.getWidth()

显然,如果(row+1)*Grid.SIZE > src.getHeight()IllegalArgumentException,您可以在方法中检查并在需要时抛出var modal = UIkit.modal(".modalSelector"); if ( modal.isActive() ) { modal.hide(); } else { modal.show(); } $('.modalSelector').on({ 'show.uk.modal': function(){ console.log("Modal is visible."); }, 'hide.uk.modal': function(){ console.log("Element is not visible."); } }); ,这将会失败。

相关问题