camera
This commit is contained in:
@@ -17,6 +17,10 @@
|
||||
|
||||
<!-- 相机权限 -->
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
<uses-feature
|
||||
android:name="android.hardware.camera"
|
||||
android:required="false" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<application
|
||||
android:name=".NutritionMaster"
|
||||
@@ -33,6 +37,7 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".camera.FoodMaterialCamera" />
|
||||
|
||||
<receiver
|
||||
android:name=".step.StepStarter"
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.example.ninefourone.nutritionmaster.camera;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Camera;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
|
||||
import com.orhanobut.logger.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by ScorpioMiku on 2018/9/3.
|
||||
*/
|
||||
|
||||
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
|
||||
|
||||
private SurfaceHolder mHolder;
|
||||
private Camera camera;
|
||||
|
||||
public CameraPreview(Context context, Camera camera) {
|
||||
super(context);
|
||||
this.camera = camera;
|
||||
mHolder = getHolder();
|
||||
mHolder.addCallback(this);
|
||||
// mHolder.setType();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
try {
|
||||
camera.setPreviewDisplay(holder);
|
||||
camera.startPreview();
|
||||
} catch (IOException e) {
|
||||
Logger.e(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
if (mHolder.getSurface() == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
camera.stopPreview();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
camera.setPreviewDisplay(mHolder);
|
||||
camera.startPreview();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package com.example.ninefourone.nutritionmaster.camera;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.hardware.Camera;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.utils.MessageUtils;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
/**
|
||||
* Created by ScorpioMiku on 2018/9/3.
|
||||
*/
|
||||
|
||||
public class FoodMaterialCamera extends AppCompatActivity {
|
||||
@BindView(R.id.camera_preview)
|
||||
FrameLayout mCameraLayout;
|
||||
@BindView(R.id.results_text_view)
|
||||
TextView resultsTextView;
|
||||
@BindView(R.id.more_take_photo_button_capture)
|
||||
ImageView moreTakePhotoButtonCapture;
|
||||
@BindView(R.id.more_takephoto_ok)
|
||||
ImageView moreTakephotoOk;
|
||||
@BindView(R.id.more_camera_cover_linearlayout)
|
||||
FrameLayout moreCameraCoverLinearlayout;
|
||||
|
||||
private Camera mCamera;
|
||||
private CameraPreview mPreview;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
//取消toolbar
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
//设置全屏
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
//注意:上面两个设置必须写在setContentView前面
|
||||
setContentView(R.layout.cameras_layout);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
if (!checkCameraHardware(this)) {
|
||||
MessageUtils.MakeToast("不支持相机");
|
||||
} else {
|
||||
openCamera();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前设备是否有相机
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
private boolean checkCameraHardware(Context context) {
|
||||
if (context.getPackageManager().hasSystemFeature(
|
||||
PackageManager.FEATURE_CAMERA)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 打开相机
|
||||
*/
|
||||
private void openCamera() {
|
||||
if (null == mCamera) {
|
||||
mCamera = getCameraInstance();
|
||||
mPreview = new CameraPreview(this, mCamera);
|
||||
// mPreview.setOnTouchListener(new View.OnTouchListener() {
|
||||
// @Override
|
||||
// public boolean onTouch(View v, MotionEvent event) {
|
||||
// mCamera.autoFocus(null);
|
||||
// return false;
|
||||
// }
|
||||
// });
|
||||
mCameraLayout.addView(mPreview);
|
||||
mCamera.startPreview();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取相机
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Camera getCameraInstance() {
|
||||
Camera c = null;
|
||||
try {
|
||||
c = Camera.open();
|
||||
Camera.Parameters mParameters = c.getParameters();
|
||||
mParameters.setPictureSize(720, 1280);
|
||||
c.setParameters(mParameters);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对焦回调,对焦完成后进行拍照
|
||||
*/
|
||||
private Camera.AutoFocusCallback mAutoFocusCallback = new Camera.AutoFocusCallback() {
|
||||
@Override
|
||||
public void onAutoFocus(boolean success, Camera camera) {
|
||||
if (success) {
|
||||
mCamera.takePicture(null, null, mPictureCallback);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 拍照回调
|
||||
*/
|
||||
private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
|
||||
@Override
|
||||
public void onPictureTaken(final byte[] data, Camera camera) {
|
||||
MessageUtils.MakeToast("拍照!");
|
||||
mCamera.startPreview();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 两个按钮的事件
|
||||
*
|
||||
* @param view
|
||||
*/
|
||||
@OnClick({R.id.more_take_photo_button_capture, R.id.more_takephoto_ok})
|
||||
public void onViewClicked(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.more_take_photo_button_capture:
|
||||
mCamera.autoFocus(mAutoFocusCallback);
|
||||
break;
|
||||
case R.id.more_takephoto_ok:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.ninefourone.nutritionmaster.utils;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* Created by ScorpioMiku on 2018/9/3.
|
||||
*/
|
||||
|
||||
public class PermissionUtils {
|
||||
|
||||
/**
|
||||
* 动态获取相机权限
|
||||
* @param context
|
||||
* @param photoFromCamera
|
||||
*/
|
||||
public static void requestCameraPermission(Context context, String photoFromCamera) {
|
||||
|
||||
}
|
||||
}
|
||||
5
app/src/main/res/drawable-v24/selector.xml
Normal file
5
app/src/main/res/drawable-v24/selector.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:state_pressed="false" android:drawable="@mipmap/camera_button" />
|
||||
<item android:state_pressed="true" android:drawable="@mipmap/camera_button_pressed" />
|
||||
</selector>
|
||||
4
app/src/main/res/drawable/ic_ok.xml
Normal file
4
app/src/main/res/drawable/ic_ok.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<vector android:height="24dp" android:viewportHeight="1024.0"
|
||||
android:viewportWidth="1024.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="#ffffff" android:pathData="M512,62.4c-248.3,0 -449.6,201.3 -449.6,449.6s201.3,449.6 449.6,449.6 449.6,-201.3 449.6,-449.6S760.3,62.4 512,62.4zM728.1,403.9 L478.8,653.3c-4.6,4.6 -10.6,6.9 -16.6,6.9s-12,-2.3 -16.6,-6.9c-37.4,-37.4 -149.6,-149.6 -149.6,-149.6 -9.2,-9.2 -9.2,-24.1 0,-33.2 9.2,-9.2 24.1,-9.2 33.2,0l133,109.5 232.7,-209.2c9.2,-9.2 24.1,-9.2 33.2,0C737.3,379.9 737.3,394.8 728.1,403.9z"/>
|
||||
</vector>
|
||||
80
app/src/main/res/layout/cameras_layout.xml
Normal file
80
app/src/main/res/layout/cameras_layout.xml
Normal file
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
|
||||
<LinearLayout
|
||||
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="9">
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/camera_preview"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/results_text_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:gravity="center"
|
||||
tools:text="@string/app_name" />
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1.2"
|
||||
android:alpha="0.4"
|
||||
android:background="#8A000000">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/more_take_photo_button_capture"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="60dp"
|
||||
android:layout_centerInParent="true"
|
||||
android:src="@drawable/selector" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/more_takephoto_ok"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="60dp"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginLeft="50dp"
|
||||
android:layout_marginTop="15dp"
|
||||
android:layout_toRightOf="@id/more_take_photo_button_capture"
|
||||
android:src="@drawable/ic_ok" />
|
||||
|
||||
</RelativeLayout>
|
||||
</LinearLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/more_camera_cover_linearlayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:alpha="0.3"
|
||||
android:background="@color/place_holder"
|
||||
android:visibility="invisible">
|
||||
|
||||
<ProgressBar
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center" />
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
</FrameLayout>
|
||||
BIN
app/src/main/res/mipmap-xxhdpi/camera_button.png
Normal file
BIN
app/src/main/res/mipmap-xxhdpi/camera_button.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
BIN
app/src/main/res/mipmap-xxhdpi/camera_button_pressed.png
Normal file
BIN
app/src/main/res/mipmap-xxhdpi/camera_button_pressed.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.0 KiB |
Reference in New Issue
Block a user