<div class="article_manage clearfix">
<div class="article_l">
<span class="link_categories">
标签:
<a href="http://www.csdn.net/tag/%e5%a4%9a%e5%aa%92%e4%bd%93" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">多媒体</a>
</span>
</div>
<div class="article_r">
<span class="link_postdate">2017-02-21 17:10</span>
<span class="link_view" title="阅读次数">8人阅读</span>
<span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>
<span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('%e5%a4%9a%e5%aa%92%e4%bd%93%ef%bc%88%e6%8b%8d%e7%85%a7-%e5%bd%95%e5%83%8f%ef%bc%89','56289625');return false;" title="收藏" target="_blank">收藏</a></span>
<span class="link_report"> <a href="#report" onclick="javascript:report(56289625,2);return false;" title="举报">举报</a></span>
</div>
</div>
<div class="embody" style="display:none" id="embody">
<span class="embody_t">本文章已收录于:</span>
<div class="embody_c" id="lib" value="{"err":0,"msg":"ok","data":[]}"></div>
</div>
<style type="text/css">
.embody{
padding:10px 10px 10px;
margin:0 -20px;
border-bottom:solid 1px #ededed;
}
.embody_b{
margin:0 ;
padding:10px 0;
}
.embody .embody_t,.embody .embody_c{
display: inline-block;
margin-right:10px;
}
.embody_t{
font-size: 12px;
color:#999;
}
.embody_c{
font-size: 12px;
}
.embody_c img,.embody_c em{
display: inline-block;
vertical-align: middle;
}
.embody_c img{
width:30px;
height:30px;
}
.embody_c em{
margin: 0 20px 0 10px;
color:#333;
font-style: normal;
}
</style>
<script type="text/javascript">
$(function () {
try
{
var lib = eval("("+$("#lib").attr("value")+")");
var html = "";
if (lib.err == 0) {
$.each(lib.data, function (i) {
var obj = lib.data[i];
//html += '<img src="' + obj.logo + '"/>' + obj.name + " ";
html += ' <a href="' + obj.url + '" target="_blank">';
html += ' <img src="' + obj.logo + '">';
html += ' <em><b>' + obj.name + '</b></em>';
html += ' </a>';
});
if (html != "") {
setTimeout(function () {
$("#lib").html(html);
$("#embody").show();
}, 100);
}
}
} catch (err)
{ }
});
</script>
<div class="category clearfix">
<div class="category_l">
<img src="http://static.blog.csdn.net/images/category_icon.jpg">
<span>分类:</span>
</div>
<div class="category_r">
<label onclick="GetCategoryArticles('6706729','qq_35228658','top','56289625');">
<span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">android<em>(23)</em></span>
<img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
<img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
<div class="subItem">
<div class="subItem_t"><a href="http://blog.csdn.net/qq_35228658/article/category/6706729" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
<ul class="subItem_l" id="top_6706729">
</ul>
</div>
</label>
</div>
</div>
<script type="text/javascript" src="http://static.blog.csdn.net/scripts/category.js"></script>
<div class="bog_copyright">
<p class="copyright_p">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
</div>
多媒体拍照界面主要是用SurfaceView控件来显示照相机的画面
通过这个重写了3个方法
surfaceView.getHolder().addCallback()
- 1
- 1
方法为:
surfaceDestroyed--->当预览界面关闭时,触发
surfaceCreated----->预览界面被创建时,调用
surfaceChanged----->当预览界面的格式和大小发生改变时,触发
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
来看下界面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sv_main_view"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takephoto"
android:text="拍照"
/>
</RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Java代码:
//当预览界面被创建时
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
//打开照相机
camera = Camera.open();
//设置参数
Camera.Parameters parameters= camera.getParameters();
// 设置照片的输出格式
parameters.setPictureFormat(PixelFormat.JPEG);
// 设置照片的质量
parameters.set("jpeg-quality",85);
// 给照相机设置参数
camera.setParameters(parameters);
try {
//将摄像头的画面显示到SurfaceView控件上
camera.setPreviewDisplay(sv_main_view.getHolder());
//开始预览
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
//当预览界面关闭时
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
// 关闭照相机
if (camera!=null){
camera.release();
camera.stopPreview();
camera=null;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
写了一个点击的方法:
public void takephoto(View view){
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
//实例化文件输出流
//写
//压缩(图片)
//将字节数组转成bitmap
//这样来写的话图片大小会很小,只不过图片的清晰度不是好
Bitmap bitmap=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
try {
//图片的名字以及路径
FileOutputStream fos=new FileOutputStream("/mnt/sdcard/1/G150831_"+System.currentTimeMillis()+".png");
bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);
//重新拍照
camera.stopPreview();
camera.startPreview();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
来看下录制视频的:
java代码如下:
sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
//实例化媒体录制器
mediaRecorder = new MediaRecorder();
public void start(View view){
mediaRecorder.reset();
//设置参数
//设置音频,视频来源
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置输出格式
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//设置编码格式(视频 音频)
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//设置保存路径
mediaRecorder.setOutputFile("/mnt/sdcard/1/G150831_"+System.currentTimeMillis()+".mp4");
//画面
mediaRecorder.setPreviewDisplay(sv_main_surface.getHolder().getSurface());
//准备
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop(View view){
//停止
mediaRecorder.stop();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
要加的权限有3个:
<!-- 打开照相机的权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<!--SD卡权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--录音权限-->
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6