Wednesday, August 5, 2015

C# Convert image to hex and vice versa

    Here I want to show you how to convert image to hex format and vice versa  using C# language

To convert image to hex data we need only one line of code.
public void ConvertImageToHex(string filePath){
    return BitConverter.ToString(File.ReadAllBytes(fName));
} 
To bring back hex string to image we need to do a little bit more
// saves hex string image to the pathToSave
public static void SaveImageFromHex(string hex, string pathToSave)
{
    // Call function to Convert the hex data to byte array
    byte[] newByte = ToByteArray(hexImgData);
    MemoryStream memStream = new MemoryStream(newByte);

    // Save the memorystream to file
    Bitmap.FromStream(memStream).Save(pathToSave);
}

// Function converts hex data into byte array
private static byte[] ToByteArray(String HexString)
{
    int NumberChars = HexString.Length;
    byte[] bytes = new byte[NumberChars / 2];

    for (int i = 0; i < NumberChars; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
    }
    return bytes;
}
original posts: http://www.nullskull.com/faq/40/convert-hex-image-data-to-jpeg.aspx http://stackoverflow.com/questions/18717182/convert-jpeg-image-to-hex-format

JavaFX Table view

I'm writing this post just to remember to myself how did I achieved some goals.
So this first is about JavaFX Table view.
// Allow TableView select multiply rows  
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

//Allow TableView to select single cell     
tableView.getSelectionModel().setCellSelectionEnabled(true);

//TableColumn<Batch, String> codeColumn;
codeColumn.setCellValueFactory(new PropertyValueFactory<Batch, String>(
                "code"));

//Make the codeColumn editable
codeColumn.setCellFactory(TextFieldTableCell.<Batch> forTableColumn());

//Register listner on finish editing cell
codeColumn.setOnEditCommit(value -> {
            System.out.println(value.getRowValue().getCode());
        });

// Make column resizable
codeColumn.prefWidthProperty()
                .bind(tableView.widthProperty().divide(3));



nameColumn.setCellValueFactory(new PropertyValueFactory<Batch, String>(
                "name"));

//If you have some double field than you have to
// use DoubleStringConverter from javafx.util.converters  
nameColumn.setCellFactory(TextFieldTableCell
                .<Batch, String> forTableColumn(new DefaultStringConverter()));

nameColumn.setOnEditCommit(value -> {
            System.out.println(value.getNewValue());
            System.out.println(value.getRowValue().getCode());
        });


nameColumn.prefWidthProperty().bind(tableView.widthProperty().divide(3));


btnCol.setMinWidth(150);


//Creating column which holds buttons
btnCol.setCellValueFactory(new Callback<CellDataFeatures<Batch, Batch>, ObservableValue<Batch>>() {
            @Override
            public ObservableValue<Batch> call(
                    CellDataFeatures<Batch, Batch> features) {
                return new ReadOnlyObjectWrapper(features.getValue());
            }
        });

btnCol.prefWidthProperty().bind(tableView.widthProperty().divide(3));

//Fill column with buttons
btnCol.setCellFactory(new Callback<TableColumn<Batch, Batch>, TableCell<Batch, Batch>>() {
            @Override
            public TableCell<Batch, Batch> call(TableColumn<Batch, Batch> btnCol) {
                return new TableCell<Batch, Batch>() {
                    final Button button = new Button();
                    {
                        button.setMinWidth(130);
                        button.setText("save");
                    }

                    @Override
                    public void updateItem(final Batch person, boolean empty) {
                        super.updateItem(person, empty);
                        //Setting user data to Model
                        button.setUserData(person);
                        setGraphic(button);
                        button.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                              
                                Button btnButton = (Button)event.getSource();
                                //Retrieving user data which is Batch
                                Batch batch = (Batch)btnButton.getUserData();
                                System.out.println(batch.getCode());
                                System.out.println(batch.getName());
                            }
                        });
                    }
                };
            }
        });

//Filling table with data 
tableView.setItems(new BatchService().list());