我正在制作相机应用程序。并且我必须在相机点击服务器之后保存图像,因为捕获的图像的大小总是非常大(以Mb为单位)。因此,将大尺寸图像保存在服务器上总是很困难。是否有任何压缩图像保存之前。
我必须只使用android原生相机
由于
答案 0 :(得分:2)
您需要在将位图实际上传到服务器之前调整位图大小。 此代码返回调整大小的位图。将位图缩小到所需的宽度和所需的高度 - 这将导致图像文件小得多。
public static Bitmap getBitmapImages(final String imagePath, final int requiredWidth, final int requiredHeight)
{
System.out.println(" --- image_path in getBitmapForCameraImages --- "+imagePath+" - reqWidth & reqHeight "+requiredWidth+" "+requiredHeight);
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inJustDecodeBounds = true;
// First decode with inJustDecodeBounds=true to check dimensions
bitmap = BitmapFactory.decodeFile(imagePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
options.inJustDecodeBounds = false;
// Decode bitmap with inSampleSize set
bitmap = BitmapFactory.decodeFile(imagePath, options);
return bitmap;
}
答案 1 :(得分:1)
另一种方法是直接制作较小的照片。优点是您使用较少的内存,但您可能需要在应用程序的另一部分中使用大图。
这可以按如下方式完成:
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
...
Camera.Parameters mParameters = mCamera.getParameters();
List<Size> sizes = mParameters.getSupportedPictureSizes();
Size optimalSize = getOptimalSize(sizes, width, height);
if (optimalSize != null && !mParameters.getPictureSize().equals(optimalSize))
mParameters.setPictureSize(optimalSize.width, optimalSize.height);
...
}
要选择最佳尺寸,您可以使用您想要的任何标准。我试图让它尽可能接近屏幕尺寸:
private Size getOptimalSize(List<Size> sizes, int w, int h){
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Size size: sizes)
{
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
if (optimalSize == null)
{
minDiff = Double.MAX_VALUE;
for (Size size: sizes)
{
if (Math.abs(size.height - targetHeight) < minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
答案 2 :(得分:0)
试试这个
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);