PASSING AND RETRIVING IMAGE IN JSON USING JAVAFX
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import javax.swing.JOptionPane;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONException;
import org.json.JSONObject;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class ImageChooser extends Application
{
private List<File> list;
private String imgpath;
private HBox hb;
private BorderPane bn;
private Stage stagenew;
private Button b1;
private static String imgname;
@Override
public void start(Stage paramStage) throws Exception {
stagenew=paramStage;
imgpath="D:\\Ragava\\ragava\\workspace\\Sample\\src\\defimage.jpg";
//BUTTON EVENT TO TRIGGER ACTION
b1=new Button("IMAGE FILE CHOOSER");
b1.setOnAction(new EventHandler() {
public void handle(Event arg0)
{
// TODO Auto-generated method stub
addMultipleBooks();
try {
String imageDataString=ImageToString();
StrTOJson(imageDataString);
String imgdata=ReadJson();
DecodeProcesss(imgdata);
GenerateScene();
JOptionPane.showMessageDialog(null,"IMAGE SUCCESSFULLY UPLODED TO JSON...");
System.out.println("Image Successfully Manipulated!");
} catch (FileNotFoundException e) {
System.out.println("Image not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the Image " + ioe);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
GenerateScene();
}
//JAVAFX SCENCE CREATION
public void GenerateScene()
{
bn=new BorderPane();
bn.setBottom(b1);
hb=new HBox();
ImageView iv1=ImageSetup();
System.out.println("FINISHED..");
hb.getChildren().addAll(iv1);
System.out.println("hbox addedd..");
bn.setCenter(hb);
System.out.println("centered..");
Scene sc=new Scene(bn);
stagenew.setScene(sc);
stagenew.show();
}
private ImageView ImageSetup()
{
System.out.println("ENTERED..");
File file = new File(imgpath);
Image image = new Image(file.toURI().toString());
ImageView iv1 = new ImageView();
iv1.setImage(image);
return iv1;
}
private void addMultipleBooks()
{
FileChooser fileChooser = new FileChooser();
configureFileChooser(fileChooser);
list = fileChooser.showOpenMultipleDialog(null);
imgpath=list.get(0).getAbsolutePath();
System.out.println(imgpath);
JOptionPane.showMessageDialog(null,imgpath);
}
private static void configureFileChooser(final FileChooser fileChooser) {
fileChooser.setTitle("View Pictures");
fileChooser.setInitialDirectory(new File(System
.getProperty("user.home")));
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Files", "*.jpg", "*.png"),
new FileChooser.ExtensionFilter("JPG", "*.jpg"),
new FileChooser.ExtensionFilter("PNG", "*.png"));
}
// READING JSON FILE
public static String ReadJson()
{
String imagbin="";
String srcUrl = "D://imgeparser.json";
String line;
String jsondata="";
StringBuffer stringBuffer = new StringBuffer();
JSONObject jsonObj=null,jsonOut=null;
try {
File file = new File(srcUrl);
if (file.exists()) {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
jsondata=stringBuffer.toString();
}
jsonObj = new JSONObject(jsondata);
String status = jsonObj.getString("Result");
System.out.println("JSON RESPONSE.."+status);
if (status.equals("success"))
{
imagbin=jsonObj.getString("image-bin");
System.out.println("image binary.."+imagbin);
String imgname=jsonObj.getString("imagename");
System.out.println("image name.."+imgname);
System.out.println("READED SUCCESSFULLY..");
}
else
{
System.out.println("ERROR IN RATE..");
}
} catch (Exception e) {
e.printStackTrace();
}
return imagbin;
}
//CREATING JSON FILE
public static void StrTOJson(String imagetxt) throws JSONException
{
JSONObject obj = new JSONObject();
obj.put("imagename",imgname);
obj.put("image-bin", imagetxt);
obj.put("Result","success");
try {
FileWriter file = new FileWriter("D://imgeparser.json");
file.write(obj.toString());
System.out.println("Successfully Copied JSON Object to File...");
System.out.println("\nJSON Object: " + obj);
file.close();
}
catch(Exception ex)
{
System.out.println("ERROR.."+ex.getMessage());
}
}
//CONVERTING IMAGE TO STRING USING ENCODING AND DECODING PROCESS
public String ImageToString() throws IOException
{
//File file = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulips.jpg");
File file = new File(imgpath);
imgname=file.getName();
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
imageInFile.close();
String imageDataString = encodeImage(imageData);
System.out.println("Image String.."+imageDataString);
return imageDataString;
}
//DECODING IMAGE
public void DecodeProcesss(String imageDataString) throws IOException
{
byte[] imageByteArray = decodeImage(imageDataString);
FileOutputStream imageOutFile = new FileOutputStream(
"D:\\JSON IMAGE\\"+imgname);
imageOutFile.write(imageByteArray);
// Converting a Base64 String into Image byte array
imageOutFile.close();
}
//ENCODE IMAGE
public static String encodeImage(byte[] imageByteArray) {
return Base64.encodeBase64URLSafeString(imageByteArray);
}
//DECODE IMAGE
public static byte[] decodeImage(String imageDataString) {
return Base64.decodeBase64(imageDataString);
}
public static void main(String[] args)
{
launch(args);
}
}
No comments:
Post a Comment