My question is about jogl. I draw image in canvas display. But I draw polygon on image. I draw polygon but polygn is transparan and not colour. Can ı do it?
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL.GL_BLEND);
BufferedImage bufferedImage = null;
int w = 0;
int h = 0;
try {
I get image and its width and height
bufferedImage = ImageIO.read(OpenGLFuncition.class.getResource("kibris.png"));
w = bufferedImage.getWidth();
h = bufferedImage.getHeight();
} catch (IOException ex) {
}
I creat the image area
WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
w,
h,
4,
null);
I set the color of the image drawing
ComponentColorModel colorModel =
new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[]{8, 8, 8, 8},
true,
false,
ComponentColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
BufferedImage dukeImg =
new BufferedImage(colorModel,
raster,
false,
null);
graphic
Graphics2D g = dukeImg.createGraphics();
g.drawImage(bufferedImage, null, null);
DataBufferByte dukeBuf =
(DataBufferByte) raster.getDataBuffer();
byte[] dukeRGBA = dukeBuf.getData();
ByteBuffer bb = ByteBuffer.wrap(dukeRGBA);
bb.position(0);
bb.mark();
gl.glBindTexture(GL.GL_TEXTURE_2D, 13);
gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, w, h, 0, GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE, bb);
int left = 0;
int top = 0;
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, 13);
gl.glBegin(GL.GL_POLYGON);
gl.glTexCoord2d(0, 0);
gl.glVertex2d(left, top);
gl.glTexCoord2d(1, 0);
gl.glVertex2d(left + w, top);
gl.glTexCoord2d(1, 1);
gl.glVertex2d(left + w, top + h);
gl.glTexCoord2d(0, 1);
gl.glVertex2d(left, top + h);
gl.glEnd();
polygon draw
for (int i = 0; coordinateLoc.length - 1 > i; i++) {
gl.glBegin(GL.GL_POLYGON);
gl.glColor3ub(coordinateLoc[i].get(coordinateLoc[i].size() - 1).getColourred(), coordinateLoc[i].get(coordinateLoc[i].size() - 1).getColourgreen(), coordinateLoc[i].get(coordinateLoc[i].size() - 1).getColourblue());
for (CoordinateXY ca : coordinateLoc[i]) {
// System.out.println(ca.getCoordinateX());
gl.glVertex2i(ca.getCoordinateX(), 449 - ca.getCoordinateY());
}
gl.glEnd();
You should call glcolor() otherwise everything will be transparent.
gl.glTexCoord2d(0, 1);
gl.glcolor(red,green,blue,alpha);
gl.glVertex2d(left, top + h)
Related
Want to Cut background image of canvas circle
canvas.drawBitmap(background_image, 0, 0, null);
FaceDetector.Face face = faces[0];
tmp_paint.setColor(Color.RED);
`face.getMidPoint(tmp_point);
canvas.drawCircle(tmp_point.x, tmp_point.y, face.eyesDistance(), tmp_paint);
You can use the following function:
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
int targetWidth = 125;
int targetHeight = 125;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(
((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(
sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight),
p);
return targetBitmap;
}
For more details check this : http://www.androiddevelopersolutions.com/2012/09/crop-image-in-circular-shape-in-android.html
I'm trying to render an image on a basic quad, I took a look at the Space Invaders Example Game for the code, and implemented that code into mine. The image gets renderer on screen, with the right colors, but the image seems shifted. This is the image I'm trying to render:
http://img203.imageshack.us/img203/5264/testwq.png
This is how it renders:
http://img593.imageshack.us/img593/6849/test2uh.png
The image is 128x128, and so is the quad.
Here is my code:
public class RenderEngine
{
private IntBuffer intbuf = BufferUtils.createIntBuffer(1);
private ColorModel glAlphaColorModel;
private ColorModel glColorModel;
public RenderEngine()
{
this.glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
this.glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 0 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
}
public void bindTexture(String filename)
{
try
{
File file = new File(CivilPolitica.instance.getDir(), "resources/" + filename);
FileInputStream fis = new FileInputStream(file);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.getTexture(fis));
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
private int getTexture(InputStream in)
{
try
{
GL11.glGenTextures(this.intbuf);
int id = this.intbuf.get(0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
BufferedImage bi = ImageIO.read(in);
int format = bi.getColorModel().hasAlpha() ? GL11.GL_RGBA : GL11.GL_RGB;
ByteBuffer texData;
WritableRaster raster;
BufferedImage texImage;
int texWidth = 2;
int texHeight = 2;
while (texWidth < bi.getWidth())
{
texWidth *= 2;
}
while (texHeight < bi.getHeight())
{
texHeight *= 2;
}
if (bi.getColorModel().hasAlpha())
{
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
texImage = new BufferedImage(this.glAlphaColorModel, raster, false, new Hashtable<String, Object>());
}
else
{
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
texImage = new BufferedImage(this.glColorModel, raster, false, new Hashtable<String, Object>());
}
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f, 0f, 0f, 0f));
g.fillRect(0, 0, texWidth, texHeight);
g.drawImage(bi, 0, 0, null);
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
texData = ByteBuffer.allocateDirect(data.length);
texData.order(ByteOrder.nativeOrder());
texData.put(data, 0, data.length);
texData.flip();
glTexParameteri(GL11.GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
glTexParameteri(GL11.GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, bi.getWidth(), bi.getHeight(), 0, format, GL_UNSIGNED_BYTE, texData);
return id;
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
return 0;
}
}
}
And the actual quad:
CivilPolitica.instance.renderer.bindTexture("test.png");
GL11.glPushMatrix();
GL11.glTranslatef(128, 128, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2i(0, 0);
GL11.glTexCoord2f(0, 127);
GL11.glVertex2i(0, 128);
GL11.glTexCoord2f(127, 127);
GL11.glVertex2i(128, 128);
GL11.glTexCoord2f(127, 0);
GL11.glVertex2i(128, 0);
GL11.glEnd();
GL11.glPopMatrix();
GL11.glTexCoord2f(0, 0);
GL11.glVertex2i(0, 0);
GL11.glTexCoord2f(0, 127);
GL11.glVertex2i(0, 128);
GL11.glTexCoord2f(127, 127);
GL11.glVertex2i(128, 128);
GL11.glTexCoord2f(127, 0);
GL11.glVertex2i(128, 0);
must be
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex2i(0, 0);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex2i(0, 128);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex2i(128, 128);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex2i(128, 0);
because it are texture coordinates from 0.0f to 1.0f (0.0f ist the one side and 1.0f is the other, that way it is not resolution dependent)
I am making an OCR for my project and stuck on a point, Right now i am performing segmentation on the basis of contours its working fine with few images but there few more where it fails even when the image quality is good, I would appreciate if someone suggest me more accurate way, and if someone provide a code example, here is my current code.
public static void imageBinarization(IplImage src, IplImage dst){
IplImage r, g, b, s;
r = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
g = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
b = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
cvSplit(src, r, g, b, null);
s = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
cvAddWeighted(r, 1./3., g, 1./3., 0.0, s);
cvAddWeighted(s, 2./3., b, 1./3., 0.0, s);
cvThreshold(s, dst, 100, 100, CV_THRESH_BINARY_INV);
cvReleaseImage(r);
cvReleaseImage(g);
cvReleaseImage(b);
cvReleaseImage(s);
}
public static void imageSegmentation(String sourcePath, String targetPath){
cvConvert(t0, mat0);
cvConvert(t8, mat8);
cvConvert(t9, mat9);
IplImage image = cvLoadImage(sourcePath);
IplImage grayImage = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
//cvSmooth(image, image, CV_BLUR_NO_SCALE, 2);
//cvSmooth(image, image, CV_BLUR, 9, 9, 2, 2);
//cvSmooth(image, image, CV_GAUSSIAN, 3);
imageBinarization(image, grayImage);
CvMemStorage mem;
CvSeq contours = new CvSeq();
CvSeq ptr = new CvSeq();
mem = cvCreateMemStorage(0);
CvRect rect = null;
int px1,px2, py1, py2;
CvScalar blue = CV_RGB(0, 0, 250);
int n = 0; int i = 0;
cvFindContours(grayImage, mem, contours, sizeof(CvContour.class) , CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
Random rand = new Random();
for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
Color randomColor = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
CvScalar color = CV_RGB( randomColor.getRed(), randomColor.getGreen(), randomColor.getBlue());
rect = cvBoundingRect(ptr, n);//new CvRect(cvGetSeqElem(c, c.total()));
px1 = rect.x(); py1 = rect.y(); px2 = (rect.x() + rect.width()); py2 = (rect.y() + rect.height());
cvRectangle(image, cvPoint(px1, py1), cvPoint(px2, py2), blue, 1, CV_AA, 0);
//----
xbox = rect.x(); ybox = rect.y(); wbox = rect.width(); hbox = rect.height();
img = cvCreateImage(cvSize(wbox, hbox), image.depth(), image.nChannels());
cvSetImageROI(image, cvRect(xbox, ybox, wbox, hbox));
cvCopy(image, img);
cvResetImageROI(image);
//cvSaveImage(targetPath+i+".jpg", img);
i++;
//---
//cvDrawContours(image, ptr, color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
}
cvSaveImage(targetPath+"mat.jpg", image);
}
Try to use some Global Thresholding algorithm such as Otsu. But JavaCV haven't implemented that. So try to find the Otsu threshold level using histogram processing and apply that value to
cvThreshold(s, dst, 100, 100, CV_THRESH_BINARY_INV);
I just want to enlarge a BufferedImage and make a watermark on it.Here is my code.
BufferedImage image = GetHtmlImage(doc, base_url, width, -1);
if (image.getHeight() < MaxShortHeight) {
return "";
}
BufferedImage gimage = new BufferedImage(image.getWidth(), image.getHeight() + 20, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = gimage.createGraphics();
g2.setBackground(Color.WHITE);
g2.drawImage(image, BufferedImageOp, 0, 0);
g2.setColor(Color.BLACK);
g2.drawString("Press by Shisoft WebFusion http://www.shisoft.net/", 10, image.getHeight() - 10);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(gimage, "jpeg", os);
At 'g2.drawImage(image, BufferedImageOp, 0, 0);'.I don't know what is BufferedImageOp.Could someone make an example.
Thank you.
I'm trying to rotate a PNG image on a canvas and the quality of the image becomes very bad after rotation. Initially the PNG is an arrow on a transparent background. After rotation it is impossible to tell that it is an arrow.
I use following code:
Transform oldTransform = new Transform(
Display.getCurrent());
gc.getTransform(oldTransform);
Transform transform = new Transform(Display.getCurrent());
transform.translate(xm + imageBounds.width / 2, ym + imageBounds.height / 2);
transform.rotate(179);
transform.translate(-xm - imageBounds.width / 2, -ym - imageBounds.height / 2);
gc.setTransform(transform);
gc.drawImage(image, xm, ym);
gc.setTransform(oldTransform);
transform.dispose();
Thank you in advance.
Instead of rotating the image 180 degrees, you could flip it horizontally and vertically (without any pixel transformation):
private BufferedImage flipH(BufferedImage src) {
int w = src.getWidth();
int h = src.getHeight();
BufferedImage dst = new BufferedImage(w, h, src.getType());
Graphics2D g = dst.createGraphics();
g.drawImage(src,
0, // x of first corner (destination)
0, // y of first corner (destination)
w, // x of second corner (destination)
h, // y of second corner (destination)
w, // x of first corner (source)
0, // y of first corner (source)
0, // x of second corner (source)
h, // y of second corner (source)
null);
g.dispose();
return dst;
}
private BufferedImage flipV(BufferedImage src) {
int w = src.getWidth();
int h = src.getHeight();
BufferedImage dst = new BufferedImage(w, h, src.getType());
Graphics2D g = dst.createGraphics();
g.drawImage(src, 0, 0, w, h, 0, h, w, 0, null);
g.dispose();
return dst;
}
...
BufferedImage flipped = flipH(flipV(ImageIO.read(new File("test.png"))));
ImageIcon icon = new ImageIcon(flipped);
...
Edit: or even better, flip both horizontally and vertically in single op (same as rotating 180 degrees):
g.drawImage(src, 0, 0, w, h, w, h, 0, 0, null);
Edit2: There is also SWT-specific example of image rotation/flipping without Transform too.