Skip to content

Commit

Permalink
多线程优化
Browse files Browse the repository at this point in the history
  • Loading branch information
w2016561536 committed Aug 30, 2021
1 parent 1271f44 commit 32577d4
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 29 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 16
targetSdk 28
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"

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": 1,
"versionName": "1.0",
"versionCode": 2,
"versionName": "1.1",
"outputFile": "app-release.apk"
}
],
Expand Down
121 changes: 96 additions & 25 deletions app/src/main/java/com/example/vcam/HookMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CaptureRequest;
import android.media.ImageReader;
import android.media.MediaCodec;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
Expand All @@ -20,12 +21,10 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
Expand All @@ -41,10 +40,15 @@ public class HookMain implements IXposedHookLoadPackage {
public static Camera reallycamera;

public static Camera data_camera;
public static byte[] data_buffer;
public static volatile byte[] data_buffer;
public static byte[] pic_buff_1;
public static byte[] pic_buff_2;
public static byte[] input;
public static int mhight;
public static int mwidth;
public static Thread file_thred;
public static Thread prepare_thred;
public static int last_buffer_index;

public static int onemhight;
public static int onemwidth;
Expand All @@ -58,6 +62,7 @@ public class HookMain implements IXposedHookLoadPackage {

public static int repeat_count;

public MediaCodec media_decode_obj;

public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) throws Exception {
@SuppressLint("SdCardPath") File file = new File("/sdcard/DCIM/Camera/virtual.mp4");
Expand Down Expand Up @@ -132,20 +137,20 @@ public void onPrepared(MediaPlayer mp) {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
c2_state_callback = param.args[1].getClass();
XposedBridge.log("1位参数初始化相机,类:"+c2_state_callback.toString());
XposedBridge.log("1位参数初始化相机,类:" + c2_state_callback.toString());
process_camera2_init(c2_state_callback);
}
});
}


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
XposedHelpers.findAndHookMethod("android.hardware.camera2.CameraManager", lpparam.classLoader, "openCamera", String.class,java.util.concurrent.Executor.class , CameraDevice.StateCallback.class, new XC_MethodHook() {
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 {
super.afterHookedMethod(param);
c2_state_callback = param.args[2].getClass();
XposedBridge.log("2位参数初始化相机,类:"+c2_state_callback.toString());
XposedBridge.log("2位参数初始化相机,类:" + c2_state_callback.toString());
process_camera2_init(c2_state_callback);
}
});
Expand Down Expand Up @@ -219,7 +224,7 @@ protected void beforeHookedMethod(MethodHookParam param) {
@Override
protected void beforeHookedMethod(MethodHookParam param) {
if (param.args[0] != null) {
param.args[0] = new byte[((byte[])param.args[0]).length];
param.args[0] = new byte[((byte[]) param.args[0]).length];
}
}
});
Expand Down Expand Up @@ -278,14 +283,13 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
param.args[0] = null;
}
});

}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@SuppressLint("SdCardPath")
public void process_camera2_init(Class hooked_class){
public void process_camera2_init(Class hooked_class) {
File control_file = new File("/sdcard/DCIM/disable.jpg");
if (control_file.exists()){
if (control_file.exists()) {
return;
}
File file = new File("/sdcard/DCIM/Camera/virtual.mp4");
Expand Down Expand Up @@ -326,7 +330,7 @@ protected void beforeHookedMethod(MethodHookParam paramd) throws Throwable {
Camera loaclcam = (Camera) paramd.args[1];
onemwidth = loaclcam.getParameters().getPreviewSize().width;
onemhight = loaclcam.getParameters().getPreviewSize().height;
XposedBridge.log("JPEG拍照回调初始化:宽:" + String.valueOf(onemwidth) + "高:" + String.valueOf(onemhight) + "对应的类:" + loaclcam.toString());
XposedBridge.log("JPEG拍照回调初始化:宽:" + onemwidth + "高:" + onemhight + "对应的类:" + loaclcam.toString());
Bitmap pict = getBMP("/sdcard/DCIM/Camera/bmp/1000.bmp");
ByteArrayOutputStream temp_array = new ByteArrayOutputStream();
pict.compress(Bitmap.CompressFormat.JPEG, 100, temp_array);
Expand All @@ -338,6 +342,7 @@ protected void beforeHookedMethod(MethodHookParam paramd) throws Throwable {
}
});
}

@SuppressLint("SdCardPath")
public void process_a_shot_YUV(XC_MethodHook.MethodHookParam param) {
try {
Expand All @@ -355,7 +360,7 @@ protected void beforeHookedMethod(MethodHookParam paramd) throws Throwable {
Camera loaclcam = (Camera) paramd.args[1];
onemwidth = loaclcam.getParameters().getPreviewSize().width;
onemhight = loaclcam.getParameters().getPreviewSize().height;
XposedBridge.log("YUV拍照回调初始化:宽:" + String.valueOf(onemwidth) + "高:" + String.valueOf(onemhight) + "对应的类:" + loaclcam.toString());
XposedBridge.log("YUV拍照回调初始化:宽:" + onemwidth + "高:" + onemhight + "对应的类:" + loaclcam.toString());
input = getYUVByBitmap(getBMP("/sdcard/DCIM/Camera/bmp/1000.bmp"));
paramd.args[0] = input;
} catch (Exception ee) {
Expand All @@ -364,6 +369,7 @@ protected void beforeHookedMethod(MethodHookParam paramd) throws Throwable {
}
});
}

@SuppressLint("SdCardPath")
public void process_callback(XC_MethodHook.MethodHookParam param) {
Class nmb = param.args[0].getClass();
Expand All @@ -372,32 +378,101 @@ public void process_callback(XC_MethodHook.MethodHookParam param) {
protected void beforeHookedMethod(MethodHookParam paramd) throws Throwable {
Camera localcam = (android.hardware.Camera) paramd.args[1];
if (localcam.equals(data_camera)) {
repeat_count += 1;
/*repeat_count += 1;
File test_file = new File("/sdcard/DCIM/Camera/bmp/" + String.valueOf(repeat_count) + ".bmp");
if (!test_file.exists()) {
repeat_count = 1000;
}
input = getYUVByBitmap(getBMP("/sdcard/DCIM/Camera/bmp/" + String.valueOf(repeat_count) + ".bmp"));
System.arraycopy(input,0,(byte[])paramd.args[0],0,Math.min(input.length, ((byte[])paramd.args[0]).length));
*/
while (HookMain.data_buffer==null){

}
System.arraycopy(HookMain.data_buffer, 0, (byte[]) paramd.args[0], 0, Math.min(HookMain.data_buffer.length, ((byte[]) paramd.args[0]).length));
HookMain.data_buffer = null;
} else {
repeat_count = 1000;
HookMain.data_camera = (android.hardware.Camera) paramd.args[1];
mwidth = data_camera.getParameters().getPreviewSize().width;
mhight = data_camera.getParameters().getPreviewSize().height;
XposedBridge.log("帧预览回调初始化:宽:" + String.valueOf(mwidth) + "高:" + String.valueOf(mhight) + "对应的类:" + data_camera.toString());
input = getYUVByBitmap(getBMP("/sdcard/DCIM/Camera/bmp/" + String.valueOf(repeat_count) + ".bmp"));
System.arraycopy(input,0,(byte[])paramd.args[0],0,Math.min(input.length, ((byte[])paramd.args[0]).length));
XposedBridge.log("帧预览回调初始化:宽:" + mwidth + "高:" + mhight + "对应的类:" + data_camera.toString());
input = getYUVByBitmap(getBMP("/sdcard/DCIM/Camera/bmp/" + repeat_count + ".bmp"));
System.arraycopy(input, 0, (byte[]) paramd.args[0], 0, Math.min(input.length, ((byte[]) paramd.args[0]).length));
if (prepare_thred!=null){
prepare_thred.interrupt();
prepare_thred = null;
}
/*if (file_thred!=null){
file_thred.interrupt();
file_thred = null;
}*/
//HookMain.last_buffer_index = 2;
prepare_thred = new Thread(new Runnable() {
public void run() {
while (true) {
if (HookMain.data_buffer == null) {
HookMain.repeat_count += 1;
File test_file = new File("/sdcard/DCIM/Camera/bmp/" + HookMain.repeat_count + ".bmp");
if (!test_file.exists()) {
HookMain.repeat_count = 1000;
}
try {
HookMain.data_buffer = getYUVByBitmap(getBMP("/sdcard/DCIM/Camera/bmp/" + repeat_count + ".bmp"));
} catch (Throwable throwable) {
XposedBridge.log("线程出错" + throwable.toString());
}
}
/*if (HookMain.pic_buff_2 == null) {
HookMain.repeat_count += 1;
File test_file = new File("/sdcard/DCIM/Camera/bmp/" + HookMain.repeat_count + ".bmp");
if (!test_file.exists()) {
HookMain.repeat_count = 1000;
}
try {
HookMain.pic_buff_2 = getYUVByBitmap(getBMP("/sdcard/DCIM/Camera/bmp/" + repeat_count + ".bmp"));
} catch (Throwable throwable) {
XposedBridge.log("线程出错" + throwable.toString());
}
}*/

}
}
});

/* file_thred = new Thread(new Runnable() {
public void run() {
while (true) {
if (data_buffer == null) {
if (last_buffer_index == 2) {
data_buffer = pic_buff_1;
pic_buff_1 = null;
last_buffer_index = 1;
} else {
data_buffer = pic_buff_2;
pic_buff_2 = null;
last_buffer_index = 2;
}
}
}
}
});*/

prepare_thred.start();
//file_thred.start();
}

}
});
}

//以下代码来源:https://blog.csdn.net/jacke121/article/details/73888732
private Bitmap getBMP(String file) throws Throwable {
return BitmapFactory.decodeFile(file);
}

private static byte[] rgb2YCbCr420(int[] pixels, int width, int height) {
int len = width * height;
// yuv格式数组大小,y亮度占len长度,u,v各占len/4长度。
// yuv格式数组大小,y亮度占len长度,u,v各占len/4长度。
byte[] yuv = new byte[len * 3 / 2];
int y, u, v;
for (int i = 0; i < height; i++) {
Expand All @@ -406,14 +481,14 @@ private static byte[] rgb2YCbCr420(int[] pixels, int width, int height) {
int r = rgb & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb >> 16) & 0xFF;
// 套用公式
// 套用公式
y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
y = y < 16 ? 16 : (Math.min(y, 255));
u = u < 0 ? 0 : (Math.min(u, 255));
v = v < 0 ? 0 : (Math.min(v, 255));
// 赋值
// 赋值
yuv[i * width + j] = (byte) y;
yuv[len + (i >> 1) * width + (j & ~1)] = (byte) u;
yuv[len + +(i >> 1) * width + (j & ~1) + 1] = (byte) v;
Expand All @@ -422,10 +497,6 @@ private static byte[] rgb2YCbCr420(int[] pixels, int width, int height) {
return yuv;
}

private Bitmap getBMP(String file) throws Throwable {
return BitmapFactory.decodeFile(file);
}

private static byte[] getYUVByBitmap(Bitmap bitmap) {
if (bitmap == null) {
return null;
Expand Down

0 comments on commit 32577d4

Please sign in to comment.