台灣最大程式設計社群網站
線上人數
863
 
會員總數:242947
討論主題:187843
歡迎您免費加入會員
討論區列表 >> Android APP 開發 >> Android studio 相機 在模擬器上會出現停止畫面
[]  
[我要回覆]
1
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
Android studio 相機 在模擬器上會出現停止畫面
價值 : 20 QP  點閱數:179 回應數:1

樓主

Hong Jin Huei
門外漢
0 8
201 8
發送站內信

我程式上沒有錯誤 但在模擬器上時會出現錯誤


import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

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


public class MainActivity extends Activity implements SurfaceHolder.Callback {
SurfaceHolder surfaceHolder;
SurfaceView surfaceView1;
Button button1;
ImageView imageView1;
Camera camera;

@SuppressLint("WrongConstant")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 =findViewById(R.id.startBtn);
//在AndroidManifest.xml中設定或是用下面的setRequestedOrientation(0)設定也可以
//0代表橫向、1代表縱向
setRequestedOrientation(90);
//設為横向顯示。因為攝影頭會自動翻轉90度,所以如果不横向顯示,看到的畫面就是翻轉的。
surfaceView1 = (SurfaceView) findViewById(R.id.surfaceView);
imageView1 = (ImageView) findViewById(R.id.img);
surfaceHolder = surfaceView1.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);
button1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

//自動對焦
camera.autoFocus(afcb);
}
});
}

Camera.PictureCallback jpeg = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {

Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
//byte數组轉換成Bitmap
imageView1.setImageBitmap(bmp);
//拍下圖片顯示在下面的ImageView裡
FileOutputStream fop;
try {
fop = new FileOutputStream("/sdcard/dd.jpg");
//實例化FileOutputStream,參數是生成路徑
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fop);
//壓缩bitmap寫進outputStream 參數:輸出格式  輸出質量  目標OutputStream
//格式可以為jpg,png,jpg不能存儲透明
fop.close();
System.out.println("拍照成功");
//關閉流
} catch (FileNotFoundException e) {

e.printStackTrace();
System.out.println("FileNotFoundException");

} catch (IOException e) {

e.printStackTrace();
System.out.println("IOException");
}
camera.startPreview();
//需要手動重新startPreview,否則停在拍下的瞬間
}

};

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

public void surfaceCreated(SurfaceHolder holder) {

camera = Camera.open();
try {

Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
//設置參數
camera.setPreviewDisplay(surfaceHolder);
//鏡頭的方向和手機相差90度,所以要轉向
//camera.setDisplayOrientation(90);
//攝影頭畫面顯示在Surface上
camera.startPreview();
} catch (IOException e) {

e.printStackTrace();
}

}

public void surfaceDestroyed(SurfaceHolder holder) {

System.out.println("surfaceDestroyed");
camera.stopPreview();
//關閉預覽
camera.release();
//
}

//自動對焦監聽式
Camera.AutoFocusCallback afcb = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {

if (success) {
//對焦成功才拍照
camera.takePicture(null, null, jpeg);
}
}
};
}

可以幫我看一下是甚麼問題嗎?
我也有在AndroidMainifest設定相機的權限
<uses-permission android:name="android.permission.CAMERA" />

搜尋相關Tags的文章: [ android 拍照 ] ,
本篇文章發表於2018-04-20 17:09
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
1樓
回應

Silent
檢舉此回應
從Android 6開始 需要另外請求權限 下面是完整代碼


public class MainActivity extends Activity implements SurfaceHolder.Callback {

public final int PERMISSIONS_REQUEST = 10;
SurfaceHolder surfaceHolder;
SurfaceView surfaceView1;
Button button1;
ImageView imageView1;
Camera camera;

@SuppressLint("WrongConstant")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(90);


//權限判斷
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
String[] permission = {Manifest.permission.CAMERA};
ActivityCompat.requestPermissions(this,permission , PERMISSIONS_REQUEST);//請求權限
}else{
initCamera();
}


}

private void initCamera(){

button1 =findViewById(R.id.startBtn);
//在AndroidManifest.xml中設定或是用下面的setRequestedOrientation(0)設定也可以
//0代表橫向、1代表縱向
//設為横向顯示。因為攝影頭會自動翻轉90度,所以如果不横向顯示,看到的畫面就是翻轉的。
surfaceView1 = (SurfaceView) findViewById(R.id.surfaceView);
imageView1 = (ImageView) findViewById(R.id.img);
surfaceHolder = surfaceView1.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//自動對焦
camera.autoFocus(afcb);
}
});


camera = Camera.open();
try {
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
//設置參數
camera.setPreviewDisplay(surfaceHolder);
//鏡頭的方向和手機相差90度,所以要轉向
//camera.setDisplayOrientation(90);
//攝影頭畫面顯示在Surface上
camera.startPreview();
} catch (IOException e) {

e.printStackTrace();
}
}

Camera.PictureCallback jpeg = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {

Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
//byte數组轉換成Bitmap
imageView1.setImageBitmap(bmp);
//拍下圖片顯示在下面的ImageView裡
FileOutputStream fop;
try {
fop = new FileOutputStream("/sdcard/dd.jpg");
//實例化FileOutputStream,參數是生成路徑
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fop);
//壓缩bitmap寫進outputStream 參數:輸出格式 輸出質量 目標OutputStream
//格式可以為jpg,png,jpg不能存儲透明
fop.close();
System.out.println("拍照成功");
//關閉流
} catch (FileNotFoundException e) {

e.printStackTrace();
System.out.println("FileNotFoundException");

} catch (IOException e) {

e.printStackTrace();
System.out.println("IOException");
}
camera.startPreview();
//需要手動重新startPreview,否則停在拍下的瞬間
}

};

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

public void surfaceCreated(SurfaceHolder holder) {


}

public void surfaceDestroyed(SurfaceHolder holder) {

System.out.println("surfaceDestroyed");
camera.stopPreview();
//關閉預覽
camera.release();
//
}

//自動對焦監聽式
Camera.AutoFocusCallback afcb = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {

if (success) {
//對焦成功才拍照
camera.takePicture(null, null, jpeg);
}
}
};

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==PERMISSIONS_REQUEST){
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(getApplicationContext(),"權限請求失敗",Toast.LENGTH_SHORT).show();
finish();
}else{
initCamera();
}
}

}
}
本篇文章回覆於2018-04-27 23:32
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
   
1

回覆
如要回應,請先登入.