安卓中調用攝像頭和相冊中要聲明權限,用Android Studio做app的話就可以
創新互聯公司堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網站設計、成都做網站、企業官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的運河網站設計、移動媒體設計的需求,幫助企業找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!
自己寫一個“攝像頭照相軟件”的東西,然后實現了在web程序中調用攝像頭,可以通過js代碼控制拍照,通過ajax技術實現數據的上傳,雖然我沒有在asp.net程序中測試,但是應該支持.net技術,也可以實現在asp.net的項目中采集攝像頭數據,例如用來通過攝像頭拍照,拍些大頭貼等
//抽象的形狀類
abstract class Shape{
abstract double getArea(); //抽象的求面積方法
}
//矩形類
class Rectangle extends Shape{
protected double width;
protected double height;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
@Override
double getArea() { //實現父類的方法
return this.width * this.height;
}
}
//橢圓類
class Ellipse extends Shape{
protected double a;
protected double b;
public Ellipse(double a, double b){
this.a = a;
this.b = b;
}
@Override
double getArea() {
return Math.PI * this.a * this.b;
}
}
public class TestAbstract {
public static void main(String[] args) {
Shape s;
s = new Rectangle(3, 4);
System.out.println("矩形的面積 : " + s.getArea());
s = new Ellipse(4, 3);
System.out.println("橢圓的面積 : " + s.getArea());
}
}
首先到sun下載最新的jmf,然后安裝。
然后,說一下需求
1. 用攝像頭拍照
2. 在文本框輸入文件名
3. 按下拍照按鈕,獲取攝像頭內的圖像
4. 在拍下的照片上有一紅框截取固定大小的照片。
5. 保存為本地圖像為jpg格式,不得壓縮畫質
技術關鍵,相信也是大家最感興趣的部分也就是如何讓一個攝像頭工作,并拍下一張照片了。
利用jmf,代碼很簡單:
//利用這三個類分別獲取攝像頭驅動,和獲取攝像頭內的圖像流,獲取到的圖像流是一個swing的component組件類
public static player player = null;
private capturedeviceinfo di = null;
private medialocator ml = null;
//文檔中提供的驅動寫法,為何這么寫我也不知:)
string str1 = "vfw:logitech usb video camera:0 ";
string str2 = "vfw:microsoft wdm image capture (win32):0 ";
di = capturedevicemanager.getdevice(str2);
ml = di.getlocator();
try
{
player = manager.createrealizedplayer(ml);
player.start();
component comp;
if ((comp = player.getvisualcomponent()) != null)
{
add(comp, borderlayout.north);
}
}
catch (exception e)
{
e.printstacktrace();
}
接下來就是點擊拍照,獲取攝像頭內的當前圖像。
代碼也是很簡單:
private jbutton capture;
private buffer buf = null;
private buffertoimage btoi = null;
private imagepanel imgpanel = null;
private image img = null;
private imagepanel imgpanel = null;
jcomponent c = (jcomponent) e.getsource();
if (c == capture)//如果按下的是拍照按鈕
{
framegrabbingcontrol fgc =(framegrabbingcontrol) player.getcontrol( "javax.media.control.framegrabbingcontrol ");
buf = fgc.grabframe(); // 獲取當前禎并存入buffer類
btoi = new buffertoimage((videoformat) buf.getformat());
img = btoi.createimage(buf); // show the image
imgpanel.setimage(img);
}
保存圖像的就不多說了,以下為示例代碼
bufferedimage bi = (bufferedimage) createimage(imgwidth, imgheight);
graphics2d g2 = bi.creategraphics();
g2.drawimage(img, null, null);
fileoutputstream out = null;
try
{
out = new fileoutputstream(s);
}
catch (java.io.filenotfoundexception io)
{
system.out.println( "file not found ");
}
jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
jpegencodeparam param = encoder.getdefaultjpegencodeparam(bi);
param.setquality(1f, false);//不壓縮圖像
encoder.setjpegencodeparam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.ioexception io)
{
system.out.println( "ioexception ");
}
把.jar文件導入。下載了jmf后需要安裝,安裝后你的那個jmf目錄下就會有一個lib文件夾里面有.jar文件,然后打開eclipse,右鍵選擇你的工程-〉屬性-〉java build path- library-〉add external jars 找到你的jmf目錄下lib的那個文件夾然后選中那些文件導入就ok了。
然后利用工具提供的導入文件幫助,一個一個導就OK了