translate PushActivity into English

translate PushActivity into English
pull/166/head
xufulong 5 years ago
parent df2dce9dd7
commit d10b6b6fd6
  1. 1
      Live/src/main/AndroidManifest.xml
  2. 19
      Live/src/main/java/com/frank/live/LiveApplication.java
  3. 485
      Live/src/main/java/com/frank/live/PushActivity.java
  4. 10
      Live/src/main/res/values/strings.xml

@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/>
<application <application
android:name=".LiveApplication"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"

@ -0,0 +1,19 @@
package com.frank.live;
import android.app.Application;
public class LiveApplication extends Application {
private static LiveApplication context;
@Override
public void onCreate() {
super.onCreate();
context = this;
}
public static LiveApplication getInstance() {
return context;
}
}

@ -9,7 +9,9 @@ import android.graphics.Bitmap;
import android.hardware.Camera; import android.hardware.Camera;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import android.util.Log; import android.util.Log;
import android.view.Surface; import android.view.Surface;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
@ -33,277 +35,274 @@ import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
public class PushActivity extends Activity implements Callback { public class PushActivity extends Activity implements Callback {
private static String TAG = PushActivity.class.getSimpleName(); private static String TAG = PushActivity.class.getSimpleName();
private SmartCameraView mSmartCameraView; private SmartCameraView mSmartCameraView;
// MagicFilterType magicType = MagicFilterType.SUNRISE; // MagicFilterType magicType = MagicFilterType.SUNRISE;
private Button btnMute; private Button btnMute;
private boolean isStart = false;
private boolean isStart = false; private boolean is_mute = false;
private boolean is_mute = false;
private int mDegree; private int mDegree;
private Spinner beautyTypeSelector; private Spinner beautyTypeSelector;
private ImageView img_photo; private ImageView img_photo;
//拍照
private boolean takePhoto;
private final static int videoWidth = 640; private boolean takePhoto;
private final static int videoHeight = 360;
private final static String[] permissions = new String[]{Manifest.permission.CAMERA};
private final static int CODE_CAMERA = 1001;
private final static String[] beautySelector = new String[]{"美颜", "冷酷", "日出","素描","白猫", "浪漫", "原图"}; private final static int videoWidth = 640;
private final static int videoHeight = 360;
private final static String[] permissions = new String[]{Manifest.permission.CAMERA};
private final static int CODE_CAMERA = 1001;
@Override private final static String[] beautySelector = new String[]{
LiveApplication.getInstance().getString(R.string.effect_beauty),
LiveApplication.getInstance().getString(R.string.effect_cool),
LiveApplication.getInstance().getString(R.string.effect_sunrise),
LiveApplication.getInstance().getString(R.string.effect_sketch),
LiveApplication.getInstance().getString(R.string.effect_white),
LiveApplication.getInstance().getString(R.string.effect_romantic),
LiveApplication.getInstance().getString(R.string.effect_raw)
};
@Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
requestPermissions(); requestPermissions();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_push); setContentView(R.layout.activity_push);
initView(); initView();
initListener(); initListener();
}
@TargetApi(23)
private void requestPermissions() {
requestPermissions(permissions, CODE_CAMERA);
}
private void initView() {
//SurfaceView
mSmartCameraView = findViewById(R.id.gl_surfaceview);
//beauty type
beautyTypeSelector = findViewById(R.id.beauty_type_selctor);
//mute
btnMute = findViewById(R.id.button_mute);
//take photo
img_photo = findViewById(R.id.img_photo);
} }
@TargetApi(23) private void initListener() {
private void requestPermissions(){
requestPermissions(permissions, CODE_CAMERA); ArrayAdapter<String> adapterBeautyType = new ArrayAdapter<>(this,
} android.R.layout.simple_spinner_item, beautySelector);
adapterBeautyType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
private void initView(){ beautyTypeSelector.setAdapter(adapterBeautyType);
//SurfaceView beautyTypeSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
mSmartCameraView = findViewById(R.id.gl_surfaceview); @Override
//美颜类型 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
beautyTypeSelector = findViewById(R.id.beauty_type_selctor);
//静音 switch (position) {
btnMute = findViewById(R.id.button_mute); case 0:
//拍照 switchCameraFilter(MagicFilterType.BEAUTY);
img_photo = findViewById(R.id.img_photo); break;
} case 1:
switchCameraFilter(MagicFilterType.COOL);
private void initListener(){ break;
case 2:
ArrayAdapter<String> adapterBeautyType = new ArrayAdapter<>(this, switchCameraFilter(MagicFilterType.SUNRISE);
android.R.layout.simple_spinner_item, beautySelector); break;
adapterBeautyType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); case 3:
beautyTypeSelector.setAdapter(adapterBeautyType); switchCameraFilter(MagicFilterType.SKETCH);
beautyTypeSelector.setOnItemSelectedListener(new OnItemSelectedListener() { break;
@Override case 4:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { switchCameraFilter(MagicFilterType.WHITECAT);
break;
switch (position){ case 5:
case 0: switchCameraFilter(MagicFilterType.ROMANCE);
switchCameraFilter(MagicFilterType.BEAUTY); break;
break; default:
case 1: switchCameraFilter(MagicFilterType.NONE);
switchCameraFilter(MagicFilterType.COOL); break;
break; }
case 2: }
switchCameraFilter(MagicFilterType.SUNRISE);
break; @Override
case 3: public void onNothingSelected(AdapterView<?> parent) {
switchCameraFilter(MagicFilterType.SKETCH);
break; }
case 4: });
switchCameraFilter(MagicFilterType.WHITECAT);
break; btnMute.setOnClickListener(new OnClickListener() {
case 5: @Override
switchCameraFilter(MagicFilterType.ROMANCE); public void onClick(View view) {
break; is_mute = !is_mute;
default:
switchCameraFilter(MagicFilterType.NONE); if (is_mute) {
break; btnMute.setText(getString(R.string.voice));
} } else {
} btnMute.setText(getString(R.string.mute));
@Override }
public void onNothingSelected(AdapterView<?> parent) { }
});
}
}); //preview data callback(RGBA)
mSmartCameraView.setPreviewCallback(new SmartCameraView.PreviewCallback() {
btnMute.setOnClickListener(new OnClickListener() { @Override
@Override public void onGetRgbaFrame(byte[] data, int width, int height) {
public void onClick(View view) {
is_mute = !is_mute; if (takePhoto) {
takePhoto = false;
if ( is_mute ) Log.i(TAG, "takePhoto...");
btnMute.setText("取消静音"); doTakePhoto(data, width, height);
else }
btnMute.setText("静音");
} }
}); });
//预览数据回调(RGBA格式) img_photo.setOnClickListener(new OnClickListener() {
mSmartCameraView.setPreviewCallback(new SmartCameraView.PreviewCallback() { @Override
@Override public void onClick(View view) {
public void onGetRgbaFrame(byte[] data, int width, int height) { takePhoto = true;
}
if(takePhoto){ });
takePhoto = false;
Log.i(TAG, "takePhoto..."); }
doTakePhoto(data, width, height);
} private void switchCameraFilter(MagicFilterType type) {
mSmartCameraView.setFilter(type);
} }
});
/**
img_photo.setOnClickListener(new OnClickListener() { * take photo
@Override *
public void onClick(View view) { * @param data preview data
takePhoto = true; * @param width the width of photo
} * @param height the height of photo
}); */
private void doTakePhoto(byte[] data, int width, int height) {
} Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = ByteBuffer.wrap(data);
private void switchCameraFilter(MagicFilterType type) { bitmap.copyPixelsFromBuffer(buffer);
mSmartCameraView.setFilter(type);
} Log.i(TAG, "doTakePhoto...");
FileOutputStream fileOutputStream = null;
/** String PATH = Environment.getExternalStorageDirectory().getPath();
* 拍照 String filePath = PATH + File.separator + "hello" + ".jpg";
* @param data 预览数据
* @param width 图片宽度
* @param height 图片高度
*/
private void doTakePhoto(byte[] data, int width, int height){
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = ByteBuffer.wrap(data);
bitmap.copyPixelsFromBuffer(buffer);
Log.i(TAG, "doTakePhoto...");
FileOutputStream fileOutputStream = null;
String PATH = Environment.getExternalStorageDirectory().getPath();
String filePath = PATH + File.separator + "hello_openGL" + ".jpg";
try {
fileOutputStream = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "doTakePhoto error=" + e.toString());
}finally {
if(fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i(TAG, "surfaceCreated..");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.i(TAG, "surfaceChanged..");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
Log.i(TAG, "Surface Destroyed");
}
public void onConfigurationChanged(Configuration newConfig) {
try { try {
super.onConfigurationChanged(newConfig); fileOutputStream = new FileOutputStream(filePath);
Log.i(TAG, "onConfigurationChanged, start:" + isStart); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "doTakePhoto error=" + e.toString());
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
setCameraDisplayOrientation(this, getCameraId()); @Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i(TAG, "surfaceCreated..");
}
mSmartCameraView.setPreviewOrientation(newConfig.orientation, mDegree); @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.i(TAG, "surfaceChanged..");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i(TAG, "Surface Destroyed");
}
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
Log.i(TAG, "onConfigurationChanged, start:" + isStart);
setCameraDisplayOrientation(this, getCameraId());
mSmartCameraView.setPreviewOrientation(newConfig.orientation, mDegree);
} catch (Exception ex) { } catch (Exception ex) {
Log.e(TAG, "error="+ex.toString()); Log.e(TAG, "error=" + ex.toString());
}
}
private int getCameraId() {
return mSmartCameraView.getCameraId();
}
public void setPreviewResolution(int width, int height) {
mSmartCameraView.setPreviewResolution(width, height);
}
private void setCameraDisplayOrientation(Activity activity, int cameraId) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
} }
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
} else {
// back-facing
result = (info.orientation - degrees + 360) % 360;
}
Log.i(TAG, "curDegree: " + result);
mDegree = result;
} }
private int getCameraId() { @Override
return mSmartCameraView.getCameraId(); protected void onDestroy() {
} if (isStart) {
isStart = false;
public void setPreviewResolution(int width, int height) { if (mSmartCameraView != null) {
mSmartCameraView.setPreviewResolution(width, height); mSmartCameraView.stopCamera();
} }
Log.i(TAG, "onDestroy StopPublish");
private void setCameraDisplayOrientation (Activity activity, int cameraId) { }
Camera.CameraInfo info = new Camera.CameraInfo(); super.onDestroy();
Camera.getCameraInfo (cameraId , info); finish();
int rotation = activity.getWindowManager ().getDefaultDisplay ().getRotation (); }
int degrees = 0;
switch (rotation) { @Override
case Surface.ROTATION_0: public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
degrees = 0; super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break; if (permissions.length > 0 && grantResults.length > 0) {
case Surface.ROTATION_90: setPreviewResolution(videoWidth, videoHeight);
degrees = 90; if (!mSmartCameraView.startCamera()) {
break; Log.e(TAG, "startCamera error...");
case Surface.ROTATION_180: }
degrees = 180; }
break; }
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
} else {
// back-facing
result = ( info.orientation - degrees + 360) % 360;
}
Log.i(TAG, "curDegree: "+ result);
mDegree = result;
}
@Override
protected void onDestroy(){
if ( isStart ) {
isStart = false;
if(mSmartCameraView != null)
{
mSmartCameraView.stopCamera();
}
Log.i(TAG, "onDestroy StopPublish");
}
super.onDestroy();
finish();
System.exit(0);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(permissions.length > 0 && grantResults.length > 0){
Log.i(TAG, "permission=" + permissions[0] + "----grantResult=" + grantResults[0]);
setPreviewResolution(videoWidth, videoHeight);
if (!mSmartCameraView.startCamera()) {
Log.e(TAG, "startCamera error...");
}
}
}
} }

@ -3,4 +3,14 @@
<string name="swap">切换</string> <string name="swap">切换</string>
<string name="start">开始</string> <string name="start">开始</string>
<string name="stop">停止</string> <string name="stop">停止</string>
<string name="mute">静音</string>
<string name="voice">取消静音</string>
<string name="effect_beauty">美颜</string>
<string name="effect_cool">冷酷</string>
<string name="effect_sunrise">日出</string>
<string name="effect_sketch">素描</string>
<string name="effect_white">纯白</string>
<string name="effect_romantic">浪漫</string>
<string name="effect_raw">原图</string>
</resources> </resources>

Loading…
Cancel
Save