Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NV21 support #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 67 additions & 51 deletions android/src/main/java/dev/zuzu/yuvtransform/YuvtransformPlugin.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,83 @@
package dev.zuzu.yuvtransform;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import androidx.annotation.NonNull;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;


/** YuvtransformPlugin */
/**
* YuvtransformPlugin
*/
public class YuvtransformPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "yuvtransform");
channel.setMethodCallHandler(this);
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("yuvtransform")) {
List<byte[]> bytesList = call.argument("platforms");
int[] strides = call.argument("strides");
int width = call.argument("width");
int height = call.argument("height");
int quality = call.argument("quality");

try {
byte[] data = YuvConverter.NV21toJPEG(YuvConverter.YUVtoNV21(bytesList, strides, width, height), width, height, 100);
Bitmap bitmapRaw = BitmapFactory.decodeByteArray(data, 0, data.length);

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap finalbitmap = Bitmap.createBitmap(bitmapRaw, 0, 0, bitmapRaw.getWidth(), bitmapRaw.getHeight(), matrix, true);
ByteArrayOutputStream outputStreamCompressed = new ByteArrayOutputStream();
finalbitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStreamCompressed);

result.success(outputStreamCompressed.toByteArray());
outputStreamCompressed.close();
data = null;
} catch (IOException e) {
e.printStackTrace();
}
} else {
result.notImplemented();
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "yuvtransform");
channel.setMethodCallHandler(this);
}
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
int width = call.argument("width");
int height = call.argument("height");
int quality = call.argument("quality");

if (call.method.equals("nv21_to_jpeg")) {
byte[] bytes = call.argument("bytes");


byte[] data = YuvConverter.NV21toJPEG(bytes, width, height, quality);
convertToJPEG(result, quality, data);

} else if (call.method.equals("yuvtransform")) {
List<byte[]> bytesList = call.argument("platforms");
int[] strides = call.argument("strides");

byte[] data = YuvConverter.NV21toJPEG(YuvConverter.YUVtoNV21(bytesList, strides, width, height), width, height, quality);
convertToJPEG(result, quality, data);

} else {
result.notImplemented();
}
}

private static void convertToJPEG(Result result, int quality, byte[] data) {
try {
Bitmap bitmapRaw = BitmapFactory.decodeByteArray(data, 0, data.length);

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap finalbitmap = Bitmap.createBitmap(bitmapRaw, 0, 0, bitmapRaw.getWidth(), bitmapRaw.getHeight(), matrix, true);
ByteArrayOutputStream outputStreamCompressed = new ByteArrayOutputStream();
finalbitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStreamCompressed);

result.success(outputStreamCompressed.toByteArray());
outputStreamCompressed.close();
data = null;
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
3 changes: 1 addition & 2 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
4 changes: 2 additions & 2 deletions example/lib/yuv_transform_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class _YuvTransformScreenState extends State<YuvTransformScreen>
super.initState();
// Registers the page to observer for life cycle managing.
_imageResultProcessorService = ImageResultProcessorService();
WidgetsBinding.instance?.addObserver(this);
WidgetsBinding.instance.addObserver(this);
_subscription.add(_imageResultProcessorService.queue.listen((event) {
_isProcessing = false;
}));
Expand All @@ -32,7 +32,7 @@ class _YuvTransformScreenState extends State<YuvTransformScreen>

@override
void dispose() {
WidgetsBinding.instance?.removeObserver(this);
WidgetsBinding.instance.removeObserver(this);
// Dispose all streams!
_subscription.forEach((element) {
element.cancel();
Expand Down
Loading