我是Android開發的初學者,我必須繼續一個由前同事開始的項目。 在這個應用程序中,我們有客戶文件和個人信息,並且有一個按鈕允許我們拍照。 但問題在於:HTC Desire和其他Android手機可以正常工作,但不適用於Galaxy S和Galaxy S II。從android應用拍攝照片(不適用於Galaxy S/SII)
使用的算法是基本的:當我們觸摸屏幕或中心墊時,我們使用「camera」類中的autoFocus方法。然後,我們顯示剛剛拍攝的圖片,當我們按下菜單按鈕或「後退」按鈕時,會顯示一個對話框,詢問我們是否要保存圖片。
這裏是 「CameraView.java」 代碼:
public class CameraView extends Activity implements Callback, AutoFocusCallback
{
private Camera camera;
private FrameLayout layout;
private SurfaceView surface;
private String idPatient;
private boolean start;
private int click;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(null);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
layout = new FrameLayout(this);
surface = new SurfaceView(this);
surface.getHolder().addCallback(this);
surface.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
layout.addView(surface);
setContentView(layout);
idPatient = (String) this.getIntent().getStringExtra("lePatient");
start = false;
click = 0;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
click++;
if((!start)&&(click==1))
{
camera.autoFocus(this);
return true;
}
}
if (keyCode == KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
click++;
if((!start)&&(click==1))
{
camera.autoFocus(this);
return true;
}
}
return false;
}
public void onAutoFocus(boolean success, Camera camera)
{
start = true;
PictureCallback picture = new PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
Intent intentPhoto = new Intent(CameraView.this, PhotoView.class);
intentPhoto.putExtra("lePatient", idPatient);
intentPhoto.putExtra("laPhoto", data);
CameraView.this.startActivityForResult(intentPhoto, 101);
camera.startPreview();
start = false;
click = 0;
}
};
camera.takePicture(null, null, picture);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Camera.Parameters params = camera.getParameters();
params.setPictureFormat(PixelFormat.JPEG);
params.setPreviewSize(width, height);
camera.setParameters(params);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
try
{
camera = Camera.open();
camera.setPreviewDisplay(holder);
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
/**
* Fermeture de l'activity
*/
protected void onDestroy()
{
super.onDestroy();
}
}
與 「photoView.java」 類:
public class PhotoView extends Activity
{
private static final int DIALOG_ENREGISTRER = 10;
private ImageView photo;
private byte[] data;
private String idPatient;
private DBAdapter db;
/**
* Creation de l'activity
*/
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(null);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.photoview);
db = new DBAdapter(this);
db.open();
idPatient = (String) this.getIntent().getStringExtra("lePatient");
data = (byte[]) this.getIntent().getByteArrayExtra("laPhoto");
photo = (ImageView) this.findViewById(R.id.photo);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
int w = bmp.getWidth();
int h = bmp.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(90);
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
WindowManager manager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
int height= display.getHeight();
int width= display.getWidth();
Bitmap bmpFullScreen = Bitmap.createScaledBitmap(rotatedBMP, width, height, true);
photo.setImageBitmap(bmpFullScreen);
}
/**
* Permet de récuperer un évènement de click de touche
*/
public boolean onKeyUp(int keyCode, KeyEvent event) {
if((keyCode==KeyEvent.KEYCODE_BACK) ||(keyCode==KeyEvent.KEYCODE_MENU))// zoom in
{
showDialog(DIALOG_ENREGISTRER);
}
//return super.onKeyUp(keyCode, event);
return false;
}
/**
* Permet de créer des boites de dialog
*/
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DIALOG_ENREGISTRER:
return new AlertDialog.Builder(PhotoView.this)
.setIcon(android.R.drawable.ic_menu_info_details)
.setTitle("Enregistrer ?")
.setPositiveButton("OUI", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
String title = PhotoView.this.savePhotoDB();
PhotoView.this.savePhotoFS(title);
finish();
}
})
.setNegativeButton("NON", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
finish();
}
})
.create();
}
return null;
}
/**
* Permet de sauvegarder une photo dans la base de données
* @param title
*/
private String savePhotoDB()
{
Log.i("","idPatient : " +idPatient);
String comment = "Note : ";
Log.i("","comment : " + comment);
Date maintenant = new Date(System.currentTimeMillis());
SimpleDateFormat formatDateJour = new SimpleDateFormat("yyyyddMM");
String date = formatDateJour.format(maintenant);
Log.i("","Date formatée : " + date);
SimpleDateFormat formatHeure = new SimpleDateFormat("mmss");
String heure = formatHeure.format(maintenant);
Log.i("","Heure formatée : " + heure);
String fileName = "photo"+date+heure+".jpeg";
Log.i("","fileName : " + fileName);
String title = "photo"+date+heure+".jpeg";
Log.i("","title : " + title);
String userDefined = "1";
Log.i("","userDefined : " + userDefined);
db.insererPhoto(idPatient, comment, date, fileName, title, userDefined);
return title;
}
/**
* Permet de sauvegarder une photo sur le file system
* @param title
*/
private void savePhotoFS(String title) {
try
{
File fs = new File(PhotoView.this.getFilesDir()+"/"+title);
FileOutputStream fos = new FileOutputStream(fs);
fos.write(data);
fos.flush();
fos.close();
//Toast.makeText(PhotoView.this, ""+fs.getAbsolutePath(), Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Fermeture de l'activity
*/
protected void onDestroy()
{
db.close();
super.onDestroy();
}
}
誰能幫助我嗎?
感謝您的閱讀:)
後除外。發生什麼事?它崩潰了嗎?或者在拍攝照片後沒有數據被返回? – Jack
噢對不起,我忘了那部分^^。好吧,當我觸摸屏幕時,我想會拍攝一張照片(我聽到了聲音,並且有自動對焦),但是之後,我們不再向我顯示剛剛拍攝的照片,而是「回到」相機查看,所以我無法保存我的照片。我檢查了我的手機相冊,這張照片不在這裏。我猜這個問題來自於「onAutoFocus」方法,它應該調用「PhotoView」類(在其他設備上正常工作,但不是像我說的那樣在Galaxy S上)。 =>對不起,如果我的英語不好,這不是我的母語^^ – Vince
Up?任何人都可以幫助我嗎? :(:( – Vince