|
|
|
@ -1,7 +1,11 @@ |
|
|
|
|
package com.wonderkiln.camerakit.demo; |
|
|
|
|
|
|
|
|
|
import android.graphics.Bitmap; |
|
|
|
|
import android.graphics.BitmapFactory; |
|
|
|
|
import android.os.Bundle; |
|
|
|
|
import android.os.Handler; |
|
|
|
|
import android.support.v7.app.AppCompatActivity; |
|
|
|
|
import android.view.View; |
|
|
|
|
import android.view.ViewGroup; |
|
|
|
|
import android.view.ViewTreeObserver; |
|
|
|
|
import android.widget.CheckBox; |
|
|
|
@ -10,6 +14,7 @@ import android.widget.EditText; |
|
|
|
|
import android.widget.TextView; |
|
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
|
import com.wonderkiln.camerakit.CameraListener; |
|
|
|
|
import com.wonderkiln.camerakit.CameraView; |
|
|
|
|
import com.wonderkiln.camerakit.Constants; |
|
|
|
|
|
|
|
|
@ -17,6 +22,7 @@ import butterknife.BindView; |
|
|
|
|
import butterknife.ButterKnife; |
|
|
|
|
import butterknife.OnCheckedChanged; |
|
|
|
|
import butterknife.OnClick; |
|
|
|
|
import butterknife.OnFocusChange; |
|
|
|
|
import butterknife.OnTextChanged; |
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity { |
|
|
|
@ -27,6 +33,12 @@ public class MainActivity extends AppCompatActivity { |
|
|
|
|
@BindView(R.id.camera) |
|
|
|
|
CameraView camera; |
|
|
|
|
|
|
|
|
|
@BindView(R.id.modeQuality) |
|
|
|
|
CheckBox modeQuality; |
|
|
|
|
|
|
|
|
|
@BindView(R.id.modeSpeed) |
|
|
|
|
CheckBox modeSpeed; |
|
|
|
|
|
|
|
|
|
@BindView(R.id.screenWidth) |
|
|
|
|
TextView screenWidth; |
|
|
|
|
|
|
|
|
@ -45,6 +57,8 @@ public class MainActivity extends AppCompatActivity { |
|
|
|
|
@BindView(R.id.height) |
|
|
|
|
EditText height; |
|
|
|
|
|
|
|
|
|
boolean blockInvalidation; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
@ -56,19 +70,13 @@ public class MainActivity extends AppCompatActivity { |
|
|
|
|
public void onGlobalLayout() { |
|
|
|
|
screenWidth.setText("screen: " + parent.getWidth() + "px"); |
|
|
|
|
screenHeight.setText("screen: " + parent.getHeight() + "px"); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!widthMatchParent.isChecked() && !widthWrapContent.isChecked()) { |
|
|
|
|
width.setText(String.valueOf(camera.getWidth())); |
|
|
|
|
width.setHint("pixels"); |
|
|
|
|
} else if (widthMatchParent.isChecked()) { |
|
|
|
|
width.setHint("match_parent"); |
|
|
|
|
width.setText(""); |
|
|
|
|
} else if (widthWrapContent.isChecked()) { |
|
|
|
|
width.setHint("wrap_content"); |
|
|
|
|
width.setText(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
height.setText(String.valueOf(camera.getHeight())); |
|
|
|
|
camera.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { |
|
|
|
|
@Override |
|
|
|
|
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { |
|
|
|
|
invalidateParameters(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
@ -87,7 +95,15 @@ public class MainActivity extends AppCompatActivity { |
|
|
|
|
|
|
|
|
|
@OnClick(R.id.capturePhoto) |
|
|
|
|
void capturePhoto() { |
|
|
|
|
|
|
|
|
|
camera.setCameraListener(new CameraListener() { |
|
|
|
|
@Override |
|
|
|
|
public void onPictureTaken(byte[] picture) { |
|
|
|
|
super.onPictureTaken(picture); |
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeByteArray(picture, 0, picture.length); |
|
|
|
|
new PreviewDialog(MainActivity.this, bitmap).show(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
camera.capturePicture(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnClick(R.id.captureVideo) |
|
|
|
@ -125,9 +141,36 @@ public class MainActivity extends AppCompatActivity { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnCheckedChanged(R.id.modeQuality) |
|
|
|
|
void modeQuality(CompoundButton buttonCompat, boolean checked) { |
|
|
|
|
modeSpeed.setChecked(false); |
|
|
|
|
if (checked) { |
|
|
|
|
camera.setPictureMode(CameraView.PICTURE_MODE_QUALITY); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
invalidateParameters(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnCheckedChanged(R.id.modeSpeed) |
|
|
|
|
void modeSpeed(CompoundButton buttonCompat, boolean checked) { |
|
|
|
|
modeQuality.setChecked(false); |
|
|
|
|
if (checked) { |
|
|
|
|
camera.setPictureMode(CameraView.PICTURE_MODE_SPEED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
invalidateParameters(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnFocusChange({ R.id.width, R.id.height }) |
|
|
|
|
void inputFocusChanged(View view, boolean f) { |
|
|
|
|
blockInvalidation = view.isFocused(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnTextChanged(value = R.id.width, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) |
|
|
|
|
void widthChanged() { |
|
|
|
|
|
|
|
|
|
if (width.isFocused()) { |
|
|
|
|
new Handler().postDelayed(new UpdateCameraRunnable(width), 2000); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnCheckedChanged(R.id.widthWrapContent) |
|
|
|
@ -139,6 +182,8 @@ public class MainActivity extends AppCompatActivity { |
|
|
|
|
|
|
|
|
|
widthMatchParent.setChecked(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
invalidateParameters(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnCheckedChanged(R.id.widthMatchParent) |
|
|
|
@ -150,20 +195,75 @@ public class MainActivity extends AppCompatActivity { |
|
|
|
|
|
|
|
|
|
widthWrapContent.setChecked(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
invalidateParameters(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnTextChanged(value = R.id.height, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) |
|
|
|
|
void heightChanged() { |
|
|
|
|
|
|
|
|
|
if (height.isFocused()) { |
|
|
|
|
new Handler().postDelayed(new UpdateCameraRunnable(height), 2000); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnCheckedChanged(R.id.heightWrapContent) |
|
|
|
|
void heightWrapContent() { |
|
|
|
|
|
|
|
|
|
invalidateParameters(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@OnCheckedChanged(R.id.heightMatchParent) |
|
|
|
|
void heightMatchParent() { |
|
|
|
|
invalidateParameters(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void invalidateParameters() { |
|
|
|
|
if (blockInvalidation) return; |
|
|
|
|
|
|
|
|
|
blockInvalidation = true; |
|
|
|
|
|
|
|
|
|
if (!widthMatchParent.isChecked() && !widthWrapContent.isChecked()) { |
|
|
|
|
width.setText(String.valueOf(camera.getWidth())); |
|
|
|
|
width.setHint("pixels"); |
|
|
|
|
} else if (widthMatchParent.isChecked()) { |
|
|
|
|
width.setHint("match_parent"); |
|
|
|
|
width.setText(""); |
|
|
|
|
} else if (widthWrapContent.isChecked()) { |
|
|
|
|
width.setHint("wrap_content"); |
|
|
|
|
width.setText(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
height.setText(String.valueOf(camera.getHeight())); |
|
|
|
|
|
|
|
|
|
blockInvalidation = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private class UpdateCameraRunnable implements Runnable { |
|
|
|
|
|
|
|
|
|
private EditText editText; |
|
|
|
|
private String startText; |
|
|
|
|
|
|
|
|
|
public UpdateCameraRunnable(EditText editText) { |
|
|
|
|
this.startText = editText.getText().toString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
if (startText.equals(editText.getText().toString())) { |
|
|
|
|
ViewGroup.LayoutParams layoutParams = camera.getLayoutParams(); |
|
|
|
|
switch (editText.getId()) { |
|
|
|
|
case R.id.width: |
|
|
|
|
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
case R.id.height: |
|
|
|
|
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
camera.setLayoutParams(layoutParams); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|