Skip to content

Commit

Permalink
解决Camera2重复打开相机问题
Browse files Browse the repository at this point in the history
solve Camera2 apps opening camera repeatedly
  • Loading branch information
w2016561536 committed Nov 28, 2021
1 parent fda5a28 commit 2ae6ee8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 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 29
versionCode 19
versionName "3.6"
versionCode 20
versionName "3.7"

testInstrumentationRunner "android.support.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": 19,
"versionName": "3.6",
"versionCode": 20,
"versionName": "3.7",
"outputFile": "app-release.apk"
}
],
Expand Down
60 changes: 40 additions & 20 deletions app/src/main/java/com/example/vcam/HookMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ public class HookMain implements IXposedHookLoadPackage {
public static Surface c2_reader_Surfcae_1;
public static MediaPlayer c2_player;
public static MediaPlayer c2_player_1;
public static CaptureRequest.Builder c2_builder;
public static Surface c2_virtual_surface;
public static SurfaceTexture c2_virtual_surfaceTexture;
public boolean need_recreate;

public static String last_package_name;
public static CameraDevice.StateCallback c2_state_cb;

public int c2_ori_width = 1280;
public int c2_ori_height = 720;
Expand Down Expand Up @@ -147,6 +145,13 @@ protected void beforeHookedMethod(MethodHookParam param) {
XposedHelpers.findAndHookMethod("android.hardware.camera2.CameraManager", lpparam.classLoader, "openCamera", String.class, CameraDevice.StateCallback.class, Handler.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
if (param.args[1] == null){
return;
}
if (param.args[1].equals(c2_state_cb)){
return;
}
c2_state_cb = (CameraDevice.StateCallback) param.args[1];
c2_state_callback = param.args[1].getClass();
File control_file = new File(video_path + "disable.jpg");
if (control_file.exists()) {
Expand All @@ -170,6 +175,13 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
XposedHelpers.findAndHookMethod("android.hardware.camera2.CameraManager", lpparam.classLoader, "openCamera", String.class, Executor.class, CameraDevice.StateCallback.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (param.args[2] == null){
return;
}
if (param.args[2].equals(c2_state_cb)){
return;
}
c2_state_cb = (CameraDevice.StateCallback) param.args[2];
File control_file = new File(video_path + "disable.jpg");
if (control_file.exists()) {
return;
Expand Down Expand Up @@ -290,8 +302,8 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {

}
}else {
File DCIM_dic = new File(Environment.getExternalStorageDirectory() + "/DCIM/");
if ((!DCIM_dic.canRead()) && auth_statue < 1){
File DCIM_dic = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/");
if (!DCIM_dic.canRead()){
auth_statue = -1;
}
}
Expand Down Expand Up @@ -322,6 +334,16 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
}
}else {
video_path = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera1/";
File uni_DCIM_path = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/");
if (uni_DCIM_path.canWrite()){
File uni_Camera1_path = new File(video_path);
if ((!uni_Camera1_path.isDirectory()) && uni_Camera1_path.exists()){
//uni_Camera1_path.delete();
}
if (!uni_Camera1_path.exists()){
uni_Camera1_path.mkdir();
}
}
}
}
}
Expand Down Expand Up @@ -470,6 +492,9 @@ protected void beforeHookedMethod(MethodHookParam param) {
if (param.args[0] == null) {
return;
}
if (param.thisObject == null){
return;
}
File control_file = new File(video_path + "disable.jpg");
if (control_file.exists()) {
return;
Expand Down Expand Up @@ -581,7 +606,7 @@ public void onPrepared(MediaPlayer mp) {
c2_player.setDataSource(video_path + "virtual.mp4");
c2_player.prepare();
} catch (IOException e) {
XposedBridge.log("【VCAM】"+e.toString());
XposedBridge.log("【VCAM】[c2player][" + c2_preview_Surfcae.toString() +"]"+e.toString());
}
}

Expand Down Expand Up @@ -609,7 +634,7 @@ public void onPrepared(MediaPlayer mp) {
c2_player_1.setDataSource(video_path + "virtual.mp4");
c2_player_1.prepare();
} catch (IOException e) {
XposedBridge.log("【VCAM】"+e.toString());
XposedBridge.log("【VCAM】[c2player1]"+"[ " + c2_preview_Surfcae_1.toString() + "]"+e.toString());
}
}

Expand All @@ -623,9 +648,9 @@ public void onPrepared(MediaPlayer mp) {
c2_hw_decode_obj = new VideoToFrames();
try {
if (Imagereader_format == 256) {
c2_hw_decode_obj.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.JPEG);
c2_hw_decode_obj.setSaveFrames("null", OutputImageFormat.JPEG);
} else {
c2_hw_decode_obj.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.NV21);
c2_hw_decode_obj.setSaveFrames("null", OutputImageFormat.NV21);
}
c2_hw_decode_obj.set_surfcae(c2_reader_Surfcae);
c2_hw_decode_obj.decode(video_path + "virtual.mp4");
Expand All @@ -643,9 +668,9 @@ public void onPrepared(MediaPlayer mp) {
c2_hw_decode_obj_1 = new VideoToFrames();
try {
if (Imagereader_format == 256) {
c2_hw_decode_obj_1.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.JPEG);
c2_hw_decode_obj_1.setSaveFrames("null", OutputImageFormat.JPEG);
} else {
c2_hw_decode_obj_1.setSaveFrames(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera2/", OutputImageFormat.NV21);
c2_hw_decode_obj_1.setSaveFrames("null", OutputImageFormat.NV21);
}
c2_hw_decode_obj_1.set_surfcae(c2_reader_Surfcae_1);
c2_hw_decode_obj_1.decode(video_path + "virtual.mp4");
Expand Down Expand Up @@ -774,13 +799,6 @@ protected void beforeHookedMethod(MethodHookParam paramd) throws Throwable {
}
});*/

XposedHelpers.findAndHookMethod(param.args[0].getClass(), "createCaptureRequest", int.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam paramd) throws Throwable {
c2_builder = (CaptureRequest.Builder) paramd.getResult();

}
});
}

});
Expand Down Expand Up @@ -1044,7 +1062,7 @@ public void run() {

@SuppressLint("WrongConstant")
public void videoDecode(String videoFilePath) throws IOException {
XposedBridge.log("【VCAM】开始解码");
XposedBridge.log("【VCAM】【decoder】开始解码");
MediaExtractor extractor = null;
MediaCodec decoder = null;
try {
Expand All @@ -1053,6 +1071,7 @@ public void videoDecode(String videoFilePath) throws IOException {
extractor.setDataSource(videoFile.toString());
int trackIndex = selectTrack(extractor);
if (trackIndex < 0) {
XposedBridge.log("【VCAM】【decoder】No video track found in " + videoFilePath);
throw new RuntimeException("No video track found in " + videoFilePath);
}
extractor.selectTrack(trackIndex);
Expand All @@ -1062,9 +1081,10 @@ public void videoDecode(String videoFilePath) throws IOException {
showSupportedColorFormat(decoder.getCodecInfo().getCapabilitiesForType(mime));
if (isColorFormatSupported(decodeColorFormat, decoder.getCodecInfo().getCapabilitiesForType(mime))) {
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, decodeColorFormat);
Log.i(TAG, "set decode color format to type " + decodeColorFormat);
XposedBridge.log( "【VCAM】【decoder】set decode color format to type " + String.valueOf(decodeColorFormat));
} else {
Log.i(TAG, "unable to set decode color format, color format type " + decodeColorFormat + " not supported");
XposedBridge.log("【VCAM】【decoder】unable to set decode color format, color format type " + String.valueOf(decodeColorFormat) + " not supported");
}
decodeFramesToImage(decoder, extractor, mediaFormat);
decoder.stop();
Expand Down

0 comments on commit 2ae6ee8

Please sign in to comment.