相机java程序_以编程方式在Android上用相机拍照

请看下面的演示代码 .

这是用于UI的XML文件,

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/btnCapture"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Camera" />

这是你的Java类文件,

public class CameraDemoActivity extends Activity {

int TAKE_PHOTO_CODE = 0;

public static int count = 0;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// Here, we are making a folder named picFolder to store

// pics taken by the camera using this application.

final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";

File newdir = new File(dir);

newdir.mkdirs();

Button capture = (Button) findViewById(R.id.btnCapture);

capture.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

// Here, the counter will be incremented each time, and the

// picture taken by camera will be stored as 1.jpg,2.jpg

// and likewise.

count++;

String file = dir+count+".jpg";

File newfile = new File(file);

try {

newfile.createNewFile();

}

catch (IOException e)

{

}

Uri outputFileUri = Uri.fromFile(newfile);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

}

});

}

@Override

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

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {

Log.d("CameraDemo", "Pic saved");

}

}

}

Note:

在清单文件中指定以下权限,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值