让 Eclipse 插件程序具有二维作图能力(2)

2016-02-19 16:28 3 1 收藏

清醒时做事,糊涂时读书,大怒时睡觉,无聊时关注图老师为大家准备的精彩内容。下面为大家推荐让 Eclipse 插件程序具有二维作图能力(2),无聊中的都看过来。

【 tulaoshi.com - 编程语言 】

   创建 SWT 图像

  SWT Image 类似于 AWT BufferedImage,因为它的像素数据可以有直接读或者写操作访问。这意味着可以通过直接读取或者修改图像的数据,来设置或者取得图像中任何像素或者任何一组像素的颜色值。不过, SWT API 与相应的 AWT API 有很大不同,并且更容易使用。

  SWT 的 Image 类提供了几个构造函数,可以完成以下任务:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  通过将一个文件名或者一个 InputStream 作为参数传递给构造函数装载一个现有的图像。图像的格式必须是所支持的格式之一:BMP、GIF、JPG、PNG、Windows ICO 等。

  构造一个指定大小的空图像。可以通过修改其像素值或者向它拷贝一个 SWT 图形上下文的内容 (GC) 来绘制该图像。构造一个用像素值的现有缓冲区进行初始化的图像。您将使用第三个构造函数创建一个 SWT 图像,它是所绘制的 AWT 图像的副本。

   关于 ImageData 类

  有关图像的像素数据的信息包含在它的 ImageData 中。ImageData 是一个包含有关图像大小、调色板、颜色值和透明度信息的类。应当特别关注以下 ImageData 字段:

  width 和 height 指定图像的大小。

  depth 指定图像的颜色深度。可能的值为 1、2、4、8、16、24 或者 32,指定编码每一个像素的值所使用的位数。

  palette 包含一个 PaletteData 对象,它存储有关图像的颜色模型的信息。与 AWT 一样,SWT 的颜色模型可以是索引或者直接的。如果颜色模型是索引的,那么 PaletteData 包含颜色索引。如果它是直接的,那么它包含转换(shift)信息,表明应当如何从像素的整数值中提取出颜色的 RGB 成分。

  data 包含包含有像素值的字节缓冲区。与 AWT 缓冲区不同,SWT 缓冲区不是包含每一个像素的一种颜色值的整数数组。相反,它包含字节值。字节编码的方法取决于所使用的颜色深度。对于一个 8 位的图像,数组中的一个字节正好表示图像中一个像素的值。对于 16 位图像,每一个像素值编码为缓冲区中的两个字节。这两个字节以最低有效字节顺序存储。对于 24 或者 32 位图像,每一个像素值以最高有效位字节顺序编码为缓冲区中的三个或者四个字节。

  bytesPerline 表明缓冲区中有多少字节用于编写图像中一行像素的所有像素值。transparentPixel 定义用于图像中透明度的像素值。我们将使用带有一个透明度颜色信道的 24 位图像。图像中的每一个像素都编码为数组中的三个字节,顺序为红、绿和蓝成分。

   转换图像

  知道了图像数据就可以容易地将 AWT 图像转换为 SWT 图像。只要将(由 AWT 图像利用 getRGB(...) 返回的)整数缓冲区转换为 SWT 图像所使用的字节缓冲。图 3 显示了在 SWT 图像的缓冲区中这些值是如何存储的。

  图3. 将像素值写入 SWT 图像(图片较大,请放大查看)和图 2 中一样,上图中下面的部分显示了图像缓冲区的内部表示。括号中的数字显示在缓冲区中表示其颜色值的那个像素的坐标。尽管每一个像素都用三个字节编码,但是对于 24 位图像,缓冲区中一行像素的大小并不总是 3*width。缓冲区中两行像素之间可能有一些索引未使用。要知道图像中每一行像素真正使用了多少字节(这样就可知道缓冲区中下一行从哪个索引位置开始),必须使用 ImageData 字段的 bytesPerLine 值。

   SWT 到 Java 2D 渲染器

  清单 1 显示实现了屏外图像技术的一般性渲染器(renderer)的源代码。这个渲染器可以在 SWT 组件或者 Draw2D 图像上绘制时透明地使用 Java 2D 例程。

  清单 1. SWT/Draw2D Java 2D renderer

  

package swtgraphics2d;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import org.Eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Display;
/**
* Helper class allowing the use of Java 2D on SWT or Draw2D graphical
* context.
* @author Yannick Saillet
*/
public class Graphics2DRenderer {
private static final PaletteData PALETTE_DATA =
new PaletteData(0xFF0000, 0xFF00, 0xFF);
private BufferedImage awtImage;
private Image swtImage;
private ImageData swtImageData;
private int[] awtPixels;
/** RGB value to use as transparent color */
private static final int TRANSPARENT_COLOR = 0x123456;
/**
* Prepare to render on a SWT graphics context.
*/
public void prepareRendering(GC gc) {
org.eclipse.swt.graphics.Rectangle clip = gc.getClipping();
prepareRendering(clip.x, clip.y, clip.width, clip.height);
}
/**
* Prepare to render on a Draw2D graphics context.
*/
public void prepareRendering(org.eclipse.draw2d.Graphics graphics) {
org.eclipse.draw2d.geometry.Rectangle clip =
graphics.getClip(new org.eclipse.draw2d.geometry.Rectangle());
prepareRendering(clip.x, clip.y, clip.width, clip.height);
}
/**
* Prepare the AWT offscreen image for the rendering of the rectangular
* region given as parameter.
*/
private void prepareRendering(int clipX, int clipY, int clipW, int clipH) {
// check that the offscreen images are initialized and large enough
checkOffScreenImages(clipW, clipH);
// fill the region in the AWT image with the transparent color
java.awt.Graphics awtGraphics = awtImage.getGraphics();
awtGraphics.setColor(new java.awt.Color(TRANSPARENT_COLOR));
awtGraphics.fillRect(clipX, clipY, clipW, clipH);
}
/**
* Returns the Graphics2D context to use.
*/
public Graphics2D getGraphics2D() {
if (awtImage == null) return null;
return (Graphics2D) awtImage.getGraphics();
}
/**
* Complete the rendering by flushing the 2D renderer on a SWT graphical
* context.
*/
public void render(GC gc) {
if (awtImage == null) return;
org.eclipse.swt.graphics.Rectangle clip = gc.getClipping();
transfERPixels(clip.x, clip.y, clip.width, clip.height);
gc.drawImage(swtImage, clip.x, clip.y, clip.width, clip.height,
clip.x, clip.y, clip.width, clip.height);
}
/**
* Complete the rendering by flushing the 2D renderer on a Draw2D
* graphical context.
*/
public void render(org.eclipse.draw2d.Graphics graphics) {
if (awtImage == null) return;
org.eclipse.draw2d.geometry.Rectangle clip =
graphics.getClip(new org.eclipse.draw2d.geometry.Rectangle());
transferPixels(clip.x, clip.y, clip.width, clip.height);
graphics.drawImage(swtImage, clip.x, clip.y, clip.width, clip.height,
clip.x, clip.y, clip.width, clip.height);
}
/**
* Transfer a rectangular region from the AWT image to the SWT image.
*/
private void transferPixels(int clipX, int clipY, int clipW, int clipH) {
int step = swtImageData.depth / 8;
byte[] data = swtImageData.data;
awtImage.getRGB(clipX, clipY, clipW, clipH, awtPixels, 0, clipW);
for (int i = 0; i clipH; i++) {
int idx = (clipY + i) * swtImageData.bytesPerLine + clipX * step;
for (int j = 0; j clipW; j++) {
int rgb = awtPixels[j + i * clipW];
for (int k = swtImageData.depth - 8; k = 0; k -= 8) {
data[idx++] = (byte) ((rgb k) & 0xFF);
}
}
}
if (swtImage != null) swtImage.dispose();
swtImage = new Image(Display.getDefault(), swtImageData);
}
/**
* Dispose the resources attached to this 2D renderer.
*/
public void dispose() {
if (awtImage != null) awtImage.flush();
if (swtImage != null) swtImage.dispose();
awtImage = null;
swtImageData = null;
awtPixels = null;
}
/**
* Ensure that the offscreen images are initialized and are at least
* as large as the size given as parameter.
*/
private void checkOffScreenImages(int width, int height) {
int currentImageWidth = 0;
int currentImageHeight = 0;
if (swtImage != null) {
currentImageWidth = swtImage.getImageData().width;
currentImageHeight = swtImage.getImageData().height;
}
// if the offscreen images are too small, recreate them
if (width currentImageWidth || height currentImageHeight) {
dispose();
width = Math.max(width, currentImageWidth);
height = Math.max(height, currentImageHeight);
awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
swtImageData = new ImageData(width, height, 24, PALETTE_DATA);
swtImageData.transparentPixel = TRANSPARENT_COLOR;
awtPixels = new int[width * height];
}
}
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  这个渲染器包含在一个实用程序类中。这个类包含并管理屏外图像技术所需要的 AWT 和 SWT 屏外图像的引用。还要注意:

  字段 swtImageData 和 awtPixels 分别是在像素转移时包含 SWT 图像的像素值的缓冲区和用于包含 AWT 图像的像素值的缓冲区。

  常量 TRANSPARENT_COLOR 包含一个作为 SWT 图像中透明颜色的 RGB 值。因为必须定义作为透明度信道的颜色以绘制背景,所以必须为此保留一个颜色值。在代码中我使用了随机值 0x123456。所有使用这个颜色值的像素都按透明处理。如果这个值所表示的颜色有可能在绘制操作中用到,可以用另一个值表示透明度。

来源:https://www.tulaoshi.com/n/20160219/1612210.html

延伸阅读
标签: 软件教程
你还在让别人加你群输qq群账号你就OUT了,新版的qq以及可以扫二维码,那么也就是说你的qq群也可以建立一个二维码,好友加你的群就不需要再复杂的输qq群账号了,那qq群二维码怎么生成呢?看看小编给大家的介绍吧! 第一步:先打开我们的手机上找到群 第二步:找到我们要生成的那个qq群 第三步:进入群,在...
做iOS的二维码扫描,有两个第三方库可以选择,ZBar和ZXing。今天要介绍的是iOS7.0后AVFoundation框架提供的原生二维码扫描。 首先需要添加AVFoundation.framework框架到你工程中build phase的"Link Binary With Libraries"之下,然后就可以开始了。 一、做好准备工作,搭建UI UI效果如图 IBOutlet、IBAction如下: @property (weak, non...
标签: 二维码
  方法一:找到要扫描和识别的二维码图片,这里我就以百度的一个应用来示范。把这个二维码图片截图,保存下来。   在百度搜索在线二维码扫描,点击第一个进入。前提是联网才能用这种方法。 有三种方法识别,大家接下来都应该会了。电脑有摄像头的可以用第一个,这三种用起来都简单,我们选择第二种来...
标签: 电脑应用
1.对于简单图形,如矩形、三角形。只须执行命令AREA(可以是命令行输入或点击对应命令图标),在命令提示“Specify firstcorner point or [Object/Add/Subtract]:”后,打开捕捉依次选取矩形或三角形各交点后回车,AutoCAD将自动计算面积(Area)、周长(Perimeter),并将结果列于命令行。   2.对于简单...
标签: 二维码 电脑
下载“电脑端二维码识别工具” 首先,要在电脑上下载一个“电脑端二维码识别工具”。 二维码图像文件的获取 可以使用截图工具将二维码图案以文件形式进行保存。比如利用QQ截图工具就可以将屏幕内指定区域进行截屏。 “文件”→“打开” 接着点击“电脑端二维码识别工具”界面中的“文件”→“打开”项。 点击“打开” 选择本...

经验教程

566

收藏

59
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部