skip to Main Content

I have one .jpg image, which I picked from ImagePicker Library, and I want to convert that file into .bmp file in flutter.

I have tried with native library but I didn’t get proper solution.

I want to perform this for Android and Ios

2

Answers


  1. you can write a function to convert the jpg to bmp

    void convertJpgToBmp(String jpgPath, String bmpPath) {
      final File jpgFile = File(jpgPath);
      final img.Image jpgImage = img.decodeImage(jpgFile.readAsBytesSync());
    
      final img.Image bmpImage = img.Image.from(jpgImage);
    
      final File bmpFile = File(bmpPath);
      bmpFile.writeAsBytesSync(img.encodeBmp(bmpImage));
    }
    
    void main() {
      final String jpgFilePath = 'test.jpg';
      final String bmpFilePath = 'converted.bmp';
    
      convertJpgToBmp(jpgFilePath, bmpFilePath);
    
      print('Convert completed.');
    }
    
    Login or Signup to reply.
  2. I converted ‘tiff’ to ‘png’ with ‘opencv’.
    Try changing the path of tiff to bmp and the path of png to jpg.

    Write code after linking opencv to android and ios respectively.

    android

                    try {
                        Mat tiffImg = Imgcodecs.imread(strTiff/*, Imgcodecs.IMREAD_COLOR*/);
    
                        //change background alpha
                        Mat gray = new Mat();
    
                        Imgproc.cvtColor(tiffImg, gray, Imgproc.COLOR_BGR2GRAY);
                        Mat mask = new Mat();
                        Imgproc.threshold(gray, mask, 100, 255, Imgproc.THRESH_BINARY_INV);
    
                        List<Mat> bgra = new ArrayList<Mat>(4);
                        Core.split(tiffImg, bgra);
                        bgra.add(mask);
    
                        Mat dst = new Mat();
                        Core.merge(bgra, dst);
    
                        //Writr png
                        boolean res = Imgcodecs.imwrite(pngPath, dst);
                    } catch (Exception e) {
                        e.printStackTrace();
    
                    }
             
    

    IOS

    NSString* tiffName = [strName stringByAppendingString:@".tiff"];
              
              NSString* readFilePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:tiffName];
              
              UIImage* image = [[UIImage alloc] initWithContentsOfFile:strTiff];
    
              cv::String tiffPath = [readFilePath cStringUsingEncoding:NSUTF8StringEncoding];
    
              //Read Tiff
              cv::Mat tiffImage;
              UIImageToMat(image, tiffImage, true);
              
              //Change Background Alpha
              cv::Mat input_bgra;
              cv::cvtColor(tiffImage, input_bgra, cv::COLOR_BGR2BGRA);
              for (int y = 0; y < input_bgra.rows; ++y)
                  for (int x = 0; x < input_bgra.cols; ++x)
                  {
                      cv::Vec4b & pixel = input_bgra.at<cv::Vec4b>(y, x);
                      // if pixel is white
                      if (pixel[0] == 255 && pixel[1] == 255 && pixel[2] == 255)
                      {
                          // set alpha to zero:
                          pixel[3] = 0;
                      }
                  }
              
            
              NSString* pngName = [strName stringByAppendingString:@".png"];
              NSString* saveFilePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:pngName];
              //Write PNG
              cv::String writepath_str = [saveFilePath cStringUsingEncoding:NSUTF8StringEncoding];
              bool res = cv::imwrite(writepath_str, input_bgra);
    

    //Change Background Alpha is the code that makes the background transparent. It doesn’t matter if you don’t write any code if you don’t need it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search