Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在不影响位图质量的情况下降低位图质量';s码_Android_Bitmap_Bitmapfactory - Fatal编程技术网

Android 如何在不影响位图质量的情况下降低位图质量';s码

Android 如何在不影响位图质量的情况下降低位图质量';s码,android,bitmap,bitmapfactory,Android,Bitmap,Bitmapfactory,有没有一种方法可以在android中将115kb的图像压缩为4kb而不影响其大小?。只是降低它的质量 我只知道使用 BitmapFactory。可减少大小和质量的选项 Bitmap.compress,它不提供以字节为单位指定大小的选项 公共位图压缩图像(字符串图像路径){ BitmapFactory.Options=new-BitmapFactory.Options(); options.inJustDecodeBounds=true; bmp=BitmapFactory.decodeStr

有没有一种方法可以在android中将115kb的图像压缩为4kb而不影响其大小?。只是降低它的质量

我只知道使用

  • BitmapFactory。可减少大小和质量的选项

  • Bitmap.compress,它不提供以字节为单位指定大小的选项

公共位图压缩图像(字符串图像路径){

BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
bmp=BitmapFactory.decodeStream(新文件输入流(imagePath),null,选项);
options.inSampleSize=calculateInSampleSize(选项、实际宽度、实际高度);
options.inJustDecodeBounds=false;
bmp=BitmapFactory.decodeStream(新文件输入流(imagePath),null,选项);
返回bmp;
}
公共int-calculateInSampleSize(BitmapFactory.Options选项、int-reqWidth、int-reqHeight){
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
inSampleSize=高度比<宽度比?高度比:宽度比;
}
最终浮点总数像素=宽度*高度;
最终浮点totalReqPixelsCap=reqWidth*reqHeight*2;
而(totalPixels/(inSampleSize*inSampleSize)>totalReqPixelsCap){
inSampleSize++;
}
返回样本大小;
}

图像重新调整大小意味着您将缩短图像的分辨率。假设用户选择1000*1000像素的图像。您要将图像转换为300*300图像。因此,图像大小将减小

图像压缩是在不影响分辨率的情况下降低图像的文件大小。当然,降低文件大小会影响图像质量。有很多压缩算法可以在不影响图像质量的情况下减小文件大小

我在这里找到的一个简便方法是:

    Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.PNG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

Log.e("Original   dimensions", original.getWidth()+" "+original.getHeight());
Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());
给予

12-07 17:43:36.333:E/原始尺寸(278):1024 768 12-07

17:43:36.333:E/压缩尺寸(278):1024 768

int-dimension=getSquareCropDimensionForBitmap(位图)


它可以裁剪和缩小尺寸您可以指定自己的尺寸

为什么需要这个?为了避免呕吐?这不会有帮助的。。。从中解码位图的文件是4KB还是1MB并不重要。。。位图对象将占用相同的内存量,这仅取决于位图的宽度和高度以及像素format@Selvin我不需要这个来避免OOME。实际上,我正在尝试实现一个像whatsapp这样的图像共享应用程序,用户可以在下载之前看到图像的样子。因此,我正在考虑通过谷歌GCM向用户发送一张与原始图像尺寸相同但质量较低的图像,这将数据加载限制为4kb:(没有其他方法可以像尝试
bitmap.compress(bitmap.CompressFormat.JPG,q)那样)
使用不同的
q
值…但您可以尝试将文件拆分为4kb图片…或使用Google drive API(或任何其他可能的存储)只发送link@Selvin如果我使用这种方法,我无法判断图像字节是否超过4kb。如果它们超过4kb,并且我通过google GCM发送它们,我会收到错误消息Toobighuh?为什么…
for(int q=100;q>0;q--){OutputStream out=getOutputFromFile(file);bitmap.compress(bitmap.CompressFormat.JPG,q,out);if(checkFileSize(file)<4000){sendFile(file);break;}}
…当然这不是最佳方式(更像暴力)此外,这可能需要很长时间…我想要一个与原始图像大小相同但质量较低的压缩图像。我正在尝试实现像whatsapp这样的图像共享,如果我向您发送图像,您将在从服务器下载之前看到图像的外观。这意味着我必须向正在考虑使用goo的用户发送低质量图像gle的GCM不允许数据加载超过4kbwell在这种情况下为什么不试试这个:我会尝试一下,并给你回复。在我的情况下,上面的解决方案能够降低质量,但字节数仍然很大,所以我最终使用了
bitmap.CompressFormat.JPEG(bitmap.CompressFormat.JPEG,10,outputStream)
,这对我来说还行
    Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.PNG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

Log.e("Original   dimensions", original.getWidth()+" "+original.getHeight());
Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());
public static int getSquareCropDimensionForBitmap(Bitmap bitmap)
{
    int dimension;
    //If the bitmap is wider than it is tall
    //use the height as the square crop dimension
    if (bitmap.getWidth() >= bitmap.getHeight())
    {
        dimension = bitmap.getHeight();
    }
    //If the bitmap is taller than it is wide
    //use the width as the square crop dimension
    else
    {
        dimension = bitmap.getWidth();
    }

return  dimension;
}
        System.out.println("before cropped height " + bitmap.getHeight() + "and width: " + bitmap.getWidth());
        Bitmap croppedBitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);
        System.out.println("after cropped height "+croppedBitmap.getHeight() +"and width: " + croppedBitmap.getWidth());