I am creating custom SWT widget but I have got problem with transparency. My class extends canvas, I have got png with alpha image in my resources, when I simply write:
this.setBackgroundImage(Colors.getMenuButton()); //getMenuButton returns Image object
everything works ok (with transparency), but my object has to be resizeable so I decided to create funcion:
protected Image BGHelper(Image src) {
Image i2 = new Image(Display.getDefault(),2,26);
GC gc2 = new GC(i2);
Image image = new Image(Display.getDefault(),this.getBounds().width,26);
GC gc = new GC(image);
gc.drawImage(src, 0, 0, 3, 25, 0, 0, 3, 26);
gc2.drawImage(src, 3, 0, 2, 25, 0, 0, 2, 26);
gc.drawImage(i2, 0, 0, 2, 25, 3, 0, this.getBounds().width-6, 26);
gc.drawImage(src,53, 0, 3, 26, this.getBounds().width-3, 0, 3, 26);
gc.dispose();
gc2.dispose();
return image;
}
it cuts left border from source and paste into result, cut center from source, resize and paste into result, cut right border from source and paste into result. Resizing works but there is no transparency (white pixel). Why?
please look at following doc.It might help you to solve your issue.
http://www.eclipse.org/articles/Article-SWT-images/graphics-resources.html#Transparency
Related
I have working code in Visual Studio with C ++ on a watershed algorithm and I need to pass it to Android Studio. I have added the OpenCV library to the app and the simple operations work. Now, my problem is that when I want to pass the code to Java there is a specific line type that I don't know how to pass. The part of the code is:
cv::Mat markers(original.size(), CV_8U, cv::Scalar(-1));
//Rect(topleftcornerX, topleftcornerY, width, height);
//top rectangle
markers(Rect(0, 0, original.cols, 5)) = Scalar::all(1);
//bottom rectangle
markers(Rect(0, original.rows - 5, original.cols, 5)) = Scalar::all(1);
//left rectangle
markers(Rect(0, 0, 5, original.rows)) = Scalar::all(1);
//right rectangle
markers(Rect(original.cols - 5, 0, 5, original.rows)) = Scalar::all(1);
What would be the creation of the object would be something like this:
Mat markers = new Mat(rgba.size(), CvType.CV_8UC3, new Scalar(-1));
But the rest of the lines do not know how they would be transformed. Help please?
markers.submat(0, 0, original.cols, 5).setTo(new Scalar(1, 1, 1));
or
Imgproc.rectangle (
markers,
new Point(0, 0),
new Point( original.cols, 5),
new Scalar(1, 1, 1),
-1
);
I have a 1x1 jpg image that consists of a single red colour.
I create a bitmap from the drawable image (Android) in this way:
Bitmap bitmap_image = BitmapFactory.decodeResource(getResources(), R.drawable.onebyonered);
However, calling getWidth() and getHeight() on the bitmap outputs 3, 3.
I then convert the bitmap to a OpenCV matrix:
Mat matrix = new Mat(1, 1, CvType.CV_64FC1);
// System.out.println(matrix);
Utils.bitmapToMat(bitmap_image, matrix);
// System.out.println(matrix);
System.out.println(matrix.dump());
The first print statement returns Mat [ 1*1*CV_64FC1, ..., the second print statement returns Mat [ 3*3*CV_8UC4, ... and the matrix dump (converting to string) shows:
[255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255;
255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255;
255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255]
Grayscaling it using:
Imgproc.cvtColor(matrix, matrix, Imgproc.COLOR_RGBA2GRAY);
Produces a 3x3x1 instead of the 1x1x1 expected:
[ 76, 76, 76;
76, 76, 76;
76, 76, 76]
Given the height and width mentioned above and assuming the dump is supposed to be in an RGBA format - why are 9 different values needed to represent a 1 pixel single colour image? And how can I change this 3x3x1 into the 1x1x1 that I want [76]? Ideally whatever conversion technique I use should be reversible or I'm able to display the bitmap image.
calling getWidth() and getHeight() returning 3x3 because of the system is scaling your image as per device density so it's returning the wrong size. Put image in assets and load from there of or put it inside drawable-nodpi folder.
I've seen similar questions before but none helped me.
The player in my game can go to the right forever until he dies, but he can also go back.
I have a wall texture that I need to repeat forever on the top and bottom of the screen. I've seen answers where they say you need to put different values when drawing the texture at srcX, srcY, srcWidth and srcHeight, but when I do that the texture doesn't look anyting like the original. I set the texture to repeat with wallTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat)
The code I have to draw the wall on the top and bottom of the screen:
spriteBatch.draw(wallTexture, -Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT - Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH / 2, Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH, Constants.WALL_HEIGHT, 1, 1, 0, 0, 0, 720, 64, false, false);
spriteBatch.draw(wallTexture, -Constants.WORLD_WIDTH, -Constants.WORLD_HEIGHT - Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH / 2, Constants.WALL_HEIGHT / 2, Constants.WALL_WIDTH, Constants.WALL_HEIGHT, 1, 1, 0, 0, 0, 720, 64, false, false);
Thanks
I'm implementing sobel edge detection algo. After processing I apply new pixel values, either 255 or 0. my problem is that the resulting bitmap is not showed in the imageView. I'm using Alpha_8 Configuration because it takes less memory and most suitable for edge results.
How can I preview the results?
The ALPHA_8 format was made to be used as a mask because it only contains alpha and no color information. You should convert it to another format or if you want to use it anyway then you should put it as a mask for background for example. Check out this response from an Android project member to a similar issue. You can also check out my response to another question, there I put a code example about how to apply a mask to another bitmap.
You can use ColorMatrixColorFilter to display ALPHA_8 bitmap.
For example, use the following matrix
mColorFilter = new ColorMatrixColorFilter(new float[]{
0, 0, 0, 1, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 255
});
to draw your bitmap using RED color.
public void onDraw(Canvas canvas){
mPaint.setColorFilter(mColorFilter);
canvas.drawBitmap(mBitmap,x,y,mPaint);
}
I'm trying to find a faster way of converting RGB to YUV using the Android SDK as the standard pixel for pixel methods are pretty slow in Java. ColorMatrices seem to be pretty efficient and I see there's a setRGB2YUV() method but I can't find any examples and the documentation simply says "Set the matrix to convert RGB to YUV" which is completely useless as usual.
Here's part of my initialization code, which is a little compicated slightly with arrays due to multithreading:
cacheBitmaps = new Bitmap[NumberOfThreads];
cacheCanvas = new Canvas[NumberOfThreads];
mRGB2YUV = new ColorMatrix();
cmfRGB2YUV = new ColorMatrixColorFilter(mRGB2YUV);
pRGB2YUV = new Paint();
pRGB2YUV.setColorFilter(cmfRGB2YUV);
for (int m=0; m< NumberOfThreads; m++) {
cacheBitmaps[m] = Bitmap.createBitmap(widthX,heightY, Config.ARGB_8888);
cacheCanvas[m] = new Canvas(cacheBitmaps[m]);
}
Later I use this to paint an RGB to a canvas with the specified paint:
cacheCanvas[n].drawBitmap(fb.frames[n].getAndroidBitmap(),0,0, pRGB2YUV);
I've also experimented using a standard matrix that shouldn't apply any changes to the RGB values like this:
float[] matrix = {
0, 1, 0, 0, 0, //red
0, 0, 1, 0, 0, //green
0, 0, 0, 1, 0, //blue
0, 0, 0, 0, 1 //alpha
};
mRGB2YUV.set(matrix);
Whatever I do I either get black, green or distorted frames in my output video (using JavaCV with FFMPEG and specifying AV_PIX_FMT_NV21 as a color format after copying the final bitmap to an IplImage).
Anyone know how to use this and if it's even possible or does what it says it does?