完成android端参数处理,功能基本完成.

This commit is contained in:
zher52 2021-10-15 14:23:51 +08:00
parent bf248c7fc4
commit d3525150e5
3 changed files with 52 additions and 43 deletions

View File

@ -24,6 +24,8 @@ import android.os.Bundle;
import org.apache.cordova.PermissionHelper;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* This class echoes a string called from JavaScript.
@ -33,7 +35,7 @@ public class CaptureCordovaPlugin extends CordovaPlugin {
private final int PERMISSION_REQUEST_CODE = 0x001;
private static String LOG_TAG = "CAPTURE_PLUGIN";
private CallbackContext callbackContext;
private JSONObject param;
private Map<String,Integer> param = new HashMap<>();
private String [] permissions = {
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
@ -44,8 +46,7 @@ public class CaptureCordovaPlugin extends CordovaPlugin {
@Override
protected void pluginInitialize() {
// 设置拍摄视频缓存路径
File dcim = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File dcim = cordova.getActivity().getCacheDir();
if (DeviceUtils.isZte()) {
if (dcim.exists()) {
JianXiCamera.setVideoCachePath(dcim + "/capture/");
@ -64,33 +65,41 @@ public class CaptureCordovaPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
this.param = args.optJSONObject(0);
if (action.equals("capture")) {
if(!hasPermisssion()) {
requestPermissions(PERMISSION_REQUEST_CODE);
} else {
this.capture();
int width = args.optInt(0, 360);
int height = args.optInt(1, 640 );
int frameRate = args.optInt(2, 30 );
int bitRate = args.optInt(3, width * height * 6 );
int limit = args.optInt(4, 3);
int duration = args.optInt(5, 10);
this.param.put("width",width);
this.param.put("height",height);
this.param.put("frameRate",frameRate);
this.param.put("bitRate",bitRate);
this.param.put("limit",limit);
this.param.put("duration",duration);
this.capture();
}
return true;
}
return false;
}
private void capture() {
JSONObject obj = this.param;
if (obj == null) {
obj = new JSONObject();
}
boolean fullScreen = obj.optBoolean("needFull", true);
private void capture() throws JSONException {
// boolean fullScreen = obj.optBoolean("needFull", true);
MediaRecorderConfig config = new MediaRecorderConfig.Buidler()
.fullScreen(fullScreen)
.smallVideoWidth(fullScreen?0:obj.optInt("width", 640))
.smallVideoHeight(obj.optInt("height", 480))
.recordTimeMax(obj.optInt("maxTime", 15000))
.recordTimeMin(obj.optInt("minTime", 3000))
.maxFrameRate(obj.optInt("maxFramerate", 24))
.videoBitrate(obj.optInt("bitrate", 580000))
.captureThumbnailsTime(obj.optInt("thumbnailsTime", 1))
.fullScreen(true)
.smallVideoWidth(this.param.get("width"))
// .smallVideoWidth(0)
.smallVideoHeight(this.param.get("height"))
.recordTimeMax(this.param.get("duration") * 1000)
.recordTimeMin(this.param.get("limit") * 1000)
.maxFrameRate(this.param.get("frameRate"))
.videoBitrate(this.param.get("bitRate"))
.captureThumbnailsTime(1)
.build();
Intent intentCapture = new Intent(this.cordova.getActivity().getBaseContext(), MediaRecorderActivity.class);
// intentCapture.putExtra(OVER_ACTIVITY_NAME, overGOActivityName);
@ -114,25 +123,25 @@ public class CaptureCordovaPlugin extends CordovaPlugin {
JSONObject obj = new JSONObject();
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode == Activity.RESULT_OK){
try {
obj.put("directory", bundle.getString(MediaRecorderActivity.OUTPUT_DIRECTORY));
obj.put("video", bundle.getString(MediaRecorderActivity.VIDEO_URI));
obj.put("thumbnail", bundle.getString(MediaRecorderActivity.VIDEO_SCREENSHOT));
obj.put("cancelled", false);
} catch (JSONException e) {
Log.d(LOG_TAG, "This should never happen");
}
callbackContext.success(obj);
// try {
// obj.put("directory", bundle.getString(MediaRecorderActivity.OUTPUT_DIRECTORY));
// obj.put("video", bundle.getString(MediaRecorderActivity.VIDEO_URI));
// obj.put("thumbnail", bundle.getString(MediaRecorderActivity.VIDEO_SCREENSHOT));
// obj.put("cancelled", false);
// } catch (JSONException e) {
// Log.d(LOG_TAG, "This should never happen");
// }
callbackContext.success(bundle.getString(MediaRecorderActivity.VIDEO_URI));
}else if (resultCode == Activity.RESULT_CANCELED){
try {
obj.put("directory", "");
obj.put("video", "");
obj.put("thumbnail", "");
obj.put("cancelled", true);
} catch (JSONException e) {
Log.d(LOG_TAG, "This should never happen");
}
callbackContext.error(obj);
// try {
// obj.put("directory", "");
// obj.put("video", "");
// obj.put("thumbnail", "");
// obj.put("cancelled", true);
// } catch (JSONException e) {
// Log.d(LOG_TAG, "This should never happen");
// }
callbackContext.error("");
}
else {
this.callbackContext.error("Unexpected error");

View File

@ -214,7 +214,6 @@ public class MediaRecorderActivity extends Activity implements
((RelativeLayout.LayoutParams) mBottomLayout.getLayoutParams()).topMargin = (int) (w / (MediaRecorderBase.SMALL_VIDEO_HEIGHT / (MediaRecorderBase.SMALL_VIDEO_WIDTH * 1.0f)));
int width = w;
int height = (int) (w * ((MediaRecorderBase.mSupportedPreviewWidth * 1.0f) / MediaRecorderBase.SMALL_VIDEO_HEIGHT));
//
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mSurfaceView
.getLayoutParams();
lp.width = width;
@ -264,6 +263,7 @@ public class MediaRecorderActivity extends Activity implements
case MotionEvent.ACTION_DOWN:
// 检测是否手动对焦
// 判断是否已经超时
Log.d("duration:"+mMediaObject.getDuration()+", max:"+RECORD_TIME_MAX);
if (mMediaObject.getDuration() >= RECORD_TIME_MAX) {
return true;
}

View File

@ -548,11 +548,12 @@ public abstract class MediaRecorderBase implements Callback, PreviewCallback, IM
// mParameters.setPreviewFpsRange(15 * 1000, 20 * 1000);
// TODO 设置浏览尺寸
boolean findWidth = false;
float ratio = screenHeight / screenWidth;
Log.d("screenHeight: " + screenHeight + ", screenWidth: " + screenWidth + ", ratio: " + (1.0d * screenHeight / screenWidth));
double ratio = 1.0d * screenHeight / screenWidth;
for (int i = mSupportedPreviewSizes.size() - 1; i >= 0; i--) {
Size size = mSupportedPreviewSizes.get(i);
Log.d("width: " + size.width + ", height: " + size.height + ", ratio: " + ((float)size.width / size.height) + ", target: " + ratio);
if (size.height >= SMALL_VIDEO_HEIGHT && ((float)size.width / size.height) == ratio) {
Log.d("width: " + size.width + ", height: " + size.height + ", ratio: " + (1.0d * size.width / size.height) + ", target: " + ratio);
if (size.height >= SMALL_VIDEO_HEIGHT && (1.0d * size.width / size.height) == ratio) {
mSupportedPreviewWidth = size.width;
checkFullWidth(mSupportedPreviewWidth,SMALL_VIDEO_WIDTH);
if (NEED_FULL_SCREEN) {
@ -667,9 +668,8 @@ public abstract class MediaRecorderBase implements Callback, PreviewCallback, IM
Size size = mParameters.getPreviewSize();
if (size != null) {
int buffSize = size.width * size.height * 3/2;
// int buffSize = SMALL_VIDEO_WIDTH * SMALL_VIDEO_HEIGHT * 3 / 2 + 9600;
try {
camera.addCallbackBuffer(new byte[buffSize]);
camera.addCallbackBuffer(new byte[buffSize]);
camera.addCallbackBuffer(new byte[buffSize]);
camera.setPreviewCallbackWithBuffer(this);
} catch (OutOfMemoryError e) {