zoukankan      html  css  js  c++  java
  • 手机拍照功能的简单实现

    1、main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        
    
        <SurfaceView 
            android:id="@+id/surfaceview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
        
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:orientation="horizontal"
            >
            
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="对焦"
                android:onClick="focus"
                android:layout_marginRight="10dp"
                />
            
            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拍照"
                android:onClick="takepicture"
                android:layout_marginRight="10dp"
                />
        </LinearLayout>
    </RelativeLayout>
    


    2、MainActivity

    package com.njupt.takepicture1;
    
    import java.io.File;
    import java.io.FileOutputStream;
    
    import android.hardware.Camera;
    import android.hardware.Camera.Parameters;
    import android.hardware.Camera.PictureCallback;
    import android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.SurfaceHolder;
    import android.view.SurfaceHolder.Callback;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    
    public class MainActivity extends Activity {
    
    	private SurfaceView surfaceview;
    	private Camera camera;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		requestWindowFeature(Window.FEATURE_NO_TITLE);
    		setContentView(R.layout.main);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            
    		
    		surfaceview = (SurfaceView) findViewById(R.id.surfaceview);
    		SurfaceHolder holder = surfaceview.getHolder();
    		holder.setFixedSize(176, 144);
    		holder.setKeepScreenOn(true);
    		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    		holder.addCallback(new MySurfaceCallback());
    	}
    
    	private class MySurfaceCallback implements Callback {
    		@Override
    		public void surfaceChanged(SurfaceHolder holder, int format, int width,
    				int height) {
    			// TODO Auto-generated method stub
    
    		}
    
    		@Override
    		public void surfaceCreated(SurfaceHolder holder) {
    			try {
    				camera = Camera.open();
    				Parameters params = camera.getParameters();
    				params.setJpegQuality(90);
    				params.setPictureSize(1024, 768);
    				params.setPreviewFrameRate(10);
    
    				camera.setParameters(params);
    				camera.setPreviewDisplay(surfaceview.getHolder());
    				camera.startPreview();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    
    		@Override
    		public void surfaceDestroyed(SurfaceHolder holder) {
    			if (camera != null) {
    				camera.release();
    				camera = null;
    			}
    		}
    	}
    
    	public void takepicture(View v) {
    		camera.takePicture(null, null, new MyPictureCallback());
    	}
    
    	private class MyPictureCallback implements PictureCallback {
    		@Override
    		public void onPictureTaken(byte[] data, Camera camera) {
    			try {
    				File file = new File(Environment.getExternalStorageDirectory(),
    						System.currentTimeMillis() + ".jpg");
    				FileOutputStream fos = new FileOutputStream(file);
    				fos.write(data);
    				camera.startPreview();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    


    3、AndroidManifest.xml

    注册权限

    <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>


    横屏显示:

    <activity
                android:name="com.njupt.takepicture1.MainActivity"
                android:label="@string/app_name" 
                android:screenOrientation="landscape">


  • 相关阅读:
    多点触摸时代来了,第一个是什么呢
    能看出点幻影刺客的风采
    XNA 贴图载入的新状况
    WAR3的模型,幻影刺客,我来了。
    幻影刺客跑起来啦
    TM2T入手
    哥们儿,你终于来了….哈哈,等的花儿都谢了。
    鸣谢 MAGOSX.com
    彩色版幻影刺客留念
    好吧,效果先到这里吧
  • 原文地址:https://www.cnblogs.com/james1207/p/3328988.html
Copyright © 2011-2022 走看看