package imageProcess;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Base64;
import android.util.Log;
public class ImageProcess {
Context context;
public ImageProcess(Context context)
{
this.context = context;
}
public String encodeTobase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.PNG, 20, baos);//100
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
Log.e("imageEncoded", "...."+imageEncoded.length());
immagex.recycle();
return imageEncoded;
}
public Bitmap decodeBase64(String input)
{
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
public Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 140;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(selectedImage), null, o2);
}
public boolean saveImageToInternalStorage(Bitmap image,String filepath) {
ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir(filepath, Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,filepath+".png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
Log.e("save",mypath.toString());
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("save", e.toString());
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("save", e.toString());
return false;
}
}
public Bitmap loadImageFromStorage(String path)
{
try {
ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir(path, Context.MODE_PRIVATE);
File f=new File(directory.getPath(), path+".png");
FileInputStream fInputStream = new FileInputStream(f);
Bitmap b = BitmapFactory.decodeStream(fInputStream);
Log.e("load", "ok");
fInputStream.close();
Log.e("load", "ok");
return b;
}
catch (FileNotFoundException e)
{
Log.e("load", e.toString());
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("load", e.toString());
return null;
}
}
}
沒有留言:
張貼留言