Phil Mood Posted March 10, 2019 Posted March 10, 2019 Hi, I use the CropImage function to insert an image from a file into a container field, Base64Decode ( Base64Encode ( CropImage( $importpath ; myTable::img_X ; myTable::img_Y ; myTable::img_pic_width ; myTable::img_pic_height ) ) ; myTable::img_destination_fieldname ) It works perfectly but, if the XY coordinates are negatives, the background that fills the empty space is set automatically to black. Ex: Base64Decode ( Base64Encode ( CropImage( $importpath ; -200 ; -75 ; 500 ; 250 ) ) ; myTable::img_destination_fieldname ) https://punto-rosso.d.pr/GHn8Vv I use negative coordinates to position the picture inside the container. How I can avoid the black background? Or at least set it to white/transparent. Thanks Small update: the problem exist even with positive values. Ex. Base64Decode ( Base64Encode ( CropImage( $importpath ; 25 ; 100; 500 ; 250 ) ) ;myTable::img_destination_fieldname ) https://punto-rosso.d.pr/u505b6 It seems that the Crop function simply add automatically a black background to fill any empty space.
john renfrew Posted March 11, 2019 Posted March 11, 2019 (edited) For JPG Add this at the top import java.awt.Color Then add //add these d.setBackground(Color.WHITE) //or choose any other Color you prefer //see https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html d.clearRect(0, 0, widthInt, heightInt) //before this d.drawImage(img, xInt*-1, yInt*-1, null) This line/param BufferedImage.TYPE_INT_RGB says it is an RGB image If your source has transparency then you want to be using BufferedImage.TYPE_INT_ARGB, but the code to get/create transparency where you have a negtive x or y is a little more difficult... Edited March 11, 2019 by john renfrew
Phil Mood Posted March 11, 2019 Author Posted March 11, 2019 Hi John, thats perfect! The ARGB argument did the trick. A genius strike! Thanks a lot for your help. Best
Phil Mood Posted March 11, 2019 Author Posted March 11, 2019 Hi John, I am trying to obtain the same result with the function "Rotate Image" but somehow your trick doesn't work here. Any help is really appreciated. Thanks
john renfrew Posted March 12, 2019 Posted March 12, 2019 Are you trying to rotate and resize at the same time?? Surely when you rotate an image there is no 'spare' to be cast as transparent? If you rotate a 400*300px image by 90degrees then you get a 300*400px image, or are you trying to rotate by other angles??
Phil Mood Posted March 12, 2019 Author Posted March 12, 2019 Jus rotate. The problem is that all transparent areas of the image get lost when using the standard parameters and, when using .png, they turn to black. I have made a small screencast example to show https://punto-rosso.d.pr/C4H6Ul Thanks
john renfrew Posted March 12, 2019 Posted March 12, 2019 Here is a new function for rotation of PNG files, which should fix your issue, if you pass it a file with no transparency or alphait returns an error to you If you do SMGetVariable("cm") after running it you will see the color model Java thinks the file has // RotatePNG ( imgLocation ; degrees ) // v1.0 // 19_03_12 JR import javax.imageio.ImageIO import java.awt.image.BufferedImage import java.awt.Graphics2D import java.awt.geom.AffineTransform import java.awt.RenderingHints isTransparent = {image, x, y -> pixel = image.getRGB(x,y) return (pixel>>24) == 0x00 } containsTransparency = { image -> for (i in 0..<image.getHeight()) { for (j in 0..<image.getWidth()) { if (isTransparent(image, j, i)){ return true } //end if } //end for } //end for } //end containsTransparency containsAlphaChannel ={ image -> return image.getColorModel().hasAlpha() }//end containsAlphaChannel degrees = Double.parseDouble(degrees) radians = Math.toRadians(degrees) isRightAngle = degrees % 90 == 0 BufferedImage img if (imgLocation.indexOf("://") != -1) { img = ImageIO.read(new URL(imgLocation)) } else { img = ImageIO.read(new File(imgLocation)) } cm = img.getColorModel() //check for transparency try{ isAlpha = containsAlphaChannel(img) if ( !isAlpha) { isTransparent = containsTransparency(img) if ( !isTransparent){ return 'ERROR - no transparency' }//end if return 'ERROR - no transparency' } //end if } catch (e) { return 'ERROR - unspecified' } int newWidth = Math.abs(Math.cos(radians) * img.getWidth()) + Math.abs(Math.sin(radians) * img.getHeight()) int newHeight = Math.abs(Math.cos(radians) * img.getHeight()) + Math.abs(Math.sin(radians) * img.getWidth()) at = AffineTransform.getRotateInstance(radians, img.getWidth()/2, img.getHeight()/2) type = BufferedImage.TYPE_INT_ARGB dest = new BufferedImage(newWidth, newHeight, type) g2d = dest.createGraphics() if (!isRightAngle) { // enable antialiasing for odd angles g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } g2d.setTransform(AffineTransform.getTranslateInstance((newWidth - img.getWidth()) / 2, (newHeight - img.getHeight()) / 2)) g2d.drawImage(img, at, null) return dest So only two parameters as you don't care about background colour 1
Phil Mood Posted March 12, 2019 Author Posted March 12, 2019 Hi John, you're the best! A big, big thanks, I really appreciate your help. Have a nice day.
Recommended Posts
This topic is 2093 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now