Skip to content

Commit

Permalink
完善屏幕缩放
Browse files Browse the repository at this point in the history
  • Loading branch information
wysaid committed Jul 20, 2015
1 parent a62ded3 commit 7a16acf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public boolean onTouch(View v, MotionEvent event) {
});
}

@Override
public void onDestroy() {
super.onDestroy();
System.exit(0);
}

@Override
public void onPause() {
super.onPause();
System.exit(0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/org/wysaid/camera/CameraInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class CameraInstance {
private int mDefaultCameraID = -1;

private static CameraInstance mThisInstance;
private int mPreviewWidth;
private int mPreviewHeight;

private CameraInstance() {}

Expand All @@ -41,6 +43,9 @@ public static synchronized CameraInstance getInstance() {

public boolean isPreviewing() { return mIsPreviewing; }

public int previewWidth() { return mPreviewWidth; }
public int previewHeight() { return mPreviewHeight; }

public interface CameraOpenCallback {
void cameraReady();
}
Expand Down Expand Up @@ -159,8 +164,9 @@ public void initCamera(int previewRate) {
prevSz = sz;
}

mCameraDevice.setDisplayOrientation(90);

mParams.setPreviewSize(prevSz.width, prevSz.height);
mParams.setPictureSize(picSz.width, picSz.height);
mParams.setFocusMode(mParams.FOCUS_MODE_CONTINUOUS_VIDEO);
mParams.setPreviewFrameRate(previewRate); //设置相机预览帧率

Expand All @@ -173,6 +179,9 @@ public void initCamera(int previewRate) {
Camera.Size szPic = mParams.getPictureSize();
Camera.Size szPrev = mParams.getPreviewSize();

mPreviewWidth = szPrev.width;
mPreviewHeight = szPrev.height;

Log.i(LOG_TAG, String.format("Camera Picture Size: %d x %d", szPic.width, szPic.height));
Log.i(LOG_TAG, String.format("Camera Preview Size: %d x %d", szPrev.width, szPrev.height));
}
Expand Down
43 changes: 19 additions & 24 deletions app/src/main/java/org/wysaid/glfunctions/MyGLSurfaceView.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ public class MyGLSurfaceView extends GLSurfaceView implements GLSurfaceView.Rend
private SurfaceTexture mSurfaceTexture;
private int mTextureID;

public class Viewport {
public int x, y;
public int width, height;
}

public Viewport drawViewport;

private CameraInstance cameraInstance() {
return CameraInstance.getInstance();
}


public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(LOG_TAG, "MyGLSurfaceView Construct...");
Expand Down Expand Up @@ -69,31 +75,19 @@ public void cameraReady() {
Log.i(LOG_TAG, "tryOpenCamera OK...");
}
});

resetLayouts();
}

private void resetLayouts() {
FrameLayout frame = (FrameLayout)getParent();
MyGLSurfaceView view = (MyGLSurfaceView)findViewById(R.id.myGLSurfaceView);

try {
private void calcViewport() {
float camHeight = (float)cameraInstance().previewWidth();
float camWidth = (float)cameraInstance().previewHeight();

int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
drawViewport = new Viewport();

frame.measure(w, h);
view.measure(w, h);

float sx = frame.getWidth() / (float) view.getWidth();
float sy = frame.getHeight() / (float) view.getHeight();
view.setScaleX(sx);
view.setScaleY(sy);
}
catch (Exception e) {
e.printStackTrace();
Log.e(LOG_TAG, "resetLayouts failed!");
}
float scale = Math.min(viewWidth / camWidth, viewHeight / camHeight);
drawViewport.width = (int)(camWidth * scale);
drawViewport.height = (int)(camHeight * scale);
drawViewport.x = (viewWidth - drawViewport.width) / 2;
drawViewport.y = (viewHeight - drawViewport.height) / 2;
}

@Override
Expand All @@ -103,11 +97,11 @@ public void onSurfaceChanged(GL10 gl, int width, int height) {
viewWidth = width;
viewHeight = height;

GLES20.glViewport(0, 0, width, height);

if(!cameraInstance().isPreviewing()) {
cameraInstance().startPreview(mSurfaceTexture);
}

calcViewport();
}

@Override
Expand All @@ -120,6 +114,7 @@ public void surfaceDestroyed(SurfaceHolder holder) {
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

GLES20.glViewport(drawViewport.x, drawViewport.y, drawViewport.width, drawViewport.height);
// myRenderer.renderTexture(mTextureID);
myRenderer.renderTextureExternalOES(mTextureID);

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

<org.wysaid.glfunctions.MyGLSurfaceView
android:id="@+id/myGLSurfaceView"
android:layout_width="480px"
android:layout_height="640px" />
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

<LinearLayout
Expand Down

0 comments on commit 7a16acf

Please sign in to comment.