Skip to content

Instantly share code, notes, and snippets.

@bulentsiyah
Last active October 20, 2017 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bulentsiyah/ae0ff321c6095b4d7d6c63c88ac24380 to your computer and use it in GitHub Desktop.
Save bulentsiyah/ae0ff321c6095b4d7d6c63c88ac24380 to your computer and use it in GitHub Desktop.
İmage convert Base64 String (Encoder) --- Base64 String convert Image (Decoder) -- http://www.bulentsiyah.com/android-sending-photos-to-webservice-photo-base64-encoding-and-decoding/
İmage convert Base64 String (Encoder)
int i = 0;
byte[] filepart = new byte[(int) (length - i)];
try {
fs.read(filepart, 0, (int) length - i);
} catch (IOException e) {
e.printStackTrace();
return returnResult;
}
//import org.kobjects.base64.Base64;
String base64String = Base64.encode(filepart);
-----------------------------------------------------------------------------------------
Base64 String convert Image (Decoder)
byte[] decodedString = android.util.Base64.decode( result.data.getBytes(), android.util.Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
imageViewAfter.setImageBitmap(decodedByte);
Best Sending Photos to web service
@SuppressWarnings("resource")
private Result pictureSending(String SendString, String PhotoTaskID) {
Result returnResult=new Result();
returnResult.data=null;
try {
byte[] filepart = new byte[3];
filepart[0] = (byte) 254;
filepart[1] = (byte) 254;
filepart[2] = (byte) 254;
String base64String = Base64.encode(filepart);
Boolean result = WebService.UploadFile(base64String,
PhotoTaskID);
if (!result) {
return returnResult;
}
} catch (Exception exp) {
exp.toString();
}
File fileOut = new File(SendString);
FileOutputStream out = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 3; // one quarter of original size
Bitmap bp = BitmapFactory.decodeFile(SendString, options);
out = new FileOutputStream(fileOut);
bp.compress(Bitmap.CompressFormat.JPEG, 30, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
long length = fileOut.length();
FileInputStream fs = null;
try {
fs = new FileInputStream(fileOut);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
return returnResult;
}
int i = 0;
byte[] filepart = new byte[(int) (length - i)];
try {
fs.read(filepart, 0, (int) length - i);
} catch (IOException e) {
e.printStackTrace();
return returnResult;
}
String base64String = Base64.encode(filepart);
Boolean result = WebService.UploadFile(base64String,
PhotoTaskID);
if (result != false) {
String resultStr = WebService.SaveFile(PhotoTaskID,
String.valueOf(Brightness),String.valueOf(Contrast));
Gson gson=new Gson();
returnResult=gson.fromJson(resultStr,Result.class);
return returnResult;
} else {
return returnResult;
}
}
}
Web service (WCF)
public bool UploadFile(string data, string fotoID)
{
dynamic result = new JObject();
result.error = "";
result.errorDetails = "";
result.info = "";
result.data = null;
try
{
return FileManager.UploadFile(data, new Guid(fotoID));
}
catch (Exception ex)
{
return false;
}
//return result.ToString();
}
public String SaveFile(string fotoID, string Brightness, string Contrast)
{
dynamic result = new JObject();
result.error = "";
result.errorDetails = "";
result.info = "";
result.data = null;
try
{
try
{
Boolean temp = FileManager.SaveUploadedFile(new Guid(fotoID));
}
catch (Exception ex)
{
}
string file = FileManager.GetFilePath(new Guid(fotoID));
Bitmap bitmap = (Bitmap)Image.FromFile(file);
Bitmap bitmap2 = FileManager.ConvertToGrayscale(bitmap, Brightness, Contrast);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitmap2.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
result.data = Convert.ToBase64String(byteImage);
}
catch (Exception ex)
{
result.error = "hata";
result.errorDetails = ex.ToString();
result.data = null;
return result;
}
return result.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment