Cannot inherit from final - java

I have a java image analysis program and am trying to get it to run on android. It starts like this (working on standard java):
import java.awt.image.BufferedImage;
public MaskImage(BufferedImage srcImage) {
super(srcImage.getWidth(), srcImage.getHeight(), srcImage.getType());
this.srcImage = srcImage;
this.height = srcImage.getHeight();
this.width = srcImage.getWidth();
}
When I try to get it working on android studio however I change bufferedimage to Bitmap, but still run into a couple of problems:
I cannot extend Bitmap in the same way I could BufferedImage
super(srcImage.getWidth(), srcImage.getHeight(), srcImage.getType());
this line runs into multiple issues, Says:
expected Parameters; actual: Arguments;
Thanks For any Help!

As you already know, Bitmap is final. By the very definition of final classes in Java, this means you cannot subclass it.
You will need to find an alternate strategy for accomplishing whatever you are trying to accomplish. Perhaps creating a class that wraps a Bitmap is one solution.

Related

JAVA CIEXYZ ColorSpace Conversion

Hello i am trying to write my own HSL Colorspace in Java but i ran into an problem with the CIEXYZ conversion...
I have already writen a from/toRGB Method but java requires to write a from/toCIEXYZ method too. I thought i could use the pre-implemented CIEXYZ Colorspace for that...
#Override
public float[] fromCIEXYZ(float[] colorvalue) {
return fromRGB(CIEXYZ.toRGB(colorvalue));
}
but this doesn't work and after some time i figured out that the CIEXYZ colorspace java provides doesnt works like i expected it...
ColorSpace cieXYZ = ColorSpace.getInstance(ColorSpace.CS_CIEXYZ);
System.out.println(Arrays.toString(cieXYZ.toRGB(cieXYZ.fromRGB(new float[]{1,0.5f,0}))));
the result of this small code is [0.9820706, 0.49709317, 0.122087434] and not [1 , 0.5, 0]...
can anybody explain me why?
and how can i fix this?

Referencing Pictures from resource folder in a Jar

I can't help it but think I've missed just the Thread to answer my question but I've been looking for a long time now and can't seem to find the help I need.
My problem is quite simple:
I've created a game (or rather I'm in the process of it) and I'm trying to add sprites (png files for the enemies and such) to it. Loading them in the IDE works just fine but when I create a jar file and try to open it, it simply says "A Java Exception has occurred". Further investigation revealed the problem is that it can't find the files I told it to load.
Through the threads I've read I gathered this much:
It's either that I'm not loading the images properly, meaning that I don't use the proper code for it, or
my MANIFEST.mf does not contain a "Class-Path" (meaning that the arguments are blank, which in fact they are)
Finding other code didn't work out for me. People suggested to use a ClassLoader which I could't manage to get working. Trying it out gave me nullpointer exceptions.
Trying to look into the latter didn't do much help, because I couldn't find any helping information, or I'm just not understanding anything.
package platformer.resources;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
public class ImageLoader
{
static String spritePath = "src/platformer/resources/";
public static BufferedImage loadImage(String path)
{
BufferedImage img = null;
try{img= ImageIO.read(new File(spritePath+path));
}catch(IOException e){System.out.println("Couldn't load File: "+spritePath+path);}
return img;
}
public static BufferedImage flipImage(BufferedImage bufferedImage)
{
AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
tx.translate(-bufferedImage.getWidth(null), 0);
AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
bufferedImage = op.filter(bufferedImage, null);
return bufferedImage;
}
}
This is the code I used so far.
Other classes would use "loadImage" or "flipImage" and in the IDE it works fine but as a .jar it fails to find the .png.
For example the class "platformer/entities/walker.class" would try to open the "Idle.png". In order to do that it uses the "loadImage("/entities/enemy/walker/Idle.png")" method. Note that in the method the actual path would end up being "src/platformer/resources/entities/enemy/walker/Idle.png".
Again, I'm terribly sorry if this has already been answered but I appreciate your help nonetheless.
Michael
When your files are inside a jar they can only be taken out as a resource stream so you will want to use something like:
ImageLoader.class.getResourceAsStream("nameoffile").
This is because a jar is actually a zipped up directory structure and files are not really files they are hidden amongst a compressed zip formatting as binary.

How to extract programmatically video frames?

I need programmatically extract frames from mp4 video file, so each frame goes into a separate file. Please advise on a library that will allow to get result similar to the following VLC command (http://www.videolan.org/vlc/):
vlc v1.mp4 --video-filter=scene --vout=dummy --start-time=1 --stop-time=5 --scene-ratio=1 --scene-prefix=img- --scene-path=./images vlc://quit
Library for any of these Java / Python / Erlang / Haskell will do the job for me.
Consider using the following class by Popscan. The usage is as follows:
VideoSource vs = new VideoSource("file://c:\test.avi");
vs.initialize();
...
int frameIndex = 12345; // any frame
BufferedImage frame = vs.getFrame(frameIndex);
I would personally look for libraries that wrap ffmpeg/libavcodec (the understands-most-things library that many encoders and players use).
I've not really tried any yet so can't say anything about code quality and ease, but the five-line pyffmpeg example suggests it's an easy option - though it may well be *nix-only.

How to make a resource loader that will work with Slick2D

so i have made a 2D game using slick however the only resource loader that i have ever made is for plain java and it went like this :
import java.awt.Image;
import java.awt.Toolkit;
public class ResourceLoader {
static ResourceLoader rl = new ResourceLoader();
public static Image getImage(String fileName){
return Toolkit.getDefaultToolkit().getImage(rl.getClass().getResource("images/" + fileName));
}
}
Slick, of course, has a different image type (org.newdawn.slick.Image) for which this Resource Loader Class will not work and so i do not know how, for when i export my project , to load resources. I have searched online but haven't found many detailed results and so i ask is there any way to load resources so when i export the project as a run able jar file it will still load them.
thanks for any helpful answers in advance - josh
Not sure if you've seen it but here's one way of doing this. Explains deferred loading:
http://slick.cokeandcode.com/wiki/doku.php?id=resource_manager_tutorial
You might want to check out the other tutorials by Spiegel if you're starting out with Slick2D. I've found them very useful

google.zxing barcode generator in iReport

I want put a barcode in my page and can preview it. The barcode generator is google.zxing and my reporting tool is iReport.
But i dont know, how to configure Image Expression and Expression Class of an image in iReport.
The two key ideas are first to write a bit of Java code to create the relevant image and then to design the report to reference this code appropriately. Perhaps the simplest way to generate the image is in a scriptlet like this:
package com.jaspersoft.alliances.mdahlman;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
public class QRCodeScriptlet extends JRDefaultScriptlet {
public void afterDetailEval() throws JRScriptletException {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = null;
try {
matrix = writer.encode(getFieldValue("barcode_text").toString(), BarcodeFormat.QR_CODE, 256, 256);
this.setVariableValue("BarCodeImage", MatrixToImageWriter.toBufferedImage(matrix) );
} catch (WriterException e) {
e.printStackTrace();
}
}
}
That's full of hard-coded ugliness, but the key ideas are all shown. Then you need to define the report like this:
Sample query: select 'some text' as barcode_text
I included this only to reinforce the point that my scriptlet hard-codes the field name barcode_text. (This is bad.)
Variable: BarCodeImage of type java.awt.image.BufferedImage with calculation System.
This name is hard-coded in the scriptlet too. (This is equally bad.)
Add to iReport's classpath:
The compiled scriptlet .jar file
core.jar (from ZXing)
javase.jar (from ZXing)
Add an Image element to the report with Expression $V{BarCodeImage}.
The result is a happy happy QR-code in your generated JasperReport:
I recall a sample that I have seen which does things much more cleanly. It actually included a nice plug-in so you could easily install this functionality into iReport with minimal effort. If I can track that down, then I'll update this post. But until then this at least covers all of the critical points.
The image expression should return any subclass of java.awt.Image. The easiest way to achieve this is to use your own helper class to generate the Image. You can create a static method that generates a barcode from a Stringand call that method from IReport.
In the case of ZXing I don't know the method to use, but I can tell what I use as ImageExpression using the Barbecue library.
net.sourceforge.barbecue.BarcodeImageHandler.getImage(
MyBarcodeGenerator.getFromString($F{field})
MyBarcodeGenerator class contains the method getFromString(...) that returns a net.sourceforge.barbecue.Barcode in my case a net.sourceforge.barbecue.linear.code39.Code39Barcode
The Expression Class is ignored.
--Edited:
To encode an Image in zxing you should use MatrixToImageWriter
The following code will encode a QRCode into a BufferedImage which you can use in the Image Expression field:
MatrixToImageWriter.toBufferedImage(new QRCodeWriter().encode("BARCODE CONTENT", BarcodeFormat.QR_CODE, 400 /*Width*/, 400/*Height*/));

Categories