Android 如何减少图片大小?
我想将相机拍摄的照片发送到服务器。但它的大小大约是1米,我相信它是两倍多。所以我想缩小照片的尺寸。下面是我正在使用的方法。Android 如何减少图片大小?,android,image,Android,Image,我想将相机拍摄的照片发送到服务器。但它的大小大约是1米,我相信它是两倍多。所以我想缩小照片的尺寸。下面是我正在使用的方法。 saveFile-只需将位图保存到文件并返回其路径即可。 calculateInSampleSize返回inSampleSize以将照片尺寸调整为所选尺寸。 decodeSampledBitmapFromResource解码文件中的birmap,并使用新参数重新生成。 每件事都很好,但似乎不起作用。small3.png的尺寸为1240*768*24,大小仍为~1M 我称之为
saveFile
-只需将位图保存到文件并返回其路径即可。calculateInSampleSize
返回inSampleSize
以将照片尺寸调整为所选尺寸。decodeSampledBitmapFromResource
解码文件中的birmap,并使用新参数重新生成。每件事都很好,但似乎不起作用。small3.png的尺寸为1240*768*24,大小仍为~1M 我称之为
photo = saveFile(decodeSampledBitmapFromResource(picturePath,
800, 800),3);
方法
public String saveFile(Bitmap bm,int id) {
String file_path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Mk";
File dir = new File(file_path);
if (!dir.exists())
dir.mkdirs();
File file = new File(dir, "smaller" + id + ".png");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file.getPath();
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(String path,
int reqWidth, int reqHeight) {
Log.d("path", path);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
公共字符串保存文件(位图bm,int-id){
字符串文件\u path=Environment.getExternalStorageDirectory()
.getAbsolutePath()+“/Mk”;
文件目录=新文件(文件路径);
如果(!dir.exists())
dir.mkdirs();
文件=新文件(dir,“较小的”+id+“.png”);
FileOutputStream-fOut;
试一试{
fOut=新文件输出流(文件);
bm.compress(Bitmap.CompressFormat.PNG,85,fOut);
fOut.flush();
fOut.close();
}
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回文件.getPath();
}
公共静态int-calculateInSampleSize(BitmapFactory.Options、,
输入要求宽度,输入要求高度){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
//计算高度和宽度与所需高度和宽度的比率
//宽度
最终整数高度比=数学圆((浮动)高度
/(浮动)高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
//选择最小比率作为采样值,这将
//保证
//最终图像的两个尺寸均大于或等于
//要求的高度和宽度。
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
公共静态位图解码SampledBitMapFromResource(字符串路径,
输入要求宽度,输入要求高度){
Log.d(“路径”,路径);
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码文件(路径、选项);
//计算样本大小
options.inSampleSize=计算样本大小(选项、要求宽度、,
(高度);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeFile(路径、选项);
}
为什么要写两次图像?只需一次性完成即可
我做什么
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap myBitmap = BitmapFactory.decodeFile({ImagePath}, options);
FileOutputStream fileOutputStream = new FileOutputStream({ImagePath});
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
myBitmapClose.compress(CompressFormat.JPEG, imageQuality, bos);
if (bos != null) {
bos.flush();
bos.close();
}
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
这肯定会压缩你的思路
myBitmapClose.compress(CompressFormat.JPEG, imageQuality, bos);
其中set imageCompression typeCompressFormat.JPEG这是我的代码…我想减小路径为字符串的图像的大小
String pics=(arraylist.get(position).getImage());
try{
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
//op.inSampleSize = 20; op.outWidth = 25;
op.outHeight = 35;
bmp = BitmapFactory.decodeFile(pics);
image.setImageBitmap(bmp);
}catch(Exception e) {
}
只是习惯于保留原始照片:)