Using PIE In Your Code

PIE (Pixel Image Encoder) Use within code.
The Library can be used in your own projects or on a command line. This guide shows how to use the objects and options. See the introduction on how to download the library.

Setup

Java : Import the jar file into the class path of your project.

  • Eclipse : Build Path -> Configure Build Path -> Add Jars etc.
  • Maven : The jar is not on maven, it needs to be added separately.  
Maven
  Folder Structure to store jar. (Windows Example)
  .m2\repository\net\pie\pie\1.3\pie-1.3.jar

Pom.xml

  <dependencies>
    <dependency>
      <groupid>net.pie</groupid>
<artifactid>pie</artifactid> <version>1.3</version> </dependency> </dependencies>

Encoding Configuration Object

Creating an encoding configuration object. There are two ways to create the object.

Using "Pie_Encode_Config" Using a list object to pass in parameters.
List<Object> parameters = Arrays.asList(
new Pie_Encode_Source(source),// File to be encoded
new Pie_Directory(Pie_Utils.getDesktop()),// Optional. Folder to place encoded file.
// See working without a directory.
Level.INFO,// Optional logging level
new Pie_Max_MB(200),// Optional largest file allowed before slicing Default is 500 MB
Pie_Encode_Mode.M_1,// Optional Default is Pie_Encode_Mode.M_2
new Pie_Encryption("my password"),// Optional Encryption. Must be over 5 characters long
Pie_Shape.SHAPE_SQUARE,// Optional Default is Pie_Shape.SHAPE_RECTANGLE
Pie_Option.OVERWRITE_FILE// Optional Default is not to overwrite file.
);
Alternate parameters.
List<Object> parameters = Arrays.asList(
new Pie_Encode_Source(new Pie_Text("Hello Field", "My_File")),// Text to be encoded, File name is optional
new Pie_Directory(Pie_Utils.getDesktop()),// Optional. Folder to place encoded file.
// See working without a directory.
new Pie_Encryption(new File("My Certificate")), // Optional Encryption.
new Pie_Encryption(new Pie_Base64("Base64 String of a certificate")), // Optional Encryption. V1.4+
new Pie_Encryption(new Pie_Base64("Base64 String. Will decode string")), // Optional Encryption. V1.4+
);
Pie_Encode_Config config = new Pie_Encode_Config(parameters);
Using the "Pie_Encode_Config" itself to add parameters
Pie_Encode_Config config = new Pie_Encode_Config(
new Pie_Encode_Source(source),// File to be encoded
new Pie_Directory(Pie_Utils.getDesktop()),// Optional. Folder to place encoded file.
// See working without a directory.
Level.INFO,// Optional logging level
new Pie_Max_MB(200),// Optional largest file allowed before slicing Default is 500 MB
Pie_Encode_Mode.M_1,// Optional Default is Pie_Encode_Mode.M_2
new Pie_Encryption("my password"),// Optional Encryption.
Pie_Shape.SHAPE_SQUARE,// Optional Default is Pie_Shape.SHAPE_RECTANGLE
Pie_Option.OVERWRITE_FILE// Optional Default is not to overwrite file.
);
Alternate parameters.
Pie_Encode_Config config = new Pie_Encode_Config(
new Pie_Encode_Source(new Pie_Text("Hello Field", "My_File")),// Text to be encoded, File name is optional
new Pie_Directory(Pie_Utils.getDesktop()),// Optional Default is desktop. Folder to place encoded file
new Pie_Encryption(new File("My Certificate")), // Optional Encryption.
new Pie_Encryption(new Pie_Base64("Base64 String of a certificate")), // Optional Encryption. V1.4+
);
Using the "Pie_Encoder_Config_Builder"
Pie_Encode_Config config = new Pie_Encoder_Config_Builder()
.add_Encode_Source(source),// File to be encoded
.add_Directory(Pie_Utils.getDesktop()),// Optional. Folder to place encoded file.
// See working without a directory.
.add_Log_Level(Level.INFO),// Optional logging level
.add_Max_MB(200),// Optional largest file allowed before slicing Default is 500 MB
.add_Mode(Pie_Encode_Mode.M_1),// Optional Default is Pie_Encode_Mode.M_2
.add_Encryption("my password"),// Optional Encryption. Must be over 5 characters long
.add_Shape(Pie_Shape.SHAPE_SQUARE),// Optional Default is Pie_Shape.SHAPE_RECTANGLE
.add_Option(Pie_Option.OVERWRITE_FILE)// Optional Default is not to overwrite file.
.build();// Build the configuration object
Alternate parameters.
Pie_Encode_Config config = new Pie_Encode_Config(
.add_Encode_Source(new Pie_Text("Hello Field", "My_File")),// Text to be encoded, File name is optional
.add_Directory(Pie_Utils.getDesktop()),// Optional. Folder to place encoded file.
// See working without a directory.
.add_Encryption(new File("My Certificate")), // Optional Encryption.
.add_Encryption(new Pie_Base64("Base64 String of a certificate")),// Optional Encryption. . V1.4+
.build();// Build the configuration object

Decoding Configuration Object

Creating a decoding configuration object. There are two ways to create the object.

Using "Pie_Decode_Config" Using a list object to pass in parameters.
List<Object> parameters = Arrays.asList(
new Pie_Decode_Source(source),// File to be decoded
new Pie_Directory(Pie_Utils.getDesktop()),// Optional. Folder to place encoded file.
// See working without a directory.
Level.INFO,// Optional logging level
new Pie_Encryption("my password"),// Required if file was encrypted. Use Certificate file or Base64 if required
Pie_Option.OVERWRITE_FILE,// Optional Default is not to overwrite file.
Pie_Option.CONSOLE,// Optional Text only. Sends text to the console, No file will be created
new Pie_PreFix("Decoded_")// Optional sets a prefix to the decoded file.
);
Pie_Decode_Config config = new Pie_Decode_Config(parameters);
Using the "Pie_Decode_Config" itself to add parameters
Pie_Decode_Config config = new Pie_Decode_Config(
new Pie_Decode_Source(source),// File to be decoded
new Pie_Directory(Pie_Utils.getDesktop()),// Optional. Folder to place encoded file.
// See working without a directory.
Level.INFO,// Optional logging level
new Pie_Encryption("my password"),// Required if file was encrypted. Use Certificate file or Base64 if required
Pie_Option.OVERWRITE_FILE,// Optional Default is not to overwrite file.
Pie_Option.CONSOLE,// Optional Text only. Sends text to the console, No file will be created
new Pie_PreFix("Decoded_")// Optional sets a prefix to the decoded file.
);
Using the "Pie_Decoder_Config_Builder"
Pie_Decode_Config config = new Pie_Decoder_Config_Builder()
.add_Decode_Source(source),// File to be decoded
.add_Directory(Pie_Utils.getDesktop()),// Optional. Folder to place encoded file.
// See working without a directory.
.add_Log_Level(Level.INFO),// Optional logging level
.add_Encryption("my password"),// Required if file was encrypted. Use Certificate file or Base64 if required
.add_Option(Pie_Option.OVERWRITE_FILE),// Optional Default is not to overwrite file.
.add_Option(Pie_Option.CONSOLE),// Optional Text only. Sends text to the console, No file will be created
.add_Prefix("Decoded_")// Optional sets a prefix to the decoded file.
.build();

Using "Pie_Encode" and configuration object

To encode a file or text use the "Pie_Encode_Config" object.

new Pie_Encode(config);

The Pie_Encode object can be used to see if the encoding was a success and to collect information

Pie_Encode encoding = new Pie_Encode(config);
if (!encoding.isEncoding_Error())
	System.out.println(encoding.getEncoding_Error_Message());
else
	System.out.println(encoding.getOutput_location()); // Translated Messaging

Advanced : Working without a directory When working without a directory you need to collect the data yourself. Here is an idea of how you can obtain the encoded file. When directory is not used, the temporary directory will be used just incase a zip file is created. You need to delete the zip file if required. In this example, the bufferedimage is converted to bytes. However the bufferedimage is available in "encoded.getOutput_Image()" if you need to use it.

Pie_Encode encoding = new Pie_Encode(config);
if (encoded.isEncoding_Error()) {
	System.out.println(encoded.getEncoding_Error_Message());
	return false;
}

String encoded_image_location = encoded.getOutput_location();	// Check for a location.
if (Pie_Utils.isEmpty(encoded_image_location)) {	// if location is null see if a bufferedimage was created.
	System.out.println("encoded_image_location is null -> Ok");
    if (encoded.getOutput_Image() != null) {
    	System.out.println("Buffered Image Created -> Ok");
        ByteArrayInputStream bytes = encoded.getBufferedImageBytes();	// Convert bufferedimage to bytes.
        // Do something with bytes
        System.out.println("Available Bytes - " + bytes.available());
   }
}else{
    System.out.println(encoded_image_location + " Created -> Ok");	// Use the location of the zip file created
    // Do something with temp file.
    if (new File(encoded_image_location).delete()) {	// Delete zip file
    	System.out.println(encoded_image + " Deleted -> Ok");
    }
}

Using "Pie_Decode" and configuration object

To decode a file or text use the "Pie_Decode_Config" object.

new Pie_Decode(config);

The Pie_Decode object can be used to see if the decoding was a success and to collect information. This object will also show what was decoded. IE Text or File so your code can decide what to do."decode.getOutputStream()" will only be available if the "Directory" was not used.

Pie_Decode decoding = new Pie_Decode(config);
if  (decoding.isDecoding_Error()) {
	System.out.println(decoding.getDecoding_Error_Message());
}else{
  if (decode.getOutputStream() != null) {
  	switch (decoding.getSource_type()) {
		case FILE : 
        	System.out.println("File output"); 
        	System.out.println(decoding.getOutput_location()); 
            break;
		case TEXT : 
          try {
              System.out.println(((ByteArrayOutputStream) decode.getOutputStream()).toString("UTF-8"));
              decode.getOutputStream().close();
          } catch (IOException ignored) { }
          break;
		default : break;
	}
  }

Encryption (Encoding and Decoding)

Using encryption with the PIE library is optional. Files and Text can be encoded without using encryption. There are two ways to use encryption in your code and both are listed below. To see how to use Encryption on a command line see "PIE (Pixel Image Encoder) Encryption (Command Line)". Encryption phrases must be over 5 characters long.

Encryption : Phrase

Using a phrase can be any length and any language including Arabic and Cyrillic characters.

Pie_Encode_Config config = new Pie_Encoder_Config_Builder() ....
    .add_Encryption("Example phrase  عبارة سبيل المثال Пример фразы.")
    .build();

Using a phrase is easy, but needs to be entered every time for encoding and decoding which means anyone can see the phrase before use. Its recommended to store the phrase somewhere safe encrypted then import it when required.

Encryption : Certificate

To create a certificate.

Pie_Certificate certificate = new Pie_Certificate();
certificate.create_Certificate(new File(.... Folder to store certificate) ); 

If you want the file to use in your own software use this
File certificate_file = certificate.create_Certificate(
    new File(.... Folder to store certificate) );

To verify the certificate.

Pie_Certificate certificate = new Pie_Certificate();
if (!certificate.verify_Certificate(certificate_file or Base64 String of the certificate))
	System.out.println("Invalid Certificate");
else
	System.out.println("Valid Certificate");

Using a certificate.

Pie_Encode_Config config = new Pie_Encoder_Config_Builder() ....
    .add_Encryption(certificate_file) 
    .build();