Android视频和图片大小调整

时间:2015-05-21 17:33:02

标签: android image video compression

我正在寻找一种方法来降低图像和视频的大小,然后我将它们上传到服务器,此时我发送的文件太大而且我似乎找不到任何有关如何压缩文件的解决方案文件,我知道whatsapp和facebook等应用程序会在几秒内压缩视频并将其缩小90%,任何人都有建议,方向我会很感激。

谢谢。

2 个答案:

答案 0 :(得分:2)

我今天确切需要,所以想出了这段代码。发送此函数一个位图,它将返回压缩图像的文件路径(本地)。然后使用返回的文件路径创建一个File对象并将其发送到服务器:

 public File compressImage(Bitmap bmp) {
        Bitmap scaledBitmap = null;

        int actualHeight = bmp.getHeight();
        int actualWidth = bmp.getWidth();

//      max Height and width values of the compressed image is taken as 816x612
        float imgRatio = actualWidth / actualHeight;
        float maxRatio = logoMaxWidth / logoMaxHeight;
//      width and height values are set maintaining the aspect ratio of the image
        if (actualHeight > logoMaxHeight || actualWidth > logoMaxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = logoMaxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) logoMaxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = logoMaxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) logoMaxWidth;
            } else {
                actualHeight = (int) logoMaxHeight;
                actualWidth = (int) logoMaxWidth;
            }
        }
        try {
            scaledBitmap = Bitmap.createScaledBitmap(bmp, actualWidth, actualHeight, true);
        } catch (OutOfMemoryError exception) {
            logoUploadFaied();
            exception.printStackTrace();
        }
        String uriSting = (System.currentTimeMillis() + ".jpg");
        File file = new File(Environment.getExternalStorageDirectory()
                + File.separator + uriSting);
        try {
            file.createNewFile();
        } catch (IOException e) {
            logoUploadFaied();
            e.printStackTrace();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            logoUploadFaied();
            e.printStackTrace();
        }
        try {
            fos.write(getBytesFromBitmap(scaledBitmap));
            fos.close();
            //recycling bitMap to overcome OutOfMemoryError
            if(scaledBitmap!=null){
                scaledBitmap.recycle();
                scaledBitmap=null;
            }
        } catch (IOException e) {
            logoUploadFaied();
            e.printStackTrace();
        }
        return file;
    }

答案 1 :(得分:2)

您可以使用ffmpeg(一个开源视频库)进行压缩。

请注意,视频压缩通常非常强大,因此除了小视频外,它不太可能只需“几秒钟”。

有很多方法可以在您的应用程序中包含ffmpeg。看一下这些“包装”项目以获取以太使用的示例,或者帮助您构建自己的ffmpeg库:

例如,一旦您选择或构建了一个ffmpeg包装器,以下代码将压缩mp4视频文件(默认压缩)(此代码使用自定义ffmpeg库但它应该为您提供概述,您应该能够替换上面的一个例子):

public class VideoCompressionTask extends AsyncTask<String, String, String> {
/* This Class is an AsynchTask to compress a video on a background thread
 * 
 */

@Override
protected String doInBackground(String... params) {
    //Compress the video in the background

    //Get the the path of the video to compress
    String videoPath;
    String videoFileName;
    File videoFileToCompress;
    if (params.length == 1) {
        videoPath = params[0];
        videoFileToCompress = new File(videoPath);
        videoFileName = videoFileToCompress.getName();
    } else {
        //One or all of the params are not present - log an error and return
        Log.d("VideoCompressionTask","doInBackground wrong number of params");
        return null;
    }

    //Make sure the video to compress actually exists
    if(!videoFileToCompress.exists()) {
        Log.d("VideoCompressionTask","doInBackground video file to compress does not exist");
        return null;
    }

    //If the compressed file already exists then delete it first and let this task create a new one
    File compressedVideoFile = new File(compressedFilePath);
    if(compressedVideoFile.exists()) {
        compressedVideoFile.delete();
    }

    String argv[] = {"ffmpeg", "-i", videoPath, "-strict", "experimental", "-acodec", "aac", compressedFilePath};
    int ffmpegWrapperreturnCode = FfmpegJNIWrapper.call_ffmpegWrapper(appContext, argv);
    Log.d("VideoCompressionTask","doInBackground ffmpegWrapperreturnCode: " + ffmpegWrapperreturnCode);

    return(compressedFilePath);
}