Camera preview

pull/1/head
Dylan McIntyre 8 years ago
parent c20a7c3218
commit 58ea080636
  1. 130
      demo/src/main/java/com/wonderkiln/camerakit/demo/MainActivity.java
  2. 34
      demo/src/main/java/com/wonderkiln/camerakit/demo/PreviewDialog.java
  3. 4
      demo/src/main/res/layout/activity_main.xml
  4. 17
      demo/src/main/res/layout/dialog_preview.xml

@ -1,7 +1,11 @@
package com.wonderkiln.camerakit.demo; package com.wonderkiln.camerakit.demo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewTreeObserver; import android.view.ViewTreeObserver;
import android.widget.CheckBox; import android.widget.CheckBox;
@ -10,6 +14,7 @@ import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.wonderkiln.camerakit.CameraListener;
import com.wonderkiln.camerakit.CameraView; import com.wonderkiln.camerakit.CameraView;
import com.wonderkiln.camerakit.Constants; import com.wonderkiln.camerakit.Constants;
@ -17,6 +22,7 @@ import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnCheckedChanged; import butterknife.OnCheckedChanged;
import butterknife.OnClick; import butterknife.OnClick;
import butterknife.OnFocusChange;
import butterknife.OnTextChanged; import butterknife.OnTextChanged;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@ -27,6 +33,12 @@ public class MainActivity extends AppCompatActivity {
@BindView(R.id.camera) @BindView(R.id.camera)
CameraView camera; CameraView camera;
@BindView(R.id.modeQuality)
CheckBox modeQuality;
@BindView(R.id.modeSpeed)
CheckBox modeSpeed;
@BindView(R.id.screenWidth) @BindView(R.id.screenWidth)
TextView screenWidth; TextView screenWidth;
@ -45,6 +57,8 @@ public class MainActivity extends AppCompatActivity {
@BindView(R.id.height) @BindView(R.id.height)
EditText height; EditText height;
boolean blockInvalidation;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -56,19 +70,13 @@ public class MainActivity extends AppCompatActivity {
public void onGlobalLayout() { public void onGlobalLayout() {
screenWidth.setText("screen: " + parent.getWidth() + "px"); screenWidth.setText("screen: " + parent.getWidth() + "px");
screenHeight.setText("screen: " + parent.getHeight() + "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) @OnClick(R.id.capturePhoto)
void 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) @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) @OnTextChanged(value = R.id.width, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void widthChanged() { void widthChanged() {
if (width.isFocused()) {
new Handler().postDelayed(new UpdateCameraRunnable(width), 2000);
}
} }
@OnCheckedChanged(R.id.widthWrapContent) @OnCheckedChanged(R.id.widthWrapContent)
@ -139,6 +182,8 @@ public class MainActivity extends AppCompatActivity {
widthMatchParent.setChecked(false); widthMatchParent.setChecked(false);
} }
invalidateParameters();
} }
@OnCheckedChanged(R.id.widthMatchParent) @OnCheckedChanged(R.id.widthMatchParent)
@ -150,20 +195,75 @@ public class MainActivity extends AppCompatActivity {
widthWrapContent.setChecked(false); widthWrapContent.setChecked(false);
} }
invalidateParameters();
} }
@OnTextChanged(value = R.id.height, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) @OnTextChanged(value = R.id.height, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void heightChanged() { void heightChanged() {
if (height.isFocused()) {
new Handler().postDelayed(new UpdateCameraRunnable(height), 2000);
}
} }
@OnCheckedChanged(R.id.heightWrapContent) @OnCheckedChanged(R.id.heightWrapContent)
void heightWrapContent() { void heightWrapContent() {
invalidateParameters();
} }
@OnCheckedChanged(R.id.heightMatchParent) @OnCheckedChanged(R.id.heightMatchParent)
void 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);
}
}
} }

@ -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);
}
}

@ -15,7 +15,7 @@
<com.wonderkiln.camerakit.CameraView xmlns:app="http://schemas.android.com/apk/res-auto" <com.wonderkiln.camerakit.CameraView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/camera" android:id="@+id/camera"
android:layout_width="300dp" android:layout_width="match_parent"
android:layout_height="400dp" android:layout_height="400dp"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
app:ckFacing="back" /> app:ckFacing="back" />
@ -112,12 +112,14 @@
android:orientation="horizontal"> android:orientation="horizontal">
<android.support.v7.widget.AppCompatCheckBox <android.support.v7.widget.AppCompatCheckBox
android:id="@+id/modeQuality"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:checked="true" android:checked="true"
android:text="Quality" /> android:text="Quality" />
<android.support.v7.widget.AppCompatCheckBox <android.support.v7.widget.AppCompatCheckBox
android:id="@+id/modeSpeed"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="20dp" android:layout_marginLeft="20dp"

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside" />
</FrameLayout>
Loading…
Cancel
Save