분류, 패키지 및 저장소 검색

GitHub 저장소deakjahn/jpeg_encode

jpeg_encode

최소한의 JPEG 인코더

jpeg_encode 프로젝트 이미지
스타
6
포크
2
최근 푸시(UTC)
2024. 11. 8.
프로젝트 상태
활성

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

JPEG encoder

pub package

Skia, the engine behind dart:ui has no support for JPEG, only PNG and raw RGBA. And the source code comments make it clear that they don't intend to add any more formats, those should be left to external packages.

While there are packages that offer full support for image manipulation, many a time we don't need that, just to be able to convert a file to JPG for use outside of our app, maybe to send it to the web. This package does that and nothing else. It's a minimalistic JPEG encoder that doesn't offer any extra functionality but to create the simplest JPEG file. Not necessarily the best or most compressed one, although it's fairly good, nonetheless.

The code is just a straight Dart port of an existing implementation of Jon Olick from https://www.jonolick.com/code.html.

How to use

Let's suppose you have a dart:ui Image from somewhere, maybe you converted from an incoming image:

import "dart:ui" as ui;

Uint8List bytes;
final codec = await ui.instantiateImageCodec(bytes);
final frame = await codec.getNextFrame();
final image = frame.image;

And you want to convert it to JPG (quality is between 0 and 100, as usual):

final data = await image.toByteData(format: skia.ImageByteFormat.rawRgba);
final jpg = JpegEncoder().compress(data!.buffer.asUint8List(), image.width, image.height, 90);

In other words, the pixel array is expected to be in row major format (all columns of first row, then of the second row, etc), four bytes for each pixel: R, G, B and A. Alpha is ignored but the byte is expected to be there. Consequently, the length of the array has to be width * height * 4.

Before you ask, saving in Flutter is as simple as:

File(path).writeAsBytesSync(jpg); // or
await File(path).writeAsBytes(jpg);

Support

If you like this package, please consider supporting it.

Buy Me A Book

Gábor GitHub avatar
GitHub 작성자

Gábor ↗

언어Dart

분류

이 저장소가 배포한 패키지

사용 중인 의존성

의존성 목록 3 개

  • flutter{"sdk":"flutter"}
  • flutter_test개발 의존성{"sdk":"flutter"}
  • flutter_lints개발 의존성^5.0.0

프로젝트에 맞는 저장소를 선택하려면?

사용 목적에 따라 검색하고 개발 활동을 비교한 뒤 원본 저장소에서 요구 사항을 확인하세요.

  1. 이름, 작성자 또는 설명으로 검색하고 기술 주제로 범위를 좁힙니다.
  2. 스타, 포크, 최근 푸시를 함께 비교하세요. 스타는 관심의 지표이며 품질을 보장하지 않습니다.
  3. 사용하기 전에 GitHub에서 README, 라이선스, Issues를 확인하세요.

출처 및 관리

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗