How to Increase JPG file size?

asked 2016-12-14 12:30:27 -0600

redleon80 gravatar image

I'm getting byte image data from native camera, takePicture and pictureCallBack. And using opencv libraries for rotating, croping and resising. I'm using Bitmap.ComptressFormat and FileExportStream to save image. Everything work fine. But It have to create jpg file size grather than 20KB but sometimes it's getting smaller than 20KB. I also add exif datas but it's doest't give target file size values.

Is there any way to increase file size? I tried imwrite but it give smallest results. I couldn't success to find save jpg without compression.

PictureCallback myPictureCallback_JPG = new PictureCallback(){
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getJpgFile();

       data = cropImage2(data);

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();

            addEXIFData(pictureFile);

        } catch (FileNotFoundException e) {
            Log.d("Method.PictureCallBack", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("Method.PictureCallBack", "Error accessing file: " + e.getMessage());
        }

public byte[] cropImage (byte[] data) {
        Mat lastMat = new Mat();

        Mat mat1_img = Imgcodecs.imdecode(new MatOfByte(data), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);

        Mat mat2_rotated = new Mat();

        Core.flip(mat1_img.t(), mat2_rotated, 1);

        Mat mat3_cropped = new Mat(mat2_rotated, cropRect);

        Imgproc.resize(mat3_cropped, lastMat, cropSize);

        Imgproc.cvtColor(lastMat, lastMat, Imgproc.COLOR_BGR2RGB,1);
        Bitmap bitmapImg = Bitmap.createBitmap(lastMat.cols(), lastMat.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(lastMat, bitmapImg);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        bitmapImg.compress(Bitmap.CompressFormat.JPEG, 100, bos);

        return bos.toByteArray();
    }
edit retag flag offensive close merge delete

Comments

I'm not sure that your question is relative to opencv. But If you want to increase jpeg file size add noise to image

LBerger gravatar imageLBerger ( 2016-12-14 15:13:17 -0600 )edit

The image format Jpeg is lossy, there is no way to save an image in Jpeg without compression. You can use PNG image format for lossless compression.

Eduardo gravatar imageEduardo ( 2016-12-14 15:41:13 -0600 )edit

Yes you right @LBerger. The question maybe is not all about opencv. But opencv libraries can make manipulation on image. I dont know maybe I can

  • add text jpeg files
  • add color profile data
  • add noise

to increase file. I added opencv library and all works fine. Just I need an example.

redleon80 gravatar imageredleon80 ( 2016-12-14 23:49:48 -0600 )edit

@Eduardo I now JPEG compressed image. But I need to have a little bit bigger jpeg file size. I edited the jpeg file in MSPaint, Photoshop etc. MSPaint save it smallest, but Photoshop can save bigger size if I chouse "add color profile" option.

I added exifdatas but this's not enough.

redleon80 gravatar imageredleon80 ( 2016-12-15 00:00:19 -0600 )edit

why on earth do you need a larger filesize ?

berak gravatar imageberak ( 2016-12-15 01:20:51 -0600 )edit

My app generate jpeg for special web application. The web application ask static resolution and at least 20KB file size. If jpeg file smaller than 20KB you cant upload the jpeg file. For the reason I need to generate fixed resolution and bigger than 20KB on earth.

redleon80 gravatar imageredleon80 ( 2016-12-15 01:45:53 -0600 )edit

you can add compression params to imwrite (try to set it to 0)

berak gravatar imageberak ( 2016-12-15 01:49:49 -0600 )edit

I tried to the all jpeg params: "JPEG_CHROMA_QUALITY, IMWRITE_JPEG_LUMA_QUALITY, IMWRITE_JPEG_OPTIMIZE, IMWRITE_JPEG_PROGRESSIVE, IMWRITE_JPEG_QUALITY, IMWRITE_JPEG_RST_INTERVAL"

redleon80 gravatar imageredleon80 ( 2016-12-15 14:56:25 -0600 )edit