From 58ea08063638992ac998a595378ff7d4183aec93 Mon Sep 17 00:00:00 2001 From: Dylan McIntyre Date: Fri, 20 Jan 2017 10:50:20 -0500 Subject: [PATCH] Camera preview --- .../camerakit/demo/MainActivity.java | 132 +++++++++++++++--- .../camerakit/demo/PreviewDialog.java | 34 +++++ demo/src/main/res/layout/activity_main.xml | 4 +- demo/src/main/res/layout/dialog_preview.xml | 17 +++ 4 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 demo/src/main/java/com/wonderkiln/camerakit/demo/PreviewDialog.java create mode 100644 demo/src/main/res/layout/dialog_preview.xml diff --git a/demo/src/main/java/com/wonderkiln/camerakit/demo/MainActivity.java b/demo/src/main/java/com/wonderkiln/camerakit/demo/MainActivity.java index 46ae68fb..380fc0c8 100644 --- a/demo/src/main/java/com/wonderkiln/camerakit/demo/MainActivity.java +++ b/demo/src/main/java/com/wonderkiln/camerakit/demo/MainActivity.java @@ -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); + + } + } } diff --git a/demo/src/main/java/com/wonderkiln/camerakit/demo/PreviewDialog.java b/demo/src/main/java/com/wonderkiln/camerakit/demo/PreviewDialog.java new file mode 100644 index 00000000..6958dc70 --- /dev/null +++ b/demo/src/main/java/com/wonderkiln/camerakit/demo/PreviewDialog.java @@ -0,0 +1,34 @@ +package com.wonderkiln.camerakit.demo; + +import android.app.Dialog; +import android.content.Context; +import android.graphics.Bitmap; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.widget.ImageView; + +import butterknife.BindView; +import butterknife.ButterKnife; + +public class PreviewDialog extends Dialog { + + @BindView(R.id.image) + ImageView image; + + private Bitmap bitmap; + + public PreviewDialog(@NonNull Context context, Bitmap bitmap) { + super(context); + this.bitmap = bitmap; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.dialog_preview); + ButterKnife.bind(this); + + image.setImageBitmap(bitmap); + } + +} diff --git a/demo/src/main/res/layout/activity_main.xml b/demo/src/main/res/layout/activity_main.xml index d570f88e..c1743757 100644 --- a/demo/src/main/res/layout/activity_main.xml +++ b/demo/src/main/res/layout/activity_main.xml @@ -15,7 +15,7 @@ @@ -112,12 +112,14 @@ android:orientation="horizontal"> + + + + + + + \ No newline at end of file