Coloring JavaFX progress bar in fxml.

In case if you want to change color of progress bar in JavaFx, but don’t want to do it by css or by java code, there is a possibility to change it by adding ColorAdjust affect to it.

For green progressbar:

<ProgressBar progress="0.0">
    <effect>
        <ColorAdjust hue="-0.50"/>
    </effect>
</ProgressBar>

For red progressbar:

<ProgressBar progress="0.0">
    <effect>
        <ColorAdjust hue="0.80"/>
    </effect>
</ProgressBar>

Any color can be achieved by changing hue attribute.

ZXing with Android quick start.

ZXing(https://code.google.com/p/zxing/) is a great library for barcode image processing, which can be used for Android apps development. There are many tutorials for integrating it, but i have not found proper one so far(don’t think that copying the whole source code of example app to your project is such a great idea to do http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/). You’ll need ZXing core lib in project for it to work.

The simple usage of this lib can be described in this steps:

  1. Get data from preview callback
  2. Convert it to PlanarYUVLuminanceSource
  3. Binarize PlanarYUVLuminanceSource and use MultiFormatReader to get results.

That’s it! Sound simple, isn’t it? Of course there is some more work to be done (there is no need to use full sized image, small rectangle area will do the trick), but those are main steps.

Capture activity is as simple as it can be and is used only to create camera instance and start previewing camera data. It also request frame for the first time for ZXing to try find barcode on it.

public class CaptureActivity extends Activity {
    private CameraPreviewView cameraPreview;
    private CameraManager cameraManager;
    private Handler captureHandler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capture);
        // Create an instance of Camera
        cameraManager = new CameraManager();
        captureHandler = new CaptureHandler(cameraManager, this, new OnDecoded());
        //requesting next frame for decoding
        cameraManager.requestNextFrame(new PreviewCallback(captureHandler, cameraManager));
        cameraPreview = (CameraPreviewView) findViewById(R.id.camera_preview);
        cameraPreview.setCameraManager(cameraManager);
        ((BoundingView) findViewById(R.id.bounding_view)).setCameraManager(cameraManager);
    }
    ...
}

Camera instance focuses with Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO, so there is no need to implement focusing by hands. But there is no need to try to find barcode on preview every time app retrieves camera preview(this is too bad for performance), so it’s a good idea to run one decoding at a time. It can be done with Camera.setOneShotPreviewCallback(Camera.PreviewCallback). If decoding is successful, than there is no need to setOneShotPreviewCallback again, otherwise setOneShotPreviewCallback must be called again.

Here is example of Camera.PreviewCallback implementation:

public class PreviewCallback implements Camera.PreviewCallback {
    ...
    private final MultiFormatReader multiFormatReader = new MultiFormatReader();
    ...
    private CameraManager cameraManager;
    ...
    @Override
    public void onPreviewFrame(byte[] bytes, Camera camera) {
        Camera.Size previewSize = camera.getParameters().getPreviewSize();
        new DecodeAsyncTask(previewSize.width, previewSize.height).execute(bytes);
    }
    /**
     * Asynchronous task for decoding and finding barcode
     */
    private class DecodeAsyncTask extends AsyncTask<byte[], Void, Result> {
        private int width;
        private int height;
        /**
         * @param width  Width of image
         * @param height Height of image
         */
        private DecodeAsyncTask(int width, int height) {
            this.width = width;
            this.height = height;
        }
        ...
        @Override
        protected Result doInBackground(byte[]... datas) {
            if (!cameraManager.hasCamera()) {
                return null;
            }
            Result rawResult = null;
            final PlanarYUVLuminanceSource source =
                    cameraManager.buildLuminanceSource(datas[0], width,
                            height, cameraManager.getBoundingRect());
            if (source != null) {
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
                try {
                    rawResult = multiFormatReader.decodeWithState(bitmap);
                } catch (ReaderException re) {
                    // nothing to do here
                } finally {
                    multiFormatReader.reset();
                }
            }
            return rawResult;
        }
    }
}

MultiFormatReader is in ZXing lib and is used to decoding different barcode formats at the same time.

And building PlanarYUVLuminanceSource is pretty simple too:

return new PlanarYUVLuminanceSource(data, height, width, boundingRect.top, boundingRect.left, boundingRect.height(), boundingRect.width(), false);

where boundingRect is rectangle area of image to search barcode on.

Source of this example simple app:
https://github.com/ShyykoSerhiy/ZXingQuickStart

GWT Tips: Debugging JSNI methods via Intellij Idea 12 and Google Chrome

Since JSNI methods are “pure” JS it’s not possible to debug them in Idea, but there is a possibility to do it in browsers which have dev tools (Chrome, Safari, Firefox, Opera…). Compiled sources can be easily accessed in dev tools, but what if you don’t want to compile whole project to debug part of jsni code?

You can debug jsni code in gwt dev mode in browser. Let’s assume that your jsni method looks something like this:

public native void jsniMethodToDebug(String stringToShow) /*-{
    $wnd.alert(stringToShow);
}-*/;

To debug this part of code you need to make some small steps:
– open your gwt dev mode page in Chrome;
– press Shift+Ctrl+I for developer tools;
– go to tab sources;
– open you Idea and place breakpoint on method;

public native void jsniMethodToDebug(String stringToShow)

(breakpoint can be also placed on method call in code jsniMethodDebug(“SomeValue”) if you experience problems with debug performance)
– when IDE hangs on method breakpoint, go back to chrome and click “Pause script execution” or F8;
– go back to IDE and continue execution of code
– now go back to chrome and you’ll see your jsni method there.

Have a nice debug!