62,614
社区成员
发帖
与我相关
我的任务
分享
String hello = "Samsung";
package com.yaxh.picCut;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;
public class BitmapUtil {
/**
* 压缩图片 如果图片宽高有一个大于2000,并且压缩比例小于等于2,则调为4 如果图片宽高小于1000*1000则不压缩
*
* @param orgImagePath
* 图片原路径
* @param desertImagePath
* 压缩图片保存路径
* @param scale
* 压缩比例,为2,4,8
* @return true,压缩成功;false,压缩失败。
*/
public static boolean compressImageFile(String orgImagePath, String desertImagePath, int scale) {
CompressFormat format = Bitmap.CompressFormat.JPEG;
OutputStream stream = null;
Bitmap bitmap = null;
boolean b = false;
try {
int quality = 100;
File file = new File(orgImagePath);
long fileSize = file.length();
if (200 * 1024 < fileSize && fileSize <= 1024 * 1024) {
quality = 75;
} else if (1024 * 1024 < fileSize) {
quality = 70;
}
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(orgImagePath, o);
// 如果图片宽高大于2000,并且压缩比例小于等于2,则调为4
int width_tmp = o.outWidth, height_tmp = o.outHeight;
if ((width_tmp > 2000 || height_tmp > 2000) && scale <= 2) {
scale = 4;
}
// 如果图片小于1000*1000则不压缩
if (width_tmp < 1000 && height_tmp < 1000) {
scale = 1;
}
if (scale == 1 && quality == 100) {
File file2 = new File(desertImagePath);
boolean b2 = copyfile(file, file2);
Log.i("xpf", "desertImagePath=" + desertImagePath);
Log.i("xpf", "无损压缩" + b2 + "原图大小=" + file.length() / 1024 + "KB");
return b2;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scale;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
bitmap = BitmapFactory.decodeFile(orgImagePath, options);
// 旋转图片
int degrees = readPictureDegree(orgImagePath);
if (degrees != 0 && bitmap != null) {
Log.i("xpf", "图片旋转=" + degrees);
Matrix m = new Matrix();
m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
}
stream = new FileOutputStream(desertImagePath);
if (bitmap != null) {
b = bitmap.compress(format, quality, stream);
File file2 = new File(desertImagePath);
Log.i("xpf", "desertImagePath=" + desertImagePath);
Log.i("xpf", "scale=" + scale + " 图片质量=" + quality + " 原图为" + width_tmp + "*" + height_tmp + " 压缩后为" + options.outWidth
+ "*" + options.outHeight);
Log.i("xpf", "原图大小=" + file.length() / 1024 + "KB" + " 压缩后图片=" + file2.length() / 1024 + "kb" + " 实际压缩率为:" + file2.length()
* 100 / file.length() + "% 标准压缩率为:" + 100 / (scale * scale) + "%");
}
bitmap.recycle();
bitmap = null;
System.gc();
if (stream != null) {
stream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
/**
* 复制文件
*
* @param fromFile
* 源文件路径
* @param toFile
* 目标文件路径
* @return true 复制成功;false 复制失败
*/
public static boolean copyfile(File fromFile, File toFile) {
if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) {
return false;
}
try {
if (!toFile.getParentFile().exists()) {
toFile.getParentFile().mkdirs();
}
toFile.delete();
java.io.FileInputStream fisfrom = new java.io.FileInputStream(fromFile);
java.io.FileOutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[1024];
int c;
while ((c = fisfrom.read(bt)) > 0) {
fosto.write(bt, 0, c); // 将内容写到新文件当中
}
if (fisfrom != null) {
fisfrom.close();
}
fosto.flush();
if (fosto != null) {
fosto.close();
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 获取图片的旋转角度
*
* @Title: readPictureDegree
* @param path
* @return int
* @date 2013-11-27 上午9:22:33
*/
@SuppressWarnings("deprecation")
private static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
//下面这个String的长度比实际的要小,怎么完全读出来?
String orientations = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
StringBuilder sb=new StringBuilder();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 300; i++) {
// sb.append(orientations.charAt(i));
// }
// Log.i("xpf", "sb=" + sb.toString());
byte[] bs = orientations.getBytes();
// byte[] bs2 = new byte[300];
// orientations.getBytes(0, 300, bs2, 0);
char[] chs = orientations.toCharArray();
Log.i("xpf", "chs.leng=" + chs.length);
Log.i("xpf", "chs=" + chs.toString());
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
String string = "csdfa";
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
default:
degree = 0;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
}
[/quote]
在第155行package com.yaxh.picCut;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;
public class BitmapUtil {
/**
* 压缩图片 如果图片宽高有一个大于2000,并且压缩比例小于等于2,则调为4 如果图片宽高小于1000*1000则不压缩
*
* @param orgImagePath
* 图片原路径
* @param desertImagePath
* 压缩图片保存路径
* @param scale
* 压缩比例,为2,4,8
* @return true,压缩成功;false,压缩失败。
*/
public static boolean compressImageFile(String orgImagePath, String desertImagePath, int scale) {
CompressFormat format = Bitmap.CompressFormat.JPEG;
OutputStream stream = null;
Bitmap bitmap = null;
boolean b = false;
try {
int quality = 100;
File file = new File(orgImagePath);
long fileSize = file.length();
if (200 * 1024 < fileSize && fileSize <= 1024 * 1024) {
quality = 75;
} else if (1024 * 1024 < fileSize) {
quality = 70;
}
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(orgImagePath, o);
// 如果图片宽高大于2000,并且压缩比例小于等于2,则调为4
int width_tmp = o.outWidth, height_tmp = o.outHeight;
if ((width_tmp > 2000 || height_tmp > 2000) && scale <= 2) {
scale = 4;
}
// 如果图片小于1000*1000则不压缩
if (width_tmp < 1000 && height_tmp < 1000) {
scale = 1;
}
if (scale == 1 && quality == 100) {
File file2 = new File(desertImagePath);
boolean b2 = copyfile(file, file2);
Log.i("xpf", "desertImagePath=" + desertImagePath);
Log.i("xpf", "无损压缩" + b2 + "原图大小=" + file.length() / 1024 + "KB");
return b2;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scale;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
bitmap = BitmapFactory.decodeFile(orgImagePath, options);
// 旋转图片
int degrees = readPictureDegree(orgImagePath);
if (degrees != 0 && bitmap != null) {
Log.i("xpf", "图片旋转=" + degrees);
Matrix m = new Matrix();
m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
}
stream = new FileOutputStream(desertImagePath);
if (bitmap != null) {
b = bitmap.compress(format, quality, stream);
File file2 = new File(desertImagePath);
Log.i("xpf", "desertImagePath=" + desertImagePath);
Log.i("xpf", "scale=" + scale + " 图片质量=" + quality + " 原图为" + width_tmp + "*" + height_tmp + " 压缩后为" + options.outWidth
+ "*" + options.outHeight);
Log.i("xpf", "原图大小=" + file.length() / 1024 + "KB" + " 压缩后图片=" + file2.length() / 1024 + "kb" + " 实际压缩率为:" + file2.length()
* 100 / file.length() + "% 标准压缩率为:" + 100 / (scale * scale) + "%");
}
bitmap.recycle();
bitmap = null;
System.gc();
if (stream != null) {
stream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
/**
* 复制文件
*
* @param fromFile
* 源文件路径
* @param toFile
* 目标文件路径
* @return true 复制成功;false 复制失败
*/
public static boolean copyfile(File fromFile, File toFile) {
if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) {
return false;
}
try {
if (!toFile.getParentFile().exists()) {
toFile.getParentFile().mkdirs();
}
toFile.delete();
java.io.FileInputStream fisfrom = new java.io.FileInputStream(fromFile);
java.io.FileOutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[1024];
int c;
while ((c = fisfrom.read(bt)) > 0) {
fosto.write(bt, 0, c); // 将内容写到新文件当中
}
if (fisfrom != null) {
fisfrom.close();
}
fosto.flush();
if (fosto != null) {
fosto.close();
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 获取图片的旋转角度
*
* @Title: readPictureDegree
* @param path
* @return int
* @date 2013-11-27 上午9:22:33
*/
@SuppressWarnings("deprecation")
private static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
//下面这个String的长度比实际的要小,怎么完全读出来?
String orientations = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
StringBuilder sb=new StringBuilder();
// StringBuilder sb = new StringBuilder();
// for (int i = 0; i < 300; i++) {
// sb.append(orientations.charAt(i));
// }
// Log.i("xpf", "sb=" + sb.toString());
byte[] bs = orientations.getBytes();
// byte[] bs2 = new byte[300];
// orientations.getBytes(0, 300, bs2, 0);
char[] chs = orientations.toCharArray();
Log.i("xpf", "chs.leng=" + chs.length);
Log.i("xpf", "chs=" + chs.toString());
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
String string = "csdfa";
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
default:
degree = 0;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
}