您的位置:首页 > 其它

旋转图片并保存

2015-12-16 16:18 691 查看
点击旋转控件实现图片的旋转及预览,点击保存控件将旋转后的图片存储起来

int current=0.0f;

Bitmap bigimage=compressImageFromFile(图片所在路径);

imageFiler为显示图片的控件ImageView

String strPath = Environment.getExternalStorageDirectory().getPath() + "/微途/" + System.currentTimeMillis() + ".jpg";

旋转控件的点击事件中:

current=current+90f;

Bitmap bm =rotaingImageView((int)current,bigimage);

imageFiler .setImageBitmap(bm);

保存控件的点击事件中:

saveImage();

/**
*
* 压缩图片
*
* @param srcPath
* @return
*/
public Bitmap compressImageFromFile(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;// 只读边,不读内容
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);

newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 800f;//
float ww = 480f;//
int be = 1;
if (w > h && w > ww) {
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// 设置采样率

newOpts.inPreferredConfig = Config.ARGB_8888;// 该模式是默认的,可不设
newOpts.inPurgeable = true;// 同时设置才会有效
newOpts.inInputShareable = true;// 。当系统内存不够时候图片自动被回收

bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
// return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩
// 其实是无效的,大家尽管尝试
return bitmap;
}


//旋转图片
public Bitmap rotaingImageView(int angle , Bitmap bitmap) {
//旋转图片 动作
Matrix matrix = new Matrix();
matrix.postRotate(angle);
// 创建新的图片
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}


/**
* 保存图片
*/
private void saveImage() {

Bitmap bitmap =  ((BitmapDrawable)imageFiler.getDrawable()).getBitmap();
try {
saveBitmapToFile(bitmap,strPath);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

this.finish();
}


public  void saveBitmapToFile(Bitmap bitmap, String _file)
throws IOException {
BufferedOutputStream os = null;

try {
File file = new File(_file);
// String _filePath_file.replace(File.separatorChar +
// file.getName(), "");
int end = _file.lastIndexOf(File.separator);
String _filePath = _file.substring(0, end);
File filePath = new File(_filePath);
if (!filePath.exists()) {
filePath.mkdirs();
}
file.createNewFile();
os = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
Log.e("保存失败", e.getMessage(), e);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: