Skip to content

Commit

Permalink
优化camera2
Browse files Browse the repository at this point in the history
optimize camera2 API
  • Loading branch information
w2016561536 committed Sep 25, 2021
1 parent b75a128 commit 6fc4920
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 48 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "com.example.vcam"
minSdk 21
targetSdk 28
versionCode 14
versionName "3.1"
versionCode 15
versionName "3.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 14,
"versionName": "3.1",
"versionCode": 15,
"versionName": "3.2",
"outputFile": "app-release.apk"
}
],
Expand Down
100 changes: 56 additions & 44 deletions app/src/main/java/com/example/vcam/HookMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.hardware.camera2.CaptureFailure;
import android.hardware.camera2.CaptureRequest;
import android.media.Image;
import android.media.ImageReader;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaExtractor;
Expand Down Expand Up @@ -66,11 +67,7 @@ public class HookMain implements IXposedHookLoadPackage {
public static SurfaceHolder ori_holder;
public static MediaPlayer mplayer1;
public static Camera mcamera1;

public static boolean is_c2_text_2_playing;
public static boolean is_c2_text_playing;
public static boolean is_c2_surf1_playing;
public static boolean is_c2_surf2_playing;
public int Imagereader_format = 0;

public static int onemhight;
public static int onemwidth;
Expand Down Expand Up @@ -400,6 +397,7 @@ protected void beforeHookedMethod(MethodHookParam param) {
XposedBridge.log("应用创建了渲染器:宽:" + (int) param.args[0] + " 高:" + (int) param.args[1] + "格式" + (int) param.args[2]);
c2_ori_width = (int) param.args[0];
c2_ori_height = (int) param.args[1];
Imagereader_format = (int) param.args[2];
if (toast_content != null) {
Toast.makeText(toast_content, "应用创建了渲染器:\n宽:" + (int) param.args[0] + "\n高:" + (int) param.args[1] + "\n一般只需要匹配宽高比", Toast.LENGTH_LONG).show();
}
Expand All @@ -415,19 +413,16 @@ protected void beforeHookedMethod(MethodHookParam param) {

}
});


}

public void process_camera2_play() {
if (c2_preview_Surfcae != null && (!is_c2_text_playing)) {
if (c2_preview_Surfcae != null ) {
if (HookMain.c2_player == null) {
HookMain.c2_player = new MediaPlayer();
} else {
HookMain.c2_player.release();
HookMain.c2_player = new MediaPlayer();
}
is_c2_text_playing = true;
HookMain.c2_player.setSurface(HookMain.c2_preview_Surfcae);
File sfile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera1/no-silent.jpg");
if (!sfile.exists()) {
Expand All @@ -449,14 +444,13 @@ public void onPrepared(MediaPlayer mp) {
}
}

if (c2_preview_Surfcae_1 != null && (!is_c2_text_2_playing)) {
if (c2_preview_Surfcae_1 != null ) {
if (HookMain.c2_player_1 == null) {
HookMain.c2_player_1 = new MediaPlayer();
} else {
HookMain.c2_player_1.release();
HookMain.c2_player_1 = new MediaPlayer();
}
is_c2_text_2_playing = true;
HookMain.c2_player_1.setSurface(HookMain.c2_preview_Surfcae_1);
File sfile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera1/no-silent.jpg");
if (!sfile.exists()) {
Expand All @@ -479,33 +473,39 @@ public void onPrepared(MediaPlayer mp) {
}


if (c2_reader_Surfcae != null && (!is_c2_surf1_playing)) {
is_c2_surf1_playing = true;
if (c2_reader_Surfcae != null) {
if (c2_hw_decode_obj != null) {
c2_hw_decode_obj.stopDecode();
c2_hw_decode_obj = null;
}

c2_hw_decode_obj = new VideoToFrames();
try {
c2_hw_decode_obj.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.NV21);
if (Imagereader_format == 256){
c2_hw_decode_obj.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.JPEG);
}else {
c2_hw_decode_obj.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.NV21);
}
c2_hw_decode_obj.set_surfcae(HookMain.c2_reader_Surfcae);
c2_hw_decode_obj.decode(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera1/virtual.mp4");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}

if (c2_reader_Surfcae_1 != null && (!is_c2_surf2_playing)) {
is_c2_surf2_playing = true;
if (c2_reader_Surfcae_1 != null ) {
if (c2_hw_decode_obj_1 != null) {
c2_hw_decode_obj_1.stopDecode();
c2_hw_decode_obj_1 = null;
}

c2_hw_decode_obj_1 = new VideoToFrames();
try {
c2_hw_decode_obj_1.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.NV21);
if (Imagereader_format == 256){
c2_hw_decode_obj_1.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.JPEG);
}else {
c2_hw_decode_obj_1.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.NV21);
}
c2_hw_decode_obj_1.set_surfcae(HookMain.c2_reader_Surfcae_1);
c2_hw_decode_obj_1.decode(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera1/virtual.mp4");
} catch (Throwable throwable) {
Expand Down Expand Up @@ -544,11 +544,32 @@ public void process_camera2_init(Class hooked_class) {
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
need_recreate = true;
create_virtual_surface();
if (c2_player != null){
c2_player.release();
c2_player = null;
}
if (c2_hw_decode_obj_1 != null){
c2_hw_decode_obj_1.stopDecode();
c2_hw_decode_obj_1 = null;
}
if (c2_hw_decode_obj != null){
c2_hw_decode_obj.stopDecode();
c2_hw_decode_obj = null;
}
if (c2_player_1 != null){
c2_player_1.release();
c2_player_1 = null;
}
c2_preview_Surfcae_1 = null;
c2_reader_Surfcae_1 = null;
c2_reader_Surfcae = null;
c2_preview_Surfcae = null;
XposedBridge.log("打开相机C2");
XposedHelpers.findAndHookMethod(param.args[0].getClass(), "createCaptureSession", List.class, CameraCaptureSession.StateCallback.class, Handler.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam paramd) throws Throwable {
create_virtual_surface();
XposedBridge.log("来HOOK了,原始:" + paramd.args[0].toString() + "虚拟:" + HookMain.c2_virtual_surface.toString());
XposedBridge.log("创捷捕获,原始:" + paramd.args[0].toString() + "虚拟:" + HookMain.c2_virtual_surface.toString());
paramd.args[0] = Arrays.asList(HookMain.c2_virtual_surface);
XposedHelpers.findAndHookMethod(paramd.args[1].getClass(), "onConfigureFailed", CameraCaptureSession.class, new XC_MethodHook() {
@Override
Expand Down Expand Up @@ -593,9 +614,14 @@ protected void beforeHookedMethod(MethodHookParam paramd) throws Throwable {
c2_player.release();
c2_player = null;
}
is_c2_surf2_playing = false;
is_c2_text_playing = false;
is_c2_surf1_playing = false;
if (c2_player_1 != null){
c2_player_1.release();
c2_player_1 = null;
}
c2_preview_Surfcae_1 = null;
c2_reader_Surfcae_1 = null;
c2_reader_Surfcae = null;
c2_preview_Surfcae = null;
need_recreate = true;
}
});
Expand All @@ -610,26 +636,6 @@ protected void beforeHookedMethod(MethodHookParam param) {
if (param.args[0] == null) {
return;
}
if ((!param.thisObject.equals(HookMain.c2_builder))) {
HookMain.c2_builder = (CaptureRequest.Builder) param.thisObject;
c2_reader_Surfcae = null;
c2_preview_Surfcae = null;
c2_reader_Surfcae_1 = null;
is_c2_text_playing = false;
is_c2_surf1_playing = false;
is_c2_surf2_playing = false;
is_c2_text_2_playing = false;
String surfaceInfo = param.args[0].toString();
if (surfaceInfo.contains("Surface(name=null)")) {
c2_reader_Surfcae = (Surface) param.args[0];
} else {
c2_preview_Surfcae = (Surface) param.args[0];
}
create_virtual_surface();
XposedBridge.log("添加目标:" + param.args[0].toString());
param.args[0] = c2_virtual_surface;
process_camera2_play();
} else {
String surfaceInfo = param.args[0].toString();
if (surfaceInfo.contains("Surface(name=null)")) {
if (c2_reader_Surfcae == null) {
Expand All @@ -648,10 +654,16 @@ protected void beforeHookedMethod(MethodHookParam param) {
}
}
}
XposedBridge.log("多个添加目标:" + param.args[0].toString());
XposedBridge.log("添加目标:" + param.args[0].toString());
param.args[0] = c2_virtual_surface;
process_camera2_play();
}

}
});
XposedHelpers.findAndHookMethod(c2_builder.getClass(), "build", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
XposedBridge.log("开始build请求");
process_camera2_play();
}
});
}
Expand Down

0 comments on commit 6fc4920

Please sign in to comment.