我正在製作相機應用程序。我必須在照相機點擊服務器之後保存圖像,並且所拍攝圖像的大小始終非常大(以Mb爲單位)。所以我一直很難將大尺寸的圖像保存在服務器上。在保存之前有沒有壓縮圖像?安卓相機圖像壓縮
我必須只使用Android原生相機
感謝
我正在製作相機應用程序。我必須在照相機點擊服務器之後保存圖像,並且所拍攝圖像的大小始終非常大(以Mb爲單位)。所以我一直很難將大尺寸的圖像保存在服務器上。在保存之前有沒有壓縮圖像?安卓相機圖像壓縮
我必須只使用Android原生相機
感謝
另一種方法是直接做更小的圖片。優點是你使用較少的內存,但你可能需要在應用程序的另一部分有一個大的圖片。
這是可以做到如下:
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;
}
你需要真正上傳到服務器之前調整位圖。 該代碼返回一個調整大小的位圖。將位圖縮小到所需的寬度和所需的高度 - 這會導致圖像文件更小。
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;
}
我只是不想要一個位圖基礎。我希望當我點擊任何immage時,它應該被轉換成一個小尺寸的圖像,然後保存。可能嗎?? –
整個問題是我必須將該圖片保存到服務器上,並且必須將其從SDCard中取出,wat的解決方案是? –
試試這個
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
我不想要任何位圖的方式..見相機點擊(Android本機相機)是否有任何位圖????? \ –
@GauravArora如果你想堅持原生相機,唯一的辦法就是調整相機拍攝照片之前的設置。或者,調用位圖來調整大小。 – dumbfingers
應該做什麼調整?爲了得到一個小尺寸的圖像(以千碼爲單位) –
這種方法會在Android本機攝像機中取得成功嗎? –
@GauravArora no。這是爲您定製自己的相機類。 – dumbfingers