⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageresizer.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		data.top = new FormAttachment(attach, 10);
		data.right = new FormAttachment(100, -10);
		btnCancel.setLayoutData(data);
		btnCancel.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event arg0) {
				result = null;
				done = true;
			}
		});

		Button btnOk = new Button(shell, SWT.PUSH);
		btnOk.setText("OK");
		data = new FormData();
		data.width = 70;
		data.top = new FormAttachment(attach, 10);
		data.right = new FormAttachment(btnCancel, -10);
		btnOk.setLayoutData(data);
		btnOk.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event arg0) {
				result = computeResultImage();
				done = true;
			}
		});

		shell.setDefaultButton(btnOk);
		btnOk.setFocus();
		shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		if (parent != null) {
			Utils.centerWindowRelativeTo(shell, parent);
		}

		shell.open();
		drawCurrentImage();

	}

	private boolean checkSize(Image image) {
		//If the image is smaller than the minimal size, we shouldn't accept it
		Rectangle size = image.getBounds();
		if (size.width < minWidth || size.height < minHeight) {
			return false;
		}

		float minHRatio = (float) (minHeight) / size.height;
		float minWRatio = (float) (minWidth) / size.width;

		float maxHRatio = (float) (minHeight * 4) / size.height;
		float maxWRatio = (float) (minWidth * 4) / size.width;

		//We must keep the min zoom bigger than the "biggest" ratio (ie, smallest zoom out)
		minZoomRatio = minHRatio > minWRatio ? minHRatio : minWRatio;
		maxZoomRatio = maxHRatio > maxWRatio ? maxHRatio : maxWRatio;

		if (maxZoomRatio > 1) {
			maxZoomRatio = 1;
		}

		zoomRatio = minZoomRatio;

		return true;
	}

	private ImageData internalResize(Image image, int newWidth, int newHeight) {

		ImageData srcData = image.getImageData();

		//int width = srcData.width,height = srcData.height;

		ImageData data = srcData;
		//int newWidth = (int)(width*zoomRatio);
		//int newHeight = (int)(height*zoomRatio);

		final ImageData copy = new ImageData(newWidth, newHeight, 24,
				new PaletteData(0x00FF0000, 0x0000FF00, 0x000000FF));

		Image src = new Image(display, srcData);
		Image dst = new Image(display, copy);

		GC gc = new GC(dst);
		gc.setAdvanced(true);
		try {
			gc.setInterpolation(SWT.HIGH);
		} catch (Exception e) {
			// may not be avail
		}
		gc.drawImage(src, 0, 0, srcData.width, srcData.height, 0, 0, copy.width,
				copy.height);
		//gc.setAlpha(50);
		//gc.drawImage(src,2,2,srcData.width-2,srcData.height-2,0,0,copy.width,copy.height);
		gc.dispose();

		data = dst.getImageData();

		src.dispose();
		dst.dispose();

		return data;
	}

	private void drawCurrentImage() {
		GC gcCanvas = new GC(canvas);

		Image temp = new Image(display, displayWidth, displayHeight);
		GC gc = new GC(temp);
		gc.drawImage(currentImage, offset.x + MARGIN + 1, offset.y + MARGIN + 1);
		//gc.setAlpha(128);
		gc.drawImage(overlay, 0, 0);
		//gc.setTextAntialias(SWT.ON);
		//gc.drawText("This is a test", 15, displayHeight-15,true);
		gc.dispose();

		gcCanvas.drawImage(temp, 0, 0);
		temp.dispose();

		gcCanvas.dispose();
	}

	private void insureOffsetIsCorrect() {
		int minX = minWidth - currentImage.getBounds().width;
		if (offset.x < minX) {
			offset.x = minX;
		}

		int minY = minHeight - currentImage.getBounds().height;
		if (offset.y < minY) {
			offset.y = minY;
		}

		if (offset.x > 0) {
			offset.x = 0;
		}

		if (offset.y > 0) {
			offset.y = 0;
		}

	}

	private void dispose() {
		if (shell != null && !shell.isDisposed()) {
			shell.dispose();
		}

		if (currentImage != null && !currentImage.isDisposed()) {
			currentImage.dispose();
		}

		if (overlayDragging != null && !overlayDragging.isDisposed()) {
			overlayDragging.dispose();
		}

		if (overlayNotDragging != null && !overlayNotDragging.isDisposed()) {
			overlayNotDragging.dispose();
		}

		if (cursor != null && !cursor.isDisposed()) {
			cursor.dispose();
		}
	}

	private Image computeResultImage() {
		Image img = new Image(display, minWidth, minHeight);

		/*ImageData srcData = original.getImageData();
		 ImageData dstData = new ImageData(
		 currentImage.getBounds().width,
		 currentImage.getBounds().height,
		 32,
		 new PaletteData(0xFF,0xFF00,0xFF0000));
		 Resample resample = new Resample();		
		 resample.setFilter(Resample.FILTER_TYPE_LANCZOS3, 7.0f);
		 resample.process(srcData, dstData);
		 Image filtered = new Image(display,dstData);
		 */
		GC gc = new GC(img);
		gc.drawImage(currentImage, offset.x, offset.y);
		gc.dispose();
		//filtered.dispose();
		return img;
	}

	private Image createOverlayImage(final byte marginAlpha,
			final int marginColor, final byte borderAlpha, final int borderColor) {

		int width = displayWidth;
		int height = displayHeight;

		ImageData data = new ImageData(width, height, 32, new PaletteData(
				0x000000FF, 0x0000FF00, 0x00FF0000));

		byte[] transparency = new byte[width * height];
		int[] pixels = new int[width * height];

		byte rowAlpha[] = new byte[width];
		int rowPixels[] = new int[width];
		//Top
		//Pattern
		for (int i = 0; i < width; i++) {
			rowAlpha[i] = marginAlpha;
			rowPixels[i] = marginColor;
		}
		//Fill
		for (int i = 0; i < MARGIN; i++) {
			System.arraycopy(rowAlpha, 0, transparency, i * width, width);
			System.arraycopy(rowPixels, 0, pixels, i * width, width);
		}

		//Main area
		//Pattern
		for (int i = 0; i < MARGIN; i++) {
			rowAlpha[i] = marginAlpha;
			rowAlpha[width - i - 1] = marginAlpha;
		}
		for (int i = MARGIN; i < width - MARGIN; i++) {
			rowAlpha[i] = 0;
		}
		//Fill
		for (int i = MARGIN; i < height - MARGIN; i++) {
			System.arraycopy(rowAlpha, 0, transparency, i * width, width);
			System.arraycopy(rowPixels, 0, pixels, i * width, width);
		}

		//Bottom
		//Pattern
		for (int i = 0; i < width; i++) {
			rowAlpha[i] = marginAlpha;
		}
		//Fill
		for (int i = height - MARGIN - 1; i < height; i++) {
			System.arraycopy(rowAlpha, 0, transparency, i * width, width);
			System.arraycopy(rowPixels, 0, pixels, i * width, width);
		}

		//Let's do the border part		
		for (int i = MARGIN; i < width - MARGIN; i++) {
			transparency[width * MARGIN + i] = borderAlpha;
			pixels[width * MARGIN + i] = borderColor;
		}
		for (int j = MARGIN; j < height - MARGIN; j++) {

			transparency[j * width + MARGIN] = borderAlpha;
			pixels[j * width + MARGIN] = borderColor;

			transparency[j * width + width - MARGIN - 1] = borderAlpha;
			pixels[j * width + width - MARGIN - 1] = borderColor;

		}
		for (int i = MARGIN; i < width - MARGIN; i++) {
			transparency[width * (height - MARGIN - 1) + i] = borderAlpha;
			pixels[width * (height - MARGIN - 1) + i] = borderColor;
		}

		data.alphaData = transparency;
		data.setPixels(0, 0, width * height, pixels, 0);

		Image overlay = new Image(display, data);

		return overlay;
	}

	private void refreshCurrentImage(int position) {
		float previousZoom = zoomRatio;
		zoomRatio = (float) position / RESIZE_STEPS;
		if (zoomRatio > 1) {
			zoomRatio = 1;
		}
		if (zoomRatio < minZoomRatio) {
			zoomRatio = minZoomRatio;
		}
		if (previousZoom != zoomRatio) {
			Image previous = currentImage;
			currentImage = new Image(display,
					internalResize(original, (int) (originalWidth * zoomRatio),
							(int) (originalHeight * zoomRatio)));

			//float ratio = zoomRatio / previousZoom;
			offset.x += (previous.getBounds().width - currentImage.getBounds().width) / 2;
			offset.y += (previous.getBounds().height - currentImage.getBounds().height) / 2;

			if (previous != null && !previous.isDisposed()) {
				previous.dispose();
			}

			insureOffsetIsCorrect();
			drawCurrentImage();
		}
	}

	public static void main(String args[]) throws Exception {
		Display display = Display.getDefault();
		Shell test = new Shell(display);
		ImageResizer resizer = new ImageResizer(display, 228, 128, null);
		String file = new FileDialog(test).open();
		Image img = new Image(display, file);
		Image thumbnail = resizer.resize(img);

		System.out.println(thumbnail);

		thumbnail.dispose();
		test.dispose();
		if (args.length == 0) {
			display.dispose();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -