I am trying to develop a Web-Client based Online game using GWT.
The game mechanics work quite fine by now and I'd really like to go on with my next step in my development plan, but now I am stuck with a simple encoding/decoding function I created to store my data for the client into a string.
The steps I took are not that complicated. During runtime, my server creates "ImageStates" of the game Objects to be drawn on a Canvas on my client. Each ImageState contains a number of Layers, which contain Details of what has to be drawn where at what specific time and so on.
I tried ObjectInput/OutputStream, combined with GZip-Compression and ByteInputStreams, but I could not find any GWT implementation of the first two so I had to think about another solution and came across gwt-lzma.
My goal is to encode those details into a single string, compress it on the server, send the compressed Data to the client, decompress and decode it there so it can be "read" by the client. To do so, I created an encoding and a decoding function:
The encoder:
public static String encodeImageStateContainer(HashMap<String,HashMap<Long,ImageState>> imageStateContainer){
StringBuilder mainStringBuilder = new StringBuilder();
Iterator<Entry<String,HashMap<Long,ImageState>>> imageStateContainerIterator = imageStateContainer.entrySet().iterator();
while(imageStateContainerIterator.hasNext()){
Entry<String,HashMap<Long,ImageState>> tempImageStateManagerMetadata = imageStateContainerIterator.next();
HashMap<Long,ImageState> tempImageStateManager = tempImageStateManagerMetadata.getValue();
if(tempImageStateManager.size() > 0){
if(mainStringBuilder.length() > 0){
mainStringBuilder.append('}'); //Divisor between Image State Managers
}
mainStringBuilder.append(tempImageStateManagerMetadata.getKey());
mainStringBuilder.append('|'); //Divisor between Name and following Data
StringBuilder subOneStringBuilder = new StringBuilder();
Iterator<Entry<Long,ImageState>> tempImageStateManagerIterator = tempImageStateManager.entrySet().iterator();
while(tempImageStateManagerIterator.hasNext()){
Entry<Long,ImageState> tempImageStateMetaData = tempImageStateManagerIterator.next();
if(subOneStringBuilder.length() > 0){
subOneStringBuilder.append(')'); //Divisor between Image Objects
}
subOneStringBuilder.append(tempImageStateMetaData.getKey());
subOneStringBuilder.append('-'); //Divisor between Object Id and Image State Data
StringBuilder subTwoStringBuilder = new StringBuilder();
ImageState tempImageState = tempImageStateMetaData.getValue();
subOneStringBuilder.append(tempImageState.getFirstFrameId()); //1. => First Frame Id
subOneStringBuilder.append(';'); //Divisor between Image State data types
subOneStringBuilder.append(tempImageState.getFramesPerLayer()); //2. => Total Frame Count
subOneStringBuilder.append(';');
subOneStringBuilder.append(tempImageState.getMinumimScaleFactor()); //3. => Minimum Scale Factor
subOneStringBuilder.append(';');
ImageStateLayer[] tempImageStateLayers = tempImageState.getImageStateLayers();
for(int layerId = 0; layerId < tempImageStateLayers.length; ++layerId){
if(subTwoStringBuilder.length() > 0){
subTwoStringBuilder.append('/'); //Divisor between Image State Layers
}
//Frame Arrays
String[] imageNativePath = tempImageStateLayers[layerId].getImageNativePath();
short[] offsetX = tempImageStateLayers[layerId].getOffsetX();
short[] offsetY = tempImageStateLayers[layerId].getOffsetY();
short[] orientation = tempImageStateLayers[layerId].getOrientation();
//Finalization Arrays
byte[] imagePathChange = tempImageStateLayers[layerId].getImagePathChange();
byte[] offsetXChange = tempImageStateLayers[layerId].getOffsetXChange();
byte[] offsetYChange = tempImageStateLayers[layerId].getOffsetYChange();
byte[] orientationChange = tempImageStateLayers[layerId].getOrientationChange();
//Image Path Data
StringBuilder subThreeStrignBuilder = new StringBuilder();
for(int imagePathId = 0; imagePathId < imageNativePath.length; ++imagePathId){
if(subThreeStrignBuilder.length() > 0){
subThreeStrignBuilder.append('#'); //Divisor between Frame Data Sets
}
subThreeStrignBuilder.append(imageNativePath[imagePathId]);
subThreeStrignBuilder.append(','); //Divisor between Frame Data Set Data
subThreeStrignBuilder.append(imagePathChange[imagePathId]);
}
subTwoStringBuilder.append(subThreeStrignBuilder.toString());
subTwoStringBuilder.append('['); //Divisor between Frame Data Types
//OffsetX Data
subThreeStrignBuilder = new StringBuilder();
for(int offsetXId = 0; offsetXId < offsetX.length; ++offsetXId){
if(subThreeStrignBuilder.length() > 0){
subThreeStrignBuilder.append('#'); //Divisor between Frame Data Sets
}
subThreeStrignBuilder.append(offsetX[offsetXId]);
subThreeStrignBuilder.append(','); //Divisor between Frame Data Set Data
subThreeStrignBuilder.append(offsetXChange[offsetXId]);
}
subTwoStringBuilder.append(subThreeStrignBuilder.toString());
subTwoStringBuilder.append('['); //Divisor between Frame Data Types
//OffsetY Data
subThreeStrignBuilder = new StringBuilder();
for(int offsetYId = 0; offsetYId < offsetY.length; ++offsetYId){
if(subThreeStrignBuilder.length() > 0){
subThreeStrignBuilder.append('#'); //Divisor between Frame Data Sets
}
subThreeStrignBuilder.append(offsetY[offsetYId]);
subThreeStrignBuilder.append(','); //Divisor between Frame Data Set Data
subThreeStrignBuilder.append(offsetYChange[offsetYId]);
}
subTwoStringBuilder.append(subThreeStrignBuilder.toString());
subTwoStringBuilder.append('['); //Divisor between Frame Data Types
//Orientation Data
subThreeStrignBuilder = new StringBuilder();
for(int orientationId = 0; orientationId < orientation.length; ++orientationId){
if(subThreeStrignBuilder.length() > 0){
subThreeStrignBuilder.append('#'); //Divisor between Frame Data Sets
}
subThreeStrignBuilder.append(orientation[orientationId]);
subThreeStrignBuilder.append(','); //Divisor between Frame Data Set Data
subThreeStrignBuilder.append(orientationChange[orientationId]);
}
subTwoStringBuilder.append(subThreeStrignBuilder.toString());
}
subOneStringBuilder.append(subTwoStringBuilder.toString());
}
mainStringBuilder.append(subOneStringBuilder.toString());
}
}
return mainStringBuilder.toString();
}
The decoder:
public static HashMap<String,HashMap<Long,ImageState>> decodeImageStateContainer(String data){
String[] imageStateManagerArray = data.split("\\}");
HashMap<String,HashMap<Long,ImageState>> imageStateManagerContainer = new HashMap<String,HashMap<Long,ImageState>>(imageStateManagerArray.length);
for(int managerId = 0; managerId < imageStateManagerArray.length; ++managerId){
String[] tempImageStateData = imageStateManagerArray[managerId].split("\\|");
HashMap<Long,ImageState> tempImageStateManager = new HashMap<Long,ImageState>();
imageStateManagerContainer.put(tempImageStateData[0], tempImageStateManager);
String[] tempImageStateManagerObjects = tempImageStateData[1].split("\\)");
for(int objectId = 0; objectId < tempImageStateManagerObjects.length; ++objectId){
String[] tempImageObjectData = tempImageStateManagerObjects[objectId].split("\\-");
long imageObjectId = Long.parseLong(tempImageObjectData[0]);
String[] imageStateMetaData = tempImageObjectData[1].split("\\;");
ImageState tempImageState = new ImageState(Integer.parseInt(imageStateMetaData[1]), Integer.parseInt(imageStateMetaData[0]), Integer.parseInt(imageStateMetaData[2]));
tempImageStateManager.put(imageObjectId, tempImageState);
String[] tempImageStateLayerMetaData = imageStateMetaData[3].split("\\/");
ImageStateLayer[] tempImageStateLayers = new ImageStateLayer[tempImageStateLayerMetaData.length];
for(int layerId = 0; layerId < tempImageStateLayerMetaData.length; ++layerId){
String[] layerElements = tempImageStateLayerMetaData[layerId].split("\\[");
String[] imageNativePathDetails = layerElements[0].split("\\#");
String[] offsetXDetails = layerElements[1].split("\\#");
String[] offsetYDetails = layerElements[2].split("\\#");
**String[] orientationDetails = layerElements[3].split("\\#");**
//Image Path Data
String[] imageNativePaths = new String[imageNativePathDetails.length];
byte[] imagePathChange = new byte[imageNativePathDetails.length];
for(int id = 0; id < imageNativePathDetails.length; ++id){
String[] imagePathDetailElements = imageNativePathDetails[id].split("\\,");
imageNativePaths[id] = imagePathDetailElements[0];
imagePathChange[id] = Byte.parseByte(imagePathDetailElements[1]);
}
//OffsetX Data
short[] offsetX = new short[offsetXDetails.length];
byte[] offsetXChange = new byte[offsetXDetails.length];
for(int id = 0; id < offsetXDetails.length; ++id){
String[] offsetXDetailElements = offsetXDetails[id].split("\\,");
offsetX[id] = Short.parseShort(offsetXDetailElements[0]);
offsetXChange[id] = Byte.parseByte(offsetXDetailElements[1]);
}
//OffsetY Data
short[] offsetY = new short[offsetYDetails.length];
byte[] offsetYChange = new byte[offsetYDetails.length];
for(int id = 0; id < offsetYDetails.length; ++id){
String[] offsetYDetailElements = offsetYDetails[id].split("\\,");
offsetY[id] = Short.parseShort(offsetYDetailElements[0]);
offsetYChange[id] = Byte.parseByte(offsetYDetailElements[1]);
}
//Orientation Data
short[] orientation = new short[orientationDetails.length];
byte[] orientationChange = new byte[orientationDetails.length];
for(int id = 0; id < orientationDetails.length; ++id){
String[] orientationDetailElements = orientationDetails[id].split("\\,");
orientation[id] = Short.parseShort(orientationDetailElements[0]);
orientationChange[id] = Byte.parseByte(orientationDetailElements[1]);
}
//Create the Layer and Add it to the Array
tempImageStateLayers[layerId] = new ImageStateLayer(imageNativePaths,new short[][]{offsetX,offsetY,orientation}, new byte[][]{imagePathChange,offsetXChange,offsetYChange,orientationChange});
}
//Connect the Reference to the layers with the Image State
tempImageState.setImageStateLayers(tempImageStateLayers);
}
}
return imageStateManagerContainer;
}
Now my problem: If I encode it on the server and decode it directly afterwards It takes about three cycles of Encoding until I get an IndexOutofBoundsException at this specific position in the decoder, the last line is the line where the error seems to be according to stacktrace:
for(int layerId = 0; layerId < tempImageStateLayerMetaData.length; ++layerId){
String[] layerElements = tempImageStateLayerMetaData[layerId].split("\\[");
String[] imageNativePathDetails = layerElements[0].split("\\#");
String[] offsetXDetails = layerElements[1].split("\\#");
String[] offsetYDetails = layerElements[2].split("\\#");
String[] orientationDetails = layerElements[3].split("\\#");
I would not say I'm an expert, but after testing the encoder I can definitely say that the string generated by it is always valid, the string at which I get this error is complete as well. There must be something wrong with my decoding function, but I have no idea what it could be.
As I said, the first three cycles are encoded and decoded properly and the data to encode did not change dramatically. There is no way that the encoded string could lead to having a String array smaller than 4 elements. Basically, this error can't exist as far as I understand my code.
I think it could be some kind of memory allocation problem that prevents the string from being split properly, but thats just an idea of a clueless programmer.
Any help is deeply appreciated!
I found the solution to my problem:
While encoding, I was printing Numeric types to my string. While selecting my Seperator symbols I didn't take negative values, resulting in a '-' before the digit into consideration, which led to an uncontrolled splitting of my String when I tryed to split by '-'. I replaced the minus with another character and everything works fine now!
Related
I am trying to implement face recognition in my photo database using JavaCV. While detecting possible faces works fine (there are a lot of examples for Java already), I am stuck at doing the actual recognition. To be more precise, at training the face recognizer.
For testing purposes I have a folder structure with a subfolder per known person labeled "s" + id, in which the training photos are located. What works is reading the images and adding them to an array. What does not work is creating a second array with the identifiers. LBPHFaceRecognizer.train does require a Mat and I cannot figure out how to create the required data structure. What I have so far is:
MatVector images = new MatVector();
List<Integer> ids = new ArrayList<Integer>();
File root = new File("orl_faces/");
for (File subFolder : root.listFiles()) {
if (subFolder.getName().matches("s\\d+") && subFolder.isDirectory()) {
int personId = Integer.parseInt(subFolder.getName().substring(1));
for (File file : subFolder.listFiles()) {
if (file.getName().endsWith(".pgm") && !"10.pgm".equals(file.getName())) {
IplImage img = cvLoadImage(file.getAbsolutePath());
images.put(img);
ids.add(personId);
}
}
}
}
// ---- FIXME here is where I am stuck ----
Mat labels = new Mat(new Size(ids.size(), 1));
for (int i = 0; i < ids.size(); i++) {
MatExpr m = Mat.ones(new Size(1, 1), CV_32SC1);
Mat m2 = m.asMat();
labels.push_back(m2);
}
model.train(images, labels);
When executing this, I get a
Exception in thread "main" java.lang.RuntimeException: vector<T> too long
at org.bytedeco.javacpp.opencv_face$FaceRecognizer.train(Native Method)
and obviously even if it did work, I still would not have my numbers in there. Any help is greatly appreciated.
You can use MatOfInt and an array of ints. For example, if you collect labels in List<Integer:
int[] allLabels = new int[labels.size()];
for (int i = 0; i < labels.size(); i++) {
allLabels[i] = labels.get(i);
}
faceRecognizer.train(faces, new MatOfInt(allLabels));
This should work :)
I need to extract all the texts from some swf files. I'm using Java since I have a lot of modules developed with this language.
Thus, I did a search through the Web for all the free Java library devoted to handle SWF files.
Finally, I found the library developed by StuartMacKay. The library, named transform-swf, may be found on GitHub by clicking here.
The question is: Once I extract the GlyphIndexes from a TextSpan, how can I convert the glyps in characters?
Please, provide a complete working and tested example. No theoretical answer will be accepted nor answers like "it cannot be done", "it ain't possible", etc.
What I know and what I did
I know that the GlyphIndexes are built by using a TextTable, which is constructed by recurring to an integer that represente the font size and a font description provided by a DefineFont2 object, but when I decode all the DefineFont2, all have a zero length advance.
Here follows what I did.
//Creating a Movie object from an swf file.
Movie movie = new Movie();
movie.decodeFromFile(new File(out));
//Saving all the decoded DefineFont2 objects.
Map<Integer,DefineFont2> fonts = new HashMap<>();
for (MovieTag object : list) {
if (object instanceof DefineFont2) {
DefineFont2 df2 = (DefineFont2) object;
fonts.put(df2.getIdentifier(), df2);
}
}
//Now I retrieve all the texts
for (MovieTag object : list) {
if (object instanceof DefineText2) {
DefineText2 dt2 = (DefineText2) object;
for (TextSpan ts : dt2.getSpans()) {
Integer fontIdentifier = ts.getIdentifier();
if (fontIdentifier != null) {
int fontSize = ts.getHeight();
// Here I try to create an object that should
// reverse the process done by a TextTable
ReverseTextTable rtt =
new ReverseTextTable(fonts.get(fontIdentifier), fontSize);
System.out.println(rtt.charactersForText(ts.getCharacters()));
}
}
}
}
The class ReverseTextTable follows here:
public final class ReverseTextTable {
private final transient Map<Character, GlyphIndex> characters;
private final transient Map<GlyphIndex, Character> glyphs;
public ReverseTextTable(final DefineFont2 font, final int fontSize) {
characters = new LinkedHashMap<>();
glyphs = new LinkedHashMap<>();
final List<Integer> codes = font.getCodes();
final List<Integer> advances = font.getAdvances();
final float scale = fontSize / EMSQUARE;
final int count = codes.size();
for (int i = 0; i < count; i++) {
characters.put((char) codes.get(i).intValue(), new GlyphIndex(i,
(int) (advances.get(i) * scale)));
glyphs.put(new GlyphIndex(i,
(int) (advances.get(i) * scale)), (char) codes.get(i).intValue());
}
}
//This method should reverse from a list of GlyphIndexes to a String
public String charactersForText(final List<GlyphIndex> list) {
String text="";
for(GlyphIndex gi: list){
text+=glyphs.get(gi);
}
return text;
}
}
Unfortunately, the list of advances from DefineFont2 is empty, then the constructor of ReverseTableText get an ArrayIndexOutOfBoundException.
Honestly, I don't know how to do that in Java. I'm not claiming that it is not possible, I also believe that there is a way to do that. However, you said that there are a lot of libraries that do that. You also suggested a library, i.e. swftools. So, I suggest to recurr to that library to extract the text from a flash file. To do that you can use Runtime.exec() just to execute a command line to run that library.
Personally, I prefer Apache Commons exec rather than the standard library released with JDK. Well, just let me show you how you should do. The executable file that you should use is "swfstrings.exe". Suppose that it is put in "C:\". Suppose that in the same folder you can find a flash file, e.g. page.swf. Then, I tried the following code (it works fine):
Path pathToSwfFile = Paths.get("C:\" + File.separator + "page.swf");
CommandLine commandLine = CommandLine.parse("C:\" + File.separator + "swfstrings.exe");
commandLine.addArgument("\"" + swfFile.toString() + "\"");
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValues(new int[]{0, 1}); //Notice that swfstrings.exe returns 1 for success,
//0 for file not found, -1 for error
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout);
executor.setStreamHandler(psh);
int exitValue;
try{
exitValue = executor.execute(commandLine);
}catch(org.apache.commons.exec.ExecuteException ex){
psh.stop();
}
if(!executor.isFailure(exitValue)){
String out = stdout.toString("UTF-8"); // here you have the extracted text
}
I know, this is not exactly the answer that you requested, but works fine.
I happened to be working on decompiling an SWF in Java now and I came across this question while figuring out how to reverse engineer the original text back.
After looking at the source code, I realise its really straightforward. Each font has an assigned sequence of characters that can be retrieved by calling DefineFont2.getCodes(), and the glyphIndex is the index to the matching character in DefineFont2.getCodes().
However, in cases where there are multiple fonts in use in a single SWF file, it is difficult to match each DefineText to the corresponding DefineFont2 because there's no attributes that identifies the DefineFont2 used for each DefineText.
To work around this issue, I came up with a self-learning algorithm which will attempt to guess the right DefineFont2 for each DefineText and hence derive the original text correctly.
To reverse engineer the original text back, I created a class called FontLearner:
public class FontLearner {
private final ArrayList<DefineFont2> fonts = new ArrayList<DefineFont2>();
private final HashMap<Integer, HashMap<Character, Integer>> advancesMap = new HashMap<Integer, HashMap<Character, Integer>>();
/**
* The same characters from the same font will have similar advance values.
* This constant defines the allowed difference between two advance values
* before they are treated as the same character
*/
private static final int ADVANCE_THRESHOLD = 10;
/**
* Some characters have outlier advance values despite being compared
* to the same character
* This constant defines the minimum accuracy level for each String
* before it is associated with the given font
*/
private static final double ACCURACY_THRESHOLD = 0.9;
/**
* This method adds a DefineFont2 to the learner, and a DefineText
* associated with the font to teach the learner about the given font.
*
* #param font The font to add to the learner
* #param text The text associated with the font
*/
private void addFont(DefineFont2 font, DefineText text) {
fonts.add(font);
HashMap<Character, Integer> advances = new HashMap<Character, Integer>();
advancesMap.put(font.getIdentifier(), advances);
List<Integer> codes = font.getCodes();
List<TextSpan> spans = text.getSpans();
for (TextSpan span : spans) {
List<GlyphIndex> characters = span.getCharacters();
for (GlyphIndex character : characters) {
int glyphIndex = character.getGlyphIndex();
char c = (char) (int) codes.get(glyphIndex);
int advance = character.getAdvance();
advances.put(c, advance);
}
}
}
/**
*
* #param text The DefineText to retrieve the original String from
* #return The String retrieved from the given DefineText
*/
public String getString(DefineText text) {
StringBuilder sb = new StringBuilder();
List<TextSpan> spans = text.getSpans();
DefineFont2 font = null;
for (DefineFont2 getFont : fonts) {
List<Integer> codes = getFont.getCodes();
HashMap<Character, Integer> advances = advancesMap.get(getFont.getIdentifier());
if (advances == null) {
advances = new HashMap<Character, Integer>();
advancesMap.put(getFont.getIdentifier(), advances);
}
boolean notFound = true;
int totalMisses = 0;
int totalCount = 0;
for (TextSpan span : spans) {
List<GlyphIndex> characters = span.getCharacters();
totalCount += characters.size();
int misses = 0;
for (GlyphIndex character : characters) {
int glyphIndex = character.getGlyphIndex();
if (codes.size() > glyphIndex) {
char c = (char) (int) codes.get(glyphIndex);
Integer getAdvance = advances.get(c);
if (getAdvance != null) {
notFound = false;
if (Math.abs(character.getAdvance() - getAdvance) > ADVANCE_THRESHOLD) {
misses += 1;
}
}
} else {
notFound = false;
misses = characters.size();
break;
}
}
totalMisses += misses;
}
double accuracy = (totalCount - totalMisses) * 1.0 / totalCount;
if (accuracy > ACCURACY_THRESHOLD && !notFound) {
font = getFont;
// teach this DefineText to the FontLearner if there are
// any new characters
for (TextSpan span : spans) {
List<GlyphIndex> characters = span.getCharacters();
for (GlyphIndex character : characters) {
int glyphIndex = character.getGlyphIndex();
char c = (char) (int) codes.get(glyphIndex);
int advance = character.getAdvance();
if (advances.get(c) == null) {
advances.put(c, advance);
}
}
}
break;
}
}
if (font != null) {
List<Integer> codes = font.getCodes();
for (TextSpan span : spans) {
List<GlyphIndex> characters = span.getCharacters();
for (GlyphIndex character : characters) {
int glyphIndex = character.getGlyphIndex();
char c = (char) (int) codes.get(glyphIndex);
sb.append(c);
}
sb = new StringBuilder(sb.toString().trim());
sb.append(" ");
}
}
return sb.toString().trim();
}
}
Usage:
Movie movie = new Movie();
movie.decodeFromStream(response.getEntity().getContent());
FontLearner learner = new FontLearner();
DefineFont2 font = null;
List<MovieTag> objects = movie.getObjects();
for (MovieTag object : objects) {
if (object instanceof DefineFont2) {
font = (DefineFont2) object;
} else if (object instanceof DefineText) {
DefineText text = (DefineText) object;
if (font != null) {
learner.addFont(font, text);
font = null;
}
String line = learner.getString(text); // reverse engineers the line
}
I am happy to say that this method has given me a 100% accuracy in reverse engineering the original String using StuartMacKay's transform-swf library.
Its seems to be difficult on what your trying to achieve, Your trying to secompile the file bur i am sorry to say that its not possible , What I would suggest you to do is to convert it into some bitmap (if possible) or by any other method try to read the characters using OCR
There are some software's which do that, you can also check some forums regarding that. Because once compiled version of swf is very difficult (and not possible as far as i know). You can check this decompiler if you want or try using some other languages like the project here
I had a similar problem with long strings using transform-swf library.
Got the source code and debugged it.
I believe there was a small bug in class com.flagstone.transform.coder.SWFDecoder.
Line 540 (applicable to version 3.0.2), change
dest += length;
with
dest += count;
That should do it for you (it's about extracting strings).
I notified Stuart as well. The problem appears only if your strings are very large.
I know this isn't what you asked but I needed to pull text from SWF recently using Java and found the ffdec library much better than transform-swf
Comment if anyone needs sample code
I want to show image as a boolean value from a servlet to android app. Servlet will be uploaded to google app engine.
This is my servlet. "arrBool" value displays like some random value.
//
resp.setContentType("text/plain");
if (req.getParameterMap().containsKey("message"))
message = req.getParameter("message");
resp.getWriter().println("Server Said" + message);
for (int i=0; i<arrBool.length; i++) {
arrBool[i] = r.nextBoolean();
if(arrBool[i]==true) {
resp.getWriter().print(arrBool);
}
}
and this is my android application file:
//
RestClient client = new RestClient("http://machougul01.appspot.com/listenforrestclient");
client.AddParam("message", "Hello two World");
// client.AddParam("arrBool", "textView1");
try
{
client.Execute(RequestMethod.GET);
}
catch (Exception e)
{
textView.setText(e.getMessage());
}
String response = client.getResponse();
textView.setText(response);
}
Output shows "Server said: Hello two world" and arrBool value: "m9a9990a m9a9990"
I want to set arrBool value as image instead of m9a9990a. So whenever randomly values are selected if it is true then number of cars will be shown from 1 - 6 out of 6.
Please help me with this.
Try changing
resp.getWriter().print(arrBool);
To
resp.getWriter().print(String.valueOf(arrBool));
It will write a string in your response.
or you can change this
for (int i=0; i<arrBool.length; i++) {
arrBool[i] = r.nextBoolean();
if(arrBool[i]==true) {
resp.getWriter().print(arrBool);
}
}
To
for (int i=0; i<arrBool.length; i++) {
arrBool[i] = r.nextBoolean();
if(arrBool[i]==true) {
resp.getWriter().print(1);
}
}
So when you read at response at client side you will get response line as
"Server said: Hello two world"
111
You can then parse string and no 1's or true's out and show or
You have a few things you need to do.
First, there's a few import statements you'll need. Maybe you can use apache commons for the base64, this example uses xerces.
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
Next you need to create an image out of your booleans:
BufferedImage image = new BufferedImage(wid,hgt,BufferedImage.TYPE_3BYTE_RGB);
final int BLACK = java.awt.Color.BLACK.getRGB();
final int WHITE = java.awt.Color.WHITE.getRGB();
for(int x = 0; x < wid; x++) {
for(int y = 0; y < hgt; y++) {
boolean val = array[x*wid + y];
image.setRGB(x,y, val ? BLACK : WHITE);
}
}
I probably could have used a binary image there. This is what I knew offhand :) I'm sure you can tinker with it to change the formatting.
Then you need to convert it to Base64
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bytes = out.toByteArray();
String imageText = Base64.encode(baos.toByteArray());
Then, you need to spit out some HTML that references that base 64:
String html = "<img src=\"data:image/bmp;" + imageText + "\" alt=\"Random booleans\">";
And spit that html out on your page and you should be all set!
I have some data that I have saved into a file using Matlab. I have saved this data in Matlab as follows:
fwrite(fid,numImg2,'integer*4');
fwrite(fid,y,'integer*4');
fwrite(fid,imgName,'char*1');
fwrite(fid,a,'integer*4');
fwrite(fid,img.imageData,'double');
I read this data back into Matlab using the following code
fread(fid,1,'integer*4');// Returns numImg2
fread(fid,1,'integer*4');// Returns y which is the number of cha rectors in the image name, i use in the following line to read the image name, say for example if the image name is 1.jpg, then using the following will return the image name
fread(fid,5,'char*1');
fread(fid,1);
etc...
I want to be able to read this data on an android phone. This is the code I have at the moment.
DataInputStream ds = new DataInputStream(new FileInputStream(imageFile));
//String line;
// Read the first byte to find out how many images are stored in the file.
int x = 0;
byte numberOfImages;
int numImages = 0;
while(x<1)
{
numberOfImages = ds.readByte();
numImages = (int)numberOfImages;
x++;
}
int lengthName = 0;
String imgName = "";
for(int y=1; y<=numImages; y++)
{
lengthName = ds.readInt();
byte[] nameBuffer = new byte[lengthName];
char[] name = new char[lengthName];
for(int z = 1; z<=5;z++)
{
nameBuffer[z-1] = ds.readByte();
//name[z-1] = ds.readChar();
}
imgName = new String(nameBuffer);
//imgName = name.toString();
}
text.append(imgName);
I cannot seem to retrieve the image name as a string from the binary file data. Any help is much appreciated.
I'm not sure it will work but anyway:
byte[] nameBuffer = new byte[lengthName];
if(ds.read(nameBuffer) != lengthName) {
// error handling here
return;
}
imgName = new String(nameBuffer, "ISO-8859-1");
I'm trying to write a simple RTF document pretty much from scratch in Java, and I'm trying to embed JPEGs in the document. Here's an example of a JPEG (a 2x2-pixel JPEG consisting of three white pixels and a black pixel in the upper left, if you're curious) embedded in an RTF document (generated by WordPad, which converted the JPEG to WMF):
{\pict\wmetafile8\picw53\pich53\picwgoal30\pichgoal30
0100090000036e00000000004500000000000400000003010800050000000b0200000000050000
000c0202000200030000001e000400000007010400040000000701040045000000410b2000cc00
020002000000000002000200000000002800000002000000020000000100040000000000000000
000000000000000000000000000000000000000000ffffff00fefefe0000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000
0000001202af0801010000040000002701ffff030000000000
}
I've been reading the RTF specification, and it looks like you can specify that the image is a JPEG, but since WordPad always converts images to WMF, I can't see an example of an embedded JPEG. So I may also end up needing to transcode from JPEG to WMF or something....
But basically, I'm looking for how to generate the binary or hexadecimal (Spec, p.148: "These pictures can be in hexadecimal (the default) or binary format.") form of a JPEG given a file URL.
Thanks!
EDIT: I have the stream stuff working all right, I think, but still don't understand exactly how to encode it, because whatever I'm doing, it's not RTF-readable. E.g., the above picture instead comes out as:
ffd8ffe00104a464946011106006000ffdb0430211211222222223533333644357677767789b988a877adaabcccc79efdcebcccffdb04312223336336c878ccccccccccccccccccccccccccccccccccccccccccccccccccffc0011802023122021113111ffc401f001511111100000000123456789abffc40b5100213324355440017d123041151221314161351617227114328191a182342b1c11552d1f024336272829a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9faffc401f103111111111000000123456789abffc40b51102124434754401277012311452131612415176171132232818144291a1b1c19233352f0156272d1a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda0c31021131103f0fdecf09f84f4af178574cd0b42d334fd1744d16d22bd3f4fb0b74b6b5bb78902450c512091c688aaaa8a0500014514507ffd9
This PHP library would do the trick, so I'm trying to port the relevant portion to Java. Here is is:
$imageData = file_get_contents($this->_file);
$size = filesize($this->_file);
$hexString = '';
for ($i = 0; $i < $size; $i++) {
$hex = dechex(ord($imageData{$i}));
if (strlen($hex) == 1) {
$hex = '0' . $hex;
}
$hexString .= $hex;
}
return $hexString;
But I don't know what the Java analogue to dechex(ord($imageData{$i})) is. :( I got only as far as the Integer.toHexString() function, which takes care of the dechex part....
Thanks all. :)
Given a file URL for any file you can get the corresponding bytes by doing (exception handling omitted for brevity)...
int BUF_SIZE = 512;
URL fileURL = new URL("http://www.somewhere.com/someurl.jpg");
InputStream inputStream = fileURL.openStream();
byte [] smallBuffer = new byte[BUF_SIZE];
ByteArrayOutputStream largeBuffer = new ByteArrayOutputStream();
int numRead = BUF_SIZE;
while(numRead == BUF_SIZE) {
numRead = inputStream.read(smallBuffer,0,BUF_SIZE);
if(numRead > 0) {
largeBuffer.write(smallBuffer,0,BUF_SIZE);
}
}
byte [] bytes = largeBuffer.toByteArray();
I'm looking at your PHP snippet now and realizing that RTF is a bizarre specification! It looks like each byte of the image is encoded as 2 hex digits (which doubles the size of the image for no apparent reason). The the entire thing is stored in raw ASCII encoding. So, you'll want to do...
StringBuilder hexStringBuilder = new StringBuilder(bytes.length * 2);
for(byte imageByte : bytes) {
String hexByteString = Integer.toHexString(0x000000FF & (int)imageByte);
if(hexByteString .size() == 1) {
hexByteString = "0" + hexByteString ;
}
hexStringBuilder.append(hexByteString);
}
String hexString = hexStringBuilder.toString();
byte [] hexBytes = hexString.getBytes("UTF-8"); //Could also use US-ASCII
EDIT: Updated code sample to pad 0's on the hex bytes
EDIT: negative bytes were getting logically right shifted when converted to ints >_<
https://joseluisbz.wordpress.com/2013/07/26/exploring-a-wmf-file-0x000900/
Maybe help you this:
String HexRTFBytes = "Representations text of bytes from Image RTF File";
String Destiny = "The path of the output File";
FileOutputStream wmf;
try {
wmf = new FileOutputStream(Destiny);
HexRTFBytes = HexRTFBytes.replaceAll("\n", ""); //Erase New Lines
HexRTFBytes = HexRTFBytes.replaceAll(" ", ""); //Erase Blank spaces
int NumBytesWrite = HexRTFBytes.length();
int WMFBytes = NumBytesWrite/2;//One byte is represented by 2 characters
byte[] ByteWrite = new byte[WMFBytes];
for (int i = 0; i < WMFBytes; i++){
se = HexRTFBytes.substring(i*2,i*2+2);
int Entero = Integer.parseInt(se,16);
ByteWrite[i] = (byte)Entero;
}
wmf.write(ByteWrite);
wmf.close();
}
catch (FileNotFoundException fnfe)
{System.out.println(fnfe.toString());}
catch (NumberFormatException fnfe)
{System.out.println(fnfe.toString());}
catch (EOFException eofe)
{System.out.println(eofe.toString());}
catch (IOException ioe)
{System.out.println(ioe.toString());}
This code take the representation in one string, and result is stored in a file.
https://joseluisbz.wordpress.com/2011/06/22/script-de-clases-rtf-para-jsp-y-php/
Now if you want to obtain the representation of the image file, you can use this:
private void ByteStreamImageString(byte[] ByteStream) {
this.Format = 0;
this.High = 0;
this.Wide = 0;
this.HexImageString = "Error";
if (ByteStream[0]== (byte)137 && ByteStream[1]== (byte)80 && ByteStream[2]== (byte)78){
this.Format = PNG; //PNG
this.High = this.Byte2PosInt(ByteStream[22],ByteStream[23]);
this.Wide = this.Byte2PosInt(ByteStream[18],ByteStream[19]);
}
if (ByteStream[0]== (byte)255 && ByteStream[1]== (byte)216
&& ByteStream[2]== (byte)255 && ByteStream[3]== (byte)224){
this.Format = JPG; //JPG
int PosJPG = 2;
while (PosJPG < ByteStream.length){
String M = String.format("%02X%02X", ByteStream[PosJPG+0],ByteStream[PosJPG+1]);
if (M.equals("FFC0") || M.equals("FFC1") || M.equals("FFC2") || M.equals("FFC3")){
this.High = this.Byte2PosInt(ByteStream[PosJPG+5],ByteStream[PosJPG+6]);
this.Wide = this.Byte2PosInt(ByteStream[PosJPG+7],ByteStream[PosJPG+8]);
}
if (M.equals("FFDA")) {
break;
}
PosJPG = PosJPG+2+this.Byte2PosInt(ByteStream[PosJPG+2],ByteStream[PosJPG+3]);
}
}
if (this.Format > 0) {
this.HexImageString = "";
int Salto = 0;
for (int i=0;i < ByteStream.length; i++){
Salto++;
this.HexImageString += String.format("%02x", ByteStream[i]);
if (Salto==64){
this.HexImageString += "\n"; //To make readable
Salto = 0;
}
}
}
}