Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…pps#95 lab1 lab2 lab3 lab4 lab5 lab8 lab9
  • Loading branch information
WL101ZYF committed May 11, 2016
1 parent f2a5dba commit 16a1c1c
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,63 +1,192 @@
package edu.hzuapps.androidworks.homeworks.net1314080903101;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;

import java.io.FileNotFoundException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.net.Uri;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.os.Environment;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

public class Net1314080903101Activity extends AppCompatActivity {
public class Net1314080903101Activity extends ActionBarActivity {

private View layout;
private Camera camera;

private Uri imageUri;
private ImageButton imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
//设置窗口没有标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置窗口全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_net1314080903101);

imageView = (ImageButton) findViewById(R.id.imageView);
imageUri = Uri.parse("file:///sdcard/temp.jpg");

Intent intent;
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //调用照相机
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //指定照相机拍到的照片原图存放位置
startActivityForResult(intent, 1);

//利用layout方法,找到两个按钮控件
layout = this.findViewById(R.id.buttonlayout);
//获取摄像头窗口
SurfaceView surfaceView = (SurfaceView) this.findViewById(R.id.surfaceview);
//将获取的摄像头填满整个窗口
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//设置窗口分辨率
surfaceView.getHolder().setFixedSize(176, 144);
//保持屏幕高亮,不要锁机
surfaceView.getHolder().setKeepScreenOn(true);
//设置摄像头被调用监听事件
surfaceView.getHolder().addCallback(new SurfaceCallback());


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_net1314080903101, menu);
return true;
/*
* 通过switch (v.getId()) 选择拍照事件和对焦事件
*/
public void takepicture(View v)
{
if(camera != null)
{
switch (v.getId())
{
case R.id.takepicture:
//拍照片经过压缩处理后的图片调用MyPictureCallback方法
camera.takePicture(null, null, new MyPictureCallback());
break;
case R.id.autofocus:
//如果不想得到对焦事件,传送NULL事件进去
camera.autoFocus(null);

default:
break;
}


}

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

// onActivityResult需要按提示修改
if(resultCode == Activity.RESULT_OK)
{Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块

/*
* 获取图片对象
*/
private final class MyPictureCallback implements PictureCallback
{

public void onPictureTaken(byte[] data, Camera camera)
{

try
{
//将文件存储在SD卡的根目录,并以系统时间将文件命名
File jpgFile = new File(Environment.getExternalStorageDirectory(),
java.lang.System.currentTimeMillis() + ".jpg");
//文件输出流对象
FileOutputStream outStream = new FileOutputStream(jpgFile);
//将文件数据存储到文件中
outStream.write(data);
//关闭输出流
outStream.close();
//开始预览照片
camera.startPreview();
}
catch (IOException e)
{
e.printStackTrace();
}
}

}


/*
* 设置摄像头参数
*/
private final class SurfaceCallback implements android.view.SurfaceHolder.Callback
{

public void surfaceCreated(SurfaceHolder holder)
{
try
{
//打开摄像头
camera = Camera.open();
//获取摄像头参数对象
Camera.Parameters parameters = camera.getParameters();
//设置摄像头分辨率
parameters.setPreviewSize(800, 480);
//设置摄像头捕获画面的频率为每秒5个画面
parameters.setPreviewFrameRate(5);
//设置拍摄照片的大小
parameters.setPictureSize(1024, 768);
//设置捕捉图像的JPEG画质
parameters.setJpegQuality(80);
//把参数返回给摄像头
camera.setParameters(parameters);
//显示摄像头捕获画面
camera.setPreviewDisplay(holder);
//开始预览摄像头
camera.startPreview();
//获取摄像头详细参数,并且打印出来
//Log.i("MainActivity", parameters.flatten());

}
catch (Exception e)
{
e.printStackTrace();
}

}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int heigh)
{

}

public void surfaceDestroyed(SurfaceHolder holder)
{
//如果摄像头不使用时,关闭摄像头
if(camera != null)
{
camera.release();
camera = null;
}


}

}


/*
*屏幕被触摸事件
*屏幕被按下后,显示相对布局里面的两个按钮
*/
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
layout.setVisibility(ViewGroup.VISIBLE);

}

return super.onTouchEvent(event);

}

}
9 changes: 4 additions & 5 deletions app/src/main/res/layout/activity_net1314080903101.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context=".Net1314080903101activity">
tools:context="edu.hzuapps.androidworks.homeworks.net1314080903101.Net1314080903101Activity">

<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
Expand All @@ -22,7 +21,7 @@

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_net1314080903101"/>
<include layout="@layout/content_net1314080903101" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
Expand Down
60 changes: 38 additions & 22 deletions app/src/main/res/layout/content_net1314080903101.xml
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_net1314080903101"
tools:context="edu.hzuapps.androidworks.homeworks.net13140809031013101.Net1314080903101activity">
tools:context="edu.hzuapps.androidworks.homeworks.net1314080903101.Net1314080903101Activity"
tools:showIn="@layout/activity_net1314080903101">

<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surfaceview"/>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:id="@+id/buttonlayout">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="90dp"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="5dp"
android:text="@string/takepicture"
android:onClick="takepicture"
android:id="@+id/takepicture" />

<ImageButton
android:id="@+id/imageView"
android:layout_width="65dp"
android:layout_height="70dp"
android:layout_marginLeft="10dp"
android:src="@drawable/net1314080903101" />

<TextView

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:text="相机" />
</LinearLayout>
</RelativeLayout>
android:layout_toLeftOf="@id/takepicture"
android:layout_alignTop="@id/takepicture"
android:layout_marginRight="20dp"
android:text="@string/autofocus"
android:onClick="takepicture"
android:id="@+id/autofocus" />

</RelativeLayout>
</FrameLayout>

0 comments on commit 16a1c1c

Please sign in to comment.