SVG image within a Batik JSVGCanvas is scaled down after rotation and setting bounding box - apache

I have a problem with the rotation within a JSVGCanvas.
The user can load SVG images into a document page (JLayeredPane).
Every image displays in his own JSVGCanvas and has his own class.
The user can resize the image by dragging the endpoints and also move the image around.
The rotation works perfectly
(Basic and rotated situation)
Only the subsequent change of the bounding box of the Canvas results in a scaling, which no longer corresponds to the original size
(Finally after setting Boundingbox)
Code fragment in UpdateManager():
....
// calculating new canvas bounding box
AffineTransform at = AffineTransform.getRotateInstance(rotation*Math.PI/180.0,
originalX + originalW/2.0,
originalY + originalH/2.0);
Rectangle2D.Double rect = new Rectangle2D.Double(originalX, originalY, originalW, originalH);
Shape s = at.createTransformedShape(rect);
xx = s.getBounds2D().getX();
yy = s.getBounds2D().getY();
ww = s.getBounds2D().getWidth();
hh = s.getBounds2D().getHeight();
canvas.setRenderingTransform(AffineTransform.getRotateInstance(rotation*Math.PI/180.0,
originalW/2.0, originalH/2.0));
// this.setBounds() will do the stuff and also change the Canvas Bounds
setBounds(xx, yy, ww, hh);
....
I am grateful for any help.

Solved...
I have found the way to solve this problem:
....
// calculating new canvas bounding box
AffineTransform at = AffineTransform.getRotateInstance(rotation*Math.PI/180.0,
originalX + originalW/2.0,
originalY + originalH/2.0);
Rectangle2D.Double rect = new Rectangle2D.Double(originalX, originalY, originalW, originalH);
Shape s = at.createTransformedShape(rect);
xx = s.getBounds2D().getX();
yy = s.getBounds2D().getY();
ww = s.getBounds2D().getWidth();
hh = s.getBounds2D().getHeight();
double rotScale = originalW/ww;
double diffX = (originalW * rotScale - originalW) / 2.0 * -1.0;
double diffY = (originalH * rotScale - originalH) / 2.0 * -1.0;
AffineTransform af = AffineTransform.getScaleInstance(rotScale, rotScale);
af.preConcatenate(AffineTransform.getTranslateInstance(diffX, diffY));
af.preConcatenate(AffineTransform.getRotateInstance(rotation*Math.PI/180.0, originalW/2.0, originalH/2.0));
canvas.setRenderingTransform(af);
// this.setBounds() will do the stuff and also change the Canvas Bounds
setBounds(xx, yy, ww, hh);
....
Maybe this will help others having the same problem.
Picture solved

Related

The code/command to determine/change the length/height ratio of scale bar?

I would like to adjust the length/height ratio of the scale bar in an image by the DM scripting. As the following code shown, I can adjust the font size by changing "scalebar.componentsetfontinfo("Microsoft Sans Serif", 0, fontsize)", but I do not know how to change the shape (length/height ratio) of rectangle which constitute to the scale bar. Is there any code/command can achieve this? Thanks,
image front:=getfrontimage()
imagedisplay imgdisp=front.imagegetimagedisplay(0)
number nobar=imgdisp.componentcountchildrenoftype(31)
number fontsize=20
imgdisp.applydatabar(0)
component scalebar=imgdisp.componentgetnthchildoftype(31,0)
scalebar.componentsetfontinfo("Microsoft Sans Serif", 0, fontsize)
scalebar.componentsetdrawingmode(1)
When you select a scalebar, you will notice the green points showing that component's rectangle.
You control that rectangle like you control any component rectangle:
number kScaleBar = 31
image img := GetFrontImage()
imageDisplay disp = img.ImageGetImageDisplay(0)
component scaleBar = disp.ComponentGetNthChildOfType( kScaleBar, 0 )
number t,l,b,r
scaleBar.ComponentGetRect(t,l,b,r)
Result("\n Current rect: [" + t + "/" + l + "/" + b + "/" + r )
number sx, sy
img.GetSize( sx, sy )
// Set centered half at bottom
l = sx*1/4
r = sx*3/4
t = sy*17/20
b = sy*19/20
scaleBar.ComponentSetRect(t,l,b,r)
Note, that you can not explicitly specify the length of the scalebar, it will always "snap" to a meaningful fraction within the boundary box, depending on the image calibration.
Setting the bounding rect will also override the font-size, while setting the font-size will override the boundary rect's height.

How to resize font in plot_net feature of phyloseq?

I want to resize my text in plot_net but none of the options are working for me. I am trying
p <- plot_net(physeqP, maxdist = 0.4, point_label = "ID", color = "Cond", shape = "Timeperiod") p + geom_text(size=15)
This gives me error
"Error: geom_text requires the following missing aesthetics: x, y,
label".
Can anyone please tell me how can I fix the issue?
I dont want to resize legends or the axis, but the nodes text.
this image is drawn using phyloseq but since the font size is very small, i want to make it prominent.
Without an example it's hard to reproduce.
p <- plot_net(physeqP, maxdist = 0.4, point_label = "ID"
, color = "Cond", shape = "Timeperiod", cex_val = 2)
I believe this is with the NeuralNetTools package.
Try using: cex_val numeric value indicating size of text labels, default 1
https://www.rdocumentation.org/packages/NeuralNetTools/versions/1.5.0/topics/plotnet

Calculte new width and height of the video based on the original width, height and ratio

After searching through - I wasn't able to find the answer so I'll give it a shot here.
I need to resize desktop video in order to feet it on mobile screen, let's say original width of the video was 1915 and original height was 1075, I calculated aspect ratio:
aspectRatio = (width/height); aspectRatio = 1.78;
Now my mobile screen resolution is: height = 1609, width = 1080.
How can I properly resize my video in order to keep the same aspect ratio??
Thank you
aspectRatio = (width/height)
You always want aspectRatio to be 1.78, if you want to prevent stretching or cropping.
And, the max new height is 1609 and the max new width is 1080, so:
1.78 = (1080/height)
height = 1080/1.78 = 606.74...
OR
1.78 = (width/1609)
width = 1.78*1609 = 2864.02...
So, you can have 1080x606.74 (which fits on screen), or 2864.02x1609 (which doesn't fit).
So, your answer is 1080x606.74...
Let's original dimensions are Wo, Ho, and target screen dimensions are Wt, Ht.
Rectangle with predetermined aspect will fit vertically if ratio of target and initial heights less than ratio of its widths, and will fit horizontally otherwise:
Coeff = Min(Wt/Wo, Ht/Ho)
W_Result = Wo * Coeff
H_Result = Ho * Coeff
or
if (Wt * Ho < Ht * Wo) then
W_Result = Wt
H_Result = Ho * Wt / Wo
else
W_Result = Wo * Ht / Ho
H_Result = Ht

Image scale from center of display using matrix

every one.
I'm a android developer.
I want to scale my image from center of displayed part of image with matrix.
So, I scaled my image with matrix. And then moved it with the calculated pointer.
But, the Application not work correctly.
This can't find the correct center, so when it does, it moved right.
Why this is?
I can't find the problem.
The code followed.
matrix.reset();
curScale += 0.02f;
orgImage.getHeight();
w = orgImage.getWidth();
matrix.postScale(curScale, curScale);
rtnBitmap = Bitmap.createBitmap(orgImage, 0, 0, w, h, matrix, true);
curImageView.setImageBitmap(rtnBitmap);
Matrix curZoomOutMatrix = new Matrix();
pointerx =(int ) ((mDisplayWidth/2 - curPosX) * curScale);
curPosX = - pointerx;
pointery =(int ) ((mDisplayWidth/2 - curPosY) * curScale);
curPosY = - pointery;
Log.i("ZoomOut-> posX = ", Integer.toString(curPosX));
Log.i("ZoomOut-> posY = ", Integer.toString(curPosY));
curZoomOutMatrix.postTranslate(curPosX, curPosY);
curImageView.setImageMatrix(curZoomOutMatrix);
curImageView.invalidate();
Did you have any sample code for center zoomIn and zoomOut the imageView with matrix?
Who can explain for that?
Please help me.
Or, It's my fault.
First, I scale the image from original one.
So, The image is (width, height) * scale;
Then I calculate the absolute position of the point that is displayed center. And then, move my ImageView to the calculated position from which the view is. My fault are here.
When i calculate view position, I change the position from now scale.
So, when it scaled, the position is not <original position> * <now scale>. It was <original position * <scale> * <now scale>, the result was strange position.
So i remade add to calculate the center position from original one.
That mode is now following.
public void calculate(float offset) {
float tmpScale = curScale - offset;
float orgWidth = (mDisplayWidth / 2 - curPosX) / tmpScale;
float orgHeight = (mDisplayHeight / 2 - curPosY) / tmpScale;
int tmpPosX = (int)(mDisplayWidth / 2 - orgWidth * curScale);
int tmpPosY = (int)(mDisplayHeight / 2 - orgHeight * curScale);
curPosX = tmpPosX;
curPosY = tmpPosY;
Matrix matrix = new Matrix();
matrix.postTranslate(tmpPosX, tmpPosY);
curImageView.setImageMatrix(matrix);
curImageView.invalidate();
}
Thank you. every one.

How do I use the scanCrop property of a ZBar reader?

I am using the ZBar SDK for iPhone in order to scan a barcode. I want the reader to scan only a specific rectangle instead of the whole view, for doing that it is needed to set the scanCrop property of the reader to the desired rectangle.
I'm having hard time with understanding the rectangle parameter that has to be set.
Can someone please tell me what rect should I give as an argument if on portrait view its coordinates would be: CGRectMake( A, B, C, D )?
From the zbar's ZBarReaderView Class documentation :
CGRect scanCrop
The region of the video image that will be scanned, in normalized image coordinates. Note that the video image is in landscape mode (default {{0, 0}, {1, 1}})
The coordinates for all of the arguments is in a normalized float, which is from 0 - 1. So, in normalized value, theView.width is 1.0, and theView.height is 1.0. Therefore, the default rect is {{0,0},{1,1}}.
So for example, if I have a transparent UIView named scanView as a scanning region for my readerView. Rather than do :
readerView.scanCrop = scanView.frame;
We should do this, normalizing every arguments first :
CGFloat x,y,width,height;
x = scanView.frame.origin.x / readerView.bounds.size.width;
y = scanView.frame.origin.y / readerView.bounds.size.height;
width = scanView.frame.size.width / readerView.bounds.size.width;
height = scanView.frame.size.height / readerView.bounds.size.height;
readerView.scanCrop = CGRectMake(x, y, width, height);
It works for me. Hope that helps.
You can use scan crop area by doing this.
reader.scanCrop = CGRectMake(x,y,width,height);
for eg.
reader.scanCrop = CGRectMake(.25,0.25,0.5,0.45);
I used this and its working for me.
come on!!! this is the right way to adjust the crop area;
I had wasted tons of time on it;
readerView.scanCrop = [self getScanCrop:cropRect readerViewBounds:contentView.bounds];
- (CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)rvBounds{
CGFloat x,y,width,height;
x = rect.origin.y / rvBounds.size.height;
y = 1 - (rect.origin.x + rect.size.width) / rvBounds.size.width;
width = rect.size.height / rvBounds.size.height;
height = rect.size.width / rvBounds.size.width;
return CGRectMake(x, y, width, height);
}