首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android视频和图像大小调整

问Android视频和图像大小调整
EN

Stack Overflow用户
提问于 2015-05-22 01:33:02
回答 2查看 5.8K关注 0票数 1

我正在寻找一种方法,以降低图像和视频的大小之前,我上传到服务器,目前我正在发送的文件太大,我似乎找不到任何解决方案,如何压缩这些文件,我知道像whatsapp和facebook的应用程序压缩视频在几秒钟内,并将其削减为90%的大小,任何人得到的建议,方向,我将不胜感激。

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2015-05-23 05:33:23

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

请注意,视频压缩通常是计算密集型的,所以除了小视频外,它不太可能只需要几秒钟。

有许多方法可以在应用程序中包含ffmpeg。看看这些“包装器”项目,你可以使用这些例子或者帮助你构建你自己的ffmpeg库:

作为示例,以下代码将在您选择或构建ffmpeg包装器后压缩mp4视频文件(默认压缩)(此代码使用自定义的FMPEG库,但它应该向您提供概述,并且您应该能够替换上面的示例之一):

代码语言:javascript
复制
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);
}
票数 3
EN

Stack Overflow用户

发布于 2015-05-22 01:40:03

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

代码语言:javascript
复制
 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;
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30380634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档