this title has been edited - bufferedreader

i edited this code because i cant delete it permanently . i edited this code because i cant delete it permanently . i edited this code because i cant delete it permanently . i edited this code because i cant delete it permanently
.i edited this code because i cant delete it permanently
please do not re edit it

You should separate the loading of the file into the graph from the creation of the graph.
Something like this:
public interface GraphLoader {
/**
* Fill a graph with no property from a single file.
*
* #param graph the graph to fill
* #param path path to graph file
* #return a graph with no property
*/
public static void loadSingleFile(Graph graph, Path path) {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path.toFile()))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] vertices = line.split(" ");
if (vertices.length < 2) {
continue;
}
graph.addEdge(Integer.parseInt(vertices[0]), Integer.parseInt(vertices[1]));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* load a graph with no property from a single file.
*
* #param path path to graph file
* #return a graph with no property
*/
public static Graph loadSingleFile(Path path) {
Graph graph = new Graph();
loadSingleFile(graph, path);
return graph;
}
/**
* load a graph with no property from multiple files in parallel.
*
* #param paths paths to graph files
* #return a graph with no property
*/
static Graph loadMultipleFiles(Path[] paths) {
Graph graph = new Graph();
for (Path path : paths) {
loadSingleFile(graph, path);
}
return graph;
}
}

Use "try with resources".
Try with resources documentation by Oracle
Divide "catch" block in hierarchy order
Advantages of exceptions by Oracle
Why do you use interface for this? Is it an architectural idea?

Related

How to display local queues where curdepth reached its maxdepth in ibm mq

I want to display all local queues where curdepth reached its maxdepth.
I understand that the below where condition in runmqsc will not work.
DIS QL(*) WHERE(CURDEPTH EQ MAXDEPTH)
I am trying to parse it with sed and awk but not even close as I am no expert in scripting
Please help in getting desired output. Thanks
Isn't this like trying to squeeze a fully blown up balloon into a wine bottle?
It would seem far, far simpler to just run a Java/MQ/PCF application to get both the current and maximum depths and compare the values.
Here is a simple (complete) Java/MQ/PCF application to do that:
Note: It has a filter on the PCF command to only return queues with a current depth greater than zero. Hence, the queue manager's command server will return less data and make over all processing faster.
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import com.ibm.mq.MQException;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.headers.MQDataException;
import com.ibm.mq.headers.pcf.PCFMessage;
import com.ibm.mq.headers.pcf.PCFMessageAgent;
/**
* Program Name
* MQCurrentDepthMonitor01
*
* Description
* This java class issues a PCF "inquire queue" request message for all ("*") local queues
* with a queue depth greater than 0 (zero) of a remote queue manager and
* (1) output an error message if current depth is the same as max depth or
* (2) output a warning message if current depth is within 90% of max depth.
*
* Sample Command Line Parameters
* -m MQA1 -h 127.0.0.1 -p 1414 -c TEST.CHL -u UserID -x Password
*
* #author Roger Lacroix
*/
public class MQCurrentDepthMonitor01
{
private static final SimpleDateFormat LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
private Hashtable<String,String> params;
private Hashtable<String,Object> mqht;
private String qMgrName;
public MQCurrentDepthMonitor01()
{
super();
params = new Hashtable<String,String>();
mqht = new Hashtable<String,Object>();
}
/**
* Make sure the required parameters are present.
* #return true/false
*/
private boolean allParamsPresent()
{
boolean b = params.containsKey("-h") && params.containsKey("-p") &&
params.containsKey("-c") && params.containsKey("-m") &&
params.containsKey("-u") && params.containsKey("-x");
if (b)
{
try
{
Integer.parseInt((String) params.get("-p"));
}
catch (NumberFormatException e)
{
b = false;
}
}
return b;
}
/**
* Extract the command-line parameters and initialize the MQ HashTable.
* #param args
* #throws IllegalArgumentException
*/
private void init(String[] args) throws IllegalArgumentException
{
int port = 1414;
if (args.length > 0 && (args.length % 2) == 0)
{
for (int i = 0; i < args.length; i += 2)
{
params.put(args[i], args[i + 1]);
}
}
else
{
throw new IllegalArgumentException();
}
if (allParamsPresent())
{
qMgrName = (String) params.get("-m");
try
{
port = Integer.parseInt((String) params.get("-p"));
}
catch (NumberFormatException e)
{
port = 1414;
}
mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c"));
mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h"));
mqht.put(CMQC.PORT_PROPERTY, new Integer(port));
mqht.put(CMQC.USER_ID_PROPERTY, params.get("-u"));
mqht.put(CMQC.PASSWORD_PROPERTY, params.get("-x"));
// I don't want to see MQ exceptions at the console.
MQException.log = null;
}
else
{
throw new IllegalArgumentException();
}
}
/**
* Handle connecting to the queue manager, issuing PCF command then
* looping through PCF response messages and disconnecting from
* the queue manager.
*/
private void doPCF()
{
MQQueueManager qMgr = null;
PCFMessageAgent agent = null;
PCFMessage request = null;
PCFMessage[] responses = null;
try
{
qMgr = new MQQueueManager(qMgrName, mqht);
MQCurrentDepthMonitor01.logger("successfully connected to "+ qMgrName);
agent = new PCFMessageAgent(qMgr);
MQCurrentDepthMonitor01.logger("successfully created agent");
// https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_9.1.0/com.ibm.mq.ref.adm.doc/q087800_.htm
request = new PCFMessage(CMQCFC.MQCMD_INQUIRE_Q);
/**
* You can explicitly set a queue name like "TEST.Q1" or
* use a wild card like "TEST.*"
*/
request.addParameter(CMQC.MQCA_Q_NAME, "*");
// Add parameter to request only local queues
request.addParameter(CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL);
// Add parameter to request only queue name, current depth and max depth
request.addParameter(CMQCFC.MQIACF_Q_ATTRS, new int [] {
CMQC.MQCA_Q_NAME,
CMQC.MQIA_CURRENT_Q_DEPTH,
CMQC.MQIA_MAX_Q_DEPTH
});
// Add filter to only return responses with a queue depth greater than 0 (zero)
// i.e. non-zero queue depth
request.addFilterParameter(CMQC.MQIA_CURRENT_Q_DEPTH, CMQCFC.MQCFOP_GREATER, 0);
responses = agent.send(request);
// MQCurrentDepthMonitor01.logger("responses.length="+responses.length);
int curDepth = -1;
int maxDepth = -1;
for (int i = 0; i < responses.length; i++)
{
if ( ((responses[i]).getCompCode() == CMQC.MQCC_OK) &&
((responses[i]).getParameterValue(CMQC.MQCA_Q_NAME) != null) )
{
String name = responses[i].getStringParameterValue(CMQC.MQCA_Q_NAME);
if (name != null)
name = name.trim();
curDepth = responses[i].getIntParameterValue(CMQC.MQIA_CURRENT_Q_DEPTH);
maxDepth = responses[i].getIntParameterValue(CMQC.MQIA_MAX_Q_DEPTH);
// MQCurrentDepthMonitor01.logger("Name="+name + " : curDepth="+curDepth + " : maxDepth="+maxDepth);
if (curDepth == maxDepth)
MQCurrentDepthMonitor01.logger("ERROR: Name="+name + " : current depth equals max depth ["+maxDepth+"]");
else if (curDepth >= (maxDepth * 0.9))
MQCurrentDepthMonitor01.logger("Warning: Name="+name + " : current depth ["+curDepth+"] is within 90% of max depth ["+maxDepth+"]");
}
}
}
catch (MQException e)
{
MQCurrentDepthMonitor01.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
catch (IOException e)
{
MQCurrentDepthMonitor01.logger("IOException:" +e.getLocalizedMessage());
}
catch (MQDataException e)
{
MQCurrentDepthMonitor01.logger("MQDataException:" +e.getLocalizedMessage());
}
finally
{
try
{
if (agent != null)
{
agent.disconnect();
MQCurrentDepthMonitor01.logger("disconnected from agent");
}
}
catch (MQDataException e)
{
MQCurrentDepthMonitor01.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
try
{
if (qMgr != null)
{
qMgr.disconnect();
MQCurrentDepthMonitor01.logger("disconnected from "+ qMgrName);
}
}
catch (MQException e)
{
MQCurrentDepthMonitor01.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
}
}
/**
* A simple logger method
* #param data
*/
public static void logger(String data)
{
String className = Thread.currentThread().getStackTrace()[2].getClassName();
// Remove the package info.
if ( (className != null) && (className.lastIndexOf('.') != -1) )
className = className.substring(className.lastIndexOf('.')+1);
System.out.println(LOGGER_TIMESTAMP.format(new Date())+" "+className+": "+Thread.currentThread().getStackTrace()[2].getMethodName()+": "+data);
}
public static void main(String[] args)
{
MQCurrentDepthMonitor01 mqlqs = new MQCurrentDepthMonitor01();
try
{
mqlqs.init(args);
mqlqs.doPCF();
}
catch (IllegalArgumentException e)
{
MQCurrentDepthMonitor01.logger("Usage: java MQCurrentDepthMonitor01 -m QueueManagerName -h host -p port -c channel -u UserID -x Password");
System.exit(1);
}
System.exit(0);
}
}

How to get random words of a give length (L) from a dictionary which has been implemented using a trie?

How to retrieve a random word of a given length from a Trie
The answer above explains how to select the first character but I am confused how we will proceed after that. I want words of Length L but when I start traversing the tree, I wouldn't know if the branch that is being traversed has depth L.
Dictionary
package com.FastDictionary;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import sun.rmi.runtime.Log;
/**
* Dictionary implementation.
* Uses Trie Data Structure
* Creates a singleton object
*/
public class FastDictionary {
private int nineWordCount;
private int totalWordCount;
// Root Node
private DictionaryNode root;
// Singleton object
private static FastDictionary fastDictionary;
// Flag; True if words.txt has been processed once
private boolean isProcessed;
private FastDictionary() {
this.root = new DictionaryNode();
isProcessed = false;
this.nineWordCount = 0;
this.totalWordCount = 0;
}
private boolean sanitiseSearch(String text) {
if (text == null) {
return false;
}
else {
return text.matches("[a-zA-Z]");
}
}
/**
* Add a word to Dictionary
* #param word word to be added
*/
public void addWord(String word) {
if (word == null) {
throw new IllegalArgumentException("Word to be added to Dictionary can't be null");
}
// Sanitise input
if (word.contains(" ")) {
throw new IllegalArgumentException(
"Word to be added to Dictionary can't contain white spaces");
}
DictionaryNode currentNode = this.root;
for (char c: word.toCharArray()) {
DictionaryNode child = currentNode.getChild(c);
if (child == null) {
currentNode = currentNode.addChild(c);
}
else {
currentNode = child;
}
}
// Last node contains last character of valid word
// Set that node as Leaf Node for valid word
currentNode.setLeaf();
}
/**
*
* #param word String to be checked if it is a valid word
* #return True if valid word
*/
public boolean isWord(String word) {
if (word == null) {
throw new IllegalArgumentException("Word to be added to Dictionary can't be null");
}
// Sanitise input
if (word.contains(" ")) {
throw new IllegalArgumentException(
"Word to be added to Dictionary can't contain white spaces");
}
DictionaryNode currentNode = this.root;
for (char c: word.toCharArray()) {
DictionaryNode child = currentNode.getChild(c);
if (child == null) {
return false;
}
currentNode = child;
}
// Returns true if Last Character was leaf
return currentNode.isLeaf();
}
/**
*
* #param text String that needs to be searched
* #return List of Strings which are valid words searched using 'text'
*
*/
public ArrayList<String> getWords(String text) {
ArrayList<String> words = new ArrayList<String>();
DictionaryNode currentNode = this.root;
for (int i = 0; i < text.length() ; i++) {
DictionaryNode child = currentNode.getChild(text.charAt(i));
if (child == null) {
return words;
}
if (child.isLeaf()) {
words.add(text.substring(0,i+1));
}
currentNode = child;
}
return words;
}
/**
*
* #param inputFileStream Text file containing list of valid words
* Switches Flag isProcessed to True
*/
public void processFile(InputStream inputFileStream) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputFileStream));
String line;
while((line = br.readLine()) != null) {
line = line.trim();
this.addWord(line);
// Nine Word test
if (line.length() == 9) {
this.nineWordCount++;
}
this.totalWordCount++;
}
}
catch(Exception e){
System.out.print(e);
}
this.isProcessed = true;
}
/**
*
* #return True if valid words text file has been processed
* Word file needs to be processed just once
*/
public boolean isProcessed() {
return this.isProcessed;
}
/**
* Factory method to create Singleton Object
* #return Singleton object
*/
public static FastDictionary getInstance() {
if (fastDictionary == null) {
fastDictionary = new FastDictionary();
}
return fastDictionary;
}
public int getNineWordCount() {
return this.nineWordCount;
}
}
**Node**
package com.FastDictionary;
import java.util.HashMap;
/**
* Node of the Trie Data Structure used for FastDictionary
*/
public class DictionaryNode {
// Character which the Node represents
private char nodeChar;
// Points to children
private HashMap<Character, DictionaryNode> children = new HashMap<Character,DictionaryNode>();
// Is Node the last character for a valid word
private boolean isLeaf;
/**
* To create Root Node
*/
DictionaryNode() {
this.nodeChar = '.';
this.isLeaf = false;
}
/**
* To create Child Node
* #param c Character that Node represents
*/
DictionaryNode(char c) {
this.nodeChar = c;
isLeaf = false;
}
/**
*
* #param c Character that Node represents
* #return Child Node which was created
*/
public DictionaryNode addChild(char c) {
DictionaryNode child = new DictionaryNode(c);
this.children.put(c, child);
return child;
}
/**
*
* #return true if Node is the last character for a valid word; default is false
*/
public boolean isLeaf() {
return this.isLeaf;
}
/**
* Set Node as Leaf Node for a valid word
*/
public void setLeaf() {
this.isLeaf = true;
}
/**
*
* #param c the character which the Child Node represnts
* #return Child Node representing character c; null if no such Child exists
*/
public DictionaryNode getChild(char c) {
DictionaryNode child = this.children.get(c);
return child;
}
}
Yes, he only shows how to choose first character from root node. However, after you update your currentNode following that character, you can apply exact same principal to find next character from the new node. Another way of viewing what his algorithm did is, given a node, an integer L(5 in his example), finds i'th (1234 in his example) word which is in the subtree of that node and is exactly L depth away from it.
So after you have made your first move, you can recursively call that algorithm from new node, with L-1 as depth. This is basic idea. Of course,some details need to be filled.
Firstly, updating i before next recursive call. Say algorithm chose first character to be d. And first 3 letters i.e a b c combinedly had 1000 5-letter words. So now, you need to find (1234-1000)=234th word from this new node.
Secondly, instead of having lengthFrequencyByLetter and totalLengthFrequency for entire tree,now you need to have them for every single node, which will require lots of ram. (you can optimize that by using HashMap though.)
A very high level implementation could be:
String randomWord(Node currentNode,int L,int index){
if(L==0) return node.wordContainedWithin();
char ch = find_next_character(node,L,index); //'d' in our example
newNode = currentNode.getChild(ch); //node following d
//following example, words_before = 1000
int words_before = sum(lengthFrequencyByLetter[x][L] of all x before ch)
int new_index = index - words_before;
return randomWord(newNode,L-1,new_index);
}
Now to get a random L-letter word, look up root's totalLengthFrequency[L], generate a number i (1234 here) between 0 to that value, and call randomWord as:
randomWord(tree.root,L,i)

Using the onFrameAvailable() in Jacobi Google Tango API

Question: Does anyone know how to get the Tango's color camera image buffer using the Tango Java (Jacobi) API onFrameAvailable() callback?
Background:
I have an augmented reality application that displays video in the background of the Tango. I've successfully created the video overlay example using the the Java API (Jacobi) following this example. My application works fine, and the video is rendered in the background properly.
As part of the application, I'd like to store a copy of the video backbuffer when the user presses a button. Therefore, I need access to the camera's RGB data.
According to the Jacobi release notes, any class desiring access to the camera RGB data should implement the new onFrameAvailable() method in the OnTangoUpdateListener. I did this, but I don't see any handle or arguments to actually get the pixels:
Java API
#Override
public void onFrameAvailable(int cameraId) {
//Log.w(TAG, "Frame available!");
if (cameraId == TangoCameraIntrinsics.TANGO_CAMERA_COLOR) {
tangoCameraPreview.onFrameAvailable();
}
}
as shown, onFrameAvailable only has one argument, and integer designating the id of the camera generating the view. Contrast this with the C-library call back, which provides access to the image buffer:
C API
TangoErrorType TangoService_connectOnFrameAvailable(
TangoCameraId id, void* context,
void (*onFrameAvailable)(void* context, TangoCameraId id,
const TangoImageBuffer* buffer));
I was expecting the Java method to have something similar to the buffer object in the C API call.
What I've Tried
I tried extending the TangoCameraPreview class and saving the image there, but I only get a black background.
public class CameraSurfaceView extends TangoCameraPreview {
private boolean takeSnapShot = false;
public void takeSnapShot() {
takeSnapShot = true;
}
/**
* Grabs a copy of the surface (which is rendering the Tango color camera)
* https://stackoverflow.com/questions/14620055/how-to-take-a-screenshot-of-androids-surface-view
*/
public void screenGrab2(){
int width = this.getWidth();
int height = this.getHeight();
long fileprefix = System.currentTimeMillis();
View v= getRootView();
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, width, height);
v.buildDrawingCache(true);
Bitmap image = v.getDrawingCache();
//TODO: make seperate subdirctories for each exploitation sessions
String targetPath =Environment.getExternalStorageDirectory() + "/RavenEye/Photos/";
String imageFileName = fileprefix + ".jpg";
if(!(new File(targetPath)).exists()) {
new File(targetPath).mkdirs();
}
try {
File targetDirectory = new File(targetPath);
File photo=new File(targetDirectory, imageFileName);
FileOutputStream fos=new FileOutputStream(photo.getPath());
image.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Log.i(this.getClass().getCanonicalName(), "Grabbed an image in target path:" + targetPath);
} catch (FileNotFoundException e) {
Log.e(CameraPreview.class.getName(),"Exception " + e);
e.printStackTrace();
} catch (IOException e) {
Log.e(CameraPreview.class.getName(),"Exception " + e);
e.printStackTrace();
}
}
/**
* Grabs a copy of the surface (which is rendering the Tango color camera)
*/
public void screenGrab(){
int width = this.getWidth();
int height = this.getHeight();
long fileprefix = System.currentTimeMillis();
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawBitmap(image, 0, 0, null);
//TODO: make seperate subdirctories for each exploitation sessions
String targetPath =Environment.getExternalStorageDirectory() + "/RavenEye/Photos/";
String imageFileName = fileprefix + ".jpg";
if(!(new File(targetPath)).exists()) {
new File(targetPath).mkdirs();
}
try {
File targetDirectory = new File(targetPath);
File photo=new File(targetDirectory, imageFileName);
FileOutputStream fos=new FileOutputStream(photo.getPath());
image.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Log.i(this.getClass().getCanonicalName(), "Grabbed an image in target path:" + targetPath);
} catch (FileNotFoundException e) {
Log.e(CameraPreview.class.getName(),"Exception " + e);
e.printStackTrace();
} catch (IOException e) {
Log.e(CameraPreview.class.getName(),"Exception " + e);
e.printStackTrace();
}
}
#Override
public void onFrameAvailable() {
super.onFrameAvailable();
if(takeSnapShot) {
screenGrab();
takeSnapShot = false;
}
}
public CameraSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
Where I'm Heading
I'm preparing to root the device, and then using the onFrameAvailable method to cue an external root process such as one of these:
post 23610900
post 10965409
post 4998527
I'm hoping I can find a way to avoid the root hack.
Thank you in advance!
OK, I figured out a way to make it work.
Update: My working solution is here:
https://github.com/stevehenderson/GoogleTango_AR_VideoCapture
I essentially set up a "man (renderer) in the middle" attack on the rendering pipeline.
This approach intercepts the SetRenderer call from the TangoCameraPreview base class, and allows one to get access
to the base renderer's OnDraw() method and the GL context. I then add additional methods to this extended renderer that allow reading of the GL buffer.
General approach
1) Extend the TangoCameraPreview class (e.g. in my example ReadableTangoCameraPreview). Override the setRenderer(GLSurfaceView.Renderer renderer), keeping a reference to the base renderer, and replacing the renderer with your own "wrapped" GLSUrface.Renderer renderer that will add methods to render the backbuffer to an image on the device.
2) Create your own GLSurfaceView.Renderer Interface (e.g. my ScreenGrabRenderer class ) that implements all the GLSurfaceView.Renderer methods, passing them on to the base renderer captured in Step 1. Also, add a few new methods to "cue" when you want to grab the image.
3) Implement the ScreenGrabRenderer described in step 2 above.
4) Use a callback interface (my TangoCameraScreengrabCallback) to communicate when an image has been copied
It works pretty well, and allows one to grab the camera bits in an image without rooting the device.
Note: I haven't had the need to closely synchronize my captured images with the point cloud. So I haven't checked the latency. For best results, you may need to invoke the C methods proposed by Mark.
Here's what each of my classes looks like..
///Main Activity Class where bulk of Tango code is
.
.
.
// Create our Preview view and set it as the content of our activity.
mTangoCameraPreview = new ReadableTangoCameraPreview(getActivity());
RelativeLayout preview = (RelativeLayout) view.findViewById(R.id.camera_preview);
preview.addView(mTangoCameraPreview);
.
.
.
//When you want to take a snapshot, call the takeSnapShotMethod()
//(you can make this respond to a button)
mTangoCameraPreview.takeSnapShot();
.
.
.
.
.
//Main Tango Listeners
#Override
public void onFrameAvailable(final int cameraId) {
// Update the UI with TangoPose information
runOnUiThread(new Runnable() {
#Override
public void run() {
if (cameraId == TangoCameraIntrinsics.TANGO_CAMERA_COLOR) {
tangoCameraPreview.onFrameAvailable();
}
}
});
}
ReadableTangoCameraPreview Class
public class ReadableTangoCameraPreview extends TangoCameraPreview implements TangoCameraScreengrabCallback {
Activity mainActivity;
private static final String TAG = ReadableTangoCameraPreview.class.getSimpleName();
//An intercept renderer
ScreenGrabRenderer screenGrabRenderer;
private boolean takeSnapShot = false;
#Override
public void setRenderer(GLSurfaceView.Renderer renderer) {
//Create our "man in the middle"
screenGrabRenderer= new ScreenGrabRenderer(renderer);
//Set it's call back
screenGrabRenderer.setTangoCameraScreengrabCallback(this);
//Tell the TangoCameraPreview class to use this intermediate renderer
super.setRenderer(screenGrabRenderer);
Log.i(TAG,"Intercepted the renderer!!!");
}
/**
* Set a trigger for snapshot. Call this from main activity
* in response to a use input
*/
public void takeSnapShot() {
takeSnapShot = true;
}
#Override
public void onFrameAvailable() {
super.onFrameAvailable();
if(takeSnapShot) {
//screenGrabWithRoot();
screenGrabRenderer.grabNextScreen(0,0,this.getWidth(),this.getHeight());
takeSnapShot = false;
}
}
public ReadableTangoCameraPreview(Activity context) {
super(context);
mainActivity = context;
}
public void newPhoto(String aNewPhotoPath) {
//This gets called when a new photo was grabbed created in the renderer
Log.i(TAG,"New image available at" + aNewPhotoPath);
}
}
ScreenGrabRenderer Interface
(Overloads the TangoCameraPreview default Renderer)
/**
* This is an intermediate class that intercepts all calls to the TangoCameraPreview's
* default renderer.
*
* It simply passes all render calls through to the default renderer.
*
* When required, it can also use the renderer methods to dump a copy of the frame to a bitmap
*
* #author henderso
*
*/
public class ScreenGrabRenderer implements GLSurfaceView.Renderer {
TangoCameraScreengrabCallback mTangoCameraScreengrabCallback;
GLSurfaceView.Renderer tangoCameraRenderer;
private static final String TAG = ScreenGrabRenderer.class.getSimpleName();
private String lastFileName = "unset";
boolean grabNextScreen = false;
int grabX = 0;
int grabY = 0;
int grabWidth = 640;
int grabHeight = 320;
public void setTangoCameraScreengrabCallback(TangoCameraScreengrabCallback aTangoCameraScreengrabCallback) {
mTangoCameraScreengrabCallback = aTangoCameraScreengrabCallback;
}
/**
* Cue the renderer to grab the next screen. This is a signal that will
* be detected inside the onDrawFrame() method
*
* #param b
*/
public void grabNextScreen(int x, int y, int w, int h) {
grabNextScreen = true;
grabX=x;
grabY=y;
grabWidth=w;
grabHeight=h;
}
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
tangoCameraRenderer.onSurfaceCreated(gl, config);
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
tangoCameraRenderer.onSurfaceChanged(gl, width, height);
}
#Override
public void onDrawFrame(GL10 gl) {
tangoCameraRenderer.onDrawFrame(gl);
if(grabNextScreen) {
screenGrab(gl);
grabNextScreen=false;
}
}
/**
*
* Creates a bitmap given a certain dimension and an OpenGL context
*
* This code was lifted from here:
*
* http://stackoverflow.com/questions/5514149/capture-screen-of-glsurfaceview-to-bitmap
*/
private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl)
throws OutOfMemoryError {
int bitmapBuffer[] = new int[w * h];
int bitmapSource[] = new int[w * h];
IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
intBuffer.position(0);
try {
gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
int offset1, offset2;
for (int i = 0; i < h; i++) {
offset1 = i * w;
offset2 = (h - i - 1) * w;
for (int j = 0; j < w; j++) {
int texturePixel = bitmapBuffer[offset1 + j];
int blue = (texturePixel >> 16) & 0xff;
int red = (texturePixel << 16) & 0x00ff0000;
int pixel = (texturePixel & 0xff00ff00) | red | blue;
bitmapSource[offset2 + j] = pixel;
}
}
} catch (GLException e) {
Log.e(TAG,e.toString());
return null;
}
return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
}
/**
* Writes a copy of the GLSurface backbuffer to storage
*/
private void screenGrab(GL10 gl) {
long fileprefix = System.currentTimeMillis();
String targetPath =Environment.getExternalStorageDirectory() + "/RavenEye/Photos/";
String imageFileName = fileprefix + ".png";
String fullPath = "error";
Bitmap image = createBitmapFromGLSurface(grabX,grabY,grabWidth,grabHeight,gl);
if(!(new File(targetPath)).exists()) {
new File(targetPath).mkdirs();
}
try {
File targetDirectory = new File(targetPath);
File photo=new File(targetDirectory, imageFileName);
FileOutputStream fos=new FileOutputStream(photo.getPath());
image.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
fullPath =targetPath + imageFileName;
Log.i(TAG, "Grabbed an image in target path:" + fullPath);
///Notify the outer class(es)
if(mTangoCameraScreengrabCallback != null) {
mTangoCameraScreengrabCallback.newPhoto(fullPath);
} else {
Log.i(TAG, "Callback not set properly..");
}
} catch (FileNotFoundException e) {
Log.e(TAG,"Exception " + e);
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG,"Exception " + e);
e.printStackTrace();
}
lastFileName = fullPath;
}
/**
* Constructor
* #param baseRenderer
*/
public ScreenGrabRenderer(GLSurfaceView.Renderer baseRenderer) {
tangoCameraRenderer = baseRenderer;
}
}
TangoCameraScreengrabCallback Interface
(not required unless you want to pass info back from the screen grab renderer)
/*
* The TangoCameraScreengrabCallback is a generic interface that provides callback mechanism
* to an implementing activity.
*
*/
interface TangoCameraScreengrabCallback {
public void newPhoto(String aNewPhotoPath);
}
I haven't tried on the latest release, but it was the absence of this functionality that drove me to the C API where I could get image data - a recent post, I think on the G+ page, seemed to indicate that the Unity API now returns image data as well - for a company that wants to keep scolding us when we don't use Java, it certainly is an odd lag :-)

ITextSharp - get image at a particular location

What I need to do is extract a 2D DataMatrix (bitmap) bar code and read it. I can make this work however, I have to loop through all the images on every page. This is taking a long time when I have 1000’s of pages, so I was wondering if it was possible to define a location(rectangle)of where the image(barcode) was and just extract that image?
The bar code is always i the same location.
note: I'm using Spire.Barcode from e-IceBlue
Thank you for any help.
CODE RenderFilter snippet:
public class MyRegionTextRenderFilter : RenderFilter {
/** the region to allow text from */
private RectangleJ filterRect;
public PdfImageObject image;
/**
* Constructs a filter
* #param filterRect the rectangle to filter text against. Note that this is a java.awt.Rectangle !
*/
public MyRegionTextRenderFilter(RectangleJ filterRect) {
this.filterRect = filterRect;
}
/**
* Constructs a filter
* #param filterRect the rectangle to filter text against.
*/
public MyRegionTextRenderFilter(iTextSharp.text.Rectangle filterRect)
{
this.filterRect = new RectangleJ(filterRect);
}
/**
* #see com.itextpdf.text.pdf.parser.RenderFilter#allowText(com.itextpdf.text.pdf.parser.TextRenderInfo)
*/
public override bool AllowImage(ImageRenderInfo renderInfo)
{
var matrix = renderInfo.GetImageCTM();
float left = matrix[6];
float top = matrix[7];
float width = matrix[0];
float height = matrix[4];
return filterRect.IntersectsLine(left, top, width, height);
}
}
Code calling :
RectangleJ rect = new RectangleJ(518.0f, 18.0f, 23.0f, 23.0f);
PdfReaderContentParser parser2 = new PdfReaderContentParser(pdfReader);
RenderFilter[] renderFilter = new RenderFilter[1];
renderFilter[0] = new MyRegionTextRenderFilter(rect);
FilteredTextRenderListener listener2 = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter);
parser2.ProcessContent(3, listener2);
The parser namespace of iText(Sharp) allows filtering of information digested by an IRenderListener implementation by using a RenderFilter:
public abstract class RenderFilter {
/**
* #param renderInfo
* #return true if the text render operation should be performed
*/
public virtual bool AllowText(TextRenderInfo renderInfo){
return true;
}
/**
*
* #param renderInfo
* #return true is the image render operation should be performed
*/
public virtual bool AllowImage(ImageRenderInfo renderInfo){
return true;
}
}
For filtering by area there already is a textual render filter, the RegionTextRenderFilter.
For your task simply copy it and add an AllowImage(ImageRenderInfo renderInfo) implementation similar to the existing AllowText(TextRenderInfo renderInfo) method.

I have a modal Window implemented in SmartGWT - how can I close it when someone clicks off of the window?

I've created a class that extends the Window SmartGWT class, and it is set to be modal. I am trying to make the Window close when a user clicks off the window. I have tried to link it up to a FocusChangedHandler with no luck. Has anyone done something like this before?
/**
* Sets up a modal Dialog box that lets the user edit attributes associated
* with the properties of the {#link LabElement} that are given.
*
* #author Therin Irwin
*/
public class EditorDialog extends Window {
final DynamicForm dyn = new DynamicForm();
final RichTextEditor richTextEditor = new RichTextEditor();
final List attrItems = new ArrayList();
/**
* Creates a new EditorDialog with a RichTextEditor and a list of
* attributes for the element.
*
* #param name the name of the element being edited.
* #param attr the List of String attributes of the element that can be
* edited.
* #param hasText true if the element supports text inside, false if not.
*/
public EditorDialog(String name, List attr, boolean hasText) {
super();
VLayout vert = new VLayout();
this.setShowMinimizeButton(false);
this.setIsModal(true);
this.setShowModalMask(true);
this.setTitle(name + " Editor");
richTextEditor.setWidth(550);
richTextEditor.setHeight(100);
richTextEditor.setPadding(5);
richTextEditor.setCanDragResize(true);
richTextEditor.setResizeFrom("B");
richTextEditor.setShowEdges(true);
if (attr == null || attr.size() == 0) {
richTextEditor.setHeight(300);
}
else {
int i = 0;
FormItem[] fi = new FormItem[attr.size()];
for (String at : attr) {
TextItem temp = new TextItem(at, at);
attrItems.add(temp);
fi[i++] = temp;
}
dyn.setFields(fi);
dyn.setPadding(5);
dyn.setTop(100);
}
if (hasText)
vert.addMember(richTextEditor);
if (!(attr == null || attr.size() == 0))
vert.addMember(dyn);
this.addItem(vert);
this.centerInPage();
this.setAutoSize(true);
}
/**
* Returns the text of the RichTextEditor.
*
* #return the text entered into the RichTextEditor.
*/
public String getRichText() {
return richTextEditor.getValue();
}
/**
* Sets the text in the RichTextEditor to String value.
*
* #param value the String to put as the contents of the RichTextEditor.
*/
public void setRichText(String value) {
richTextEditor.setValue(value);
}
/**
* Returns the List of TextItems that hold the user-entered values for
* attributes.
*
* #return the TextItems associated with each attribute, in order.
*/
public DynamicForm getFormItems() {
return dyn;
}
public TextItem getFormItem(int item) {
return (TextItem) dyn.getFields()[item];
}
}
#Therin
I guess according to your requirement, you need to implement this property of Window:
this.setDismissOnOutsideClick(true);

Resources