[英]How to get current path from another class (Camera)
我正在開發一個應用程序,該應用程序從我的自定義相機類中拍攝圖片,而不是像預覽圖像一樣將其路徑並放入Activity中的imageView中,現在我創建了一個類,用於處理Camera功能並將預覽結果的路徑發送給活動。 但是我的結果不是我拍攝的正確照片。 示例:第一次拍照時,我的“ currentPicpath”為null,但是第二次拍照時,它為我提供了之前捕獲的第一張圖像。
因此,在第2類中,我創建了一種獲取“當前”路徑的方法,但除非給出新照片,否則鋼不會給出null。
還有一個問題。為什么保存圖像后它們卻相反? 我的課程:
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_additem);
d_image_pre1 = (ImageView) findViewById(R.id.d_image1);
d_BTakePicture = (Button) findViewById(R.id.d_bTakePicture);
bOpenCamera = (Button) findViewById(R.id.bOpenCamera);
d_BTakePicture.setOnClickListener(this);
bOpenCamera.setOnClickListener(this);
take = new TakeApicture(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bOpenCamera:
take.openCam();
break;
case R.id.d_bTakePicture:
take.makeFolder("myTest");
take.captureImage();
String path = take.getCurrentPicPath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(path, options);
d_image_pre1.setImageBitmap(bm);
break;
default:
break;
}
}
第2類:
public class TakeApicture implements SurfaceHolder.Callback{
Activity context;
Camera camera;
SurfaceView surface;
SurfaceHolder holder;
PictureCallback jpegCallback;
File myGeneralFolder;
FileOutputStream outStream = null;
private String fullPathFolder;
String currentPicPath = "No image path";
@SuppressWarnings("deprecation")
public TakeApicture(Activity context) {
super();
this.context = context;
surface = (SurfaceView)context.findViewById(R.id.surfaceView);
holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
jpegCallBack();
}
public void captureImage() {
camera.takePicture(null, null, jpegCallback);
}
public void makeFolder(String itemFolderName) {
fullPathFolder = Environment.getExternalStorageDirectory()+File.separator+"mySalesImages"+File.separator+itemFolderName;
myGeneralFolder = new File(fullPathFolder);
myGeneralFolder.mkdirs();
}
public void jpegCallBack(){
jpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
getPicPath(data);
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
public void getPicPath(byte[] data) throws IOException{
currentPicPath = String.format(myGeneralFolder+"/%d.jpg",(System.currentTimeMillis()));
outStream = new FileOutputStream(currentPicPath);
outStream.write(data);
outStream.close();
}
public String getCurrentPicPath() {
return currentPicPath;
}
@SuppressWarnings("deprecation")
public void openCam(){
try {
camera = Camera.open();
Camera.Parameters param;
param = camera.getParameters();
//modify parameter
camera.setDisplayOrientation(90);
param.setPreviewFrameRate(20);
param.setPreviewSize(176, 144);
camera.setParameters(param);
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (Exception e) {
// TODO: handle exception
}
}
public void closeCam(){
camera.stopPreview();
camera.release();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
closeCam();
}
}
這是正確的解決方案???
take.captureImage();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
String path = take.getCurrentPicPath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(path, options);
d_image_pre1.setImageBitmap(bm);
}
}, 1000);
該行take.captureImage();
開始捕獲照片的異步過程。 一段時間后,Android系統將調用您的onPictureTaken()
回調,然后您將計算新的圖像路徑(並相應地寫入照片)。 但是線
String path = take.getCurrentPicPath();
將已經執行。
您可以同步計算路徑,但是即使如此,您的“活動”也必須等待將實際映像寫入磁盤。 因此,您別無選擇,只能提取片段
String path = take.getCurrentPicPath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(path, options);
d_image_pre1.setImageBitmap(bm);
變成一個單獨的方法。 您可以直接從onPictureTaken()
調用此新方法,也可以使用post()
(無需從postDelayed()
進行onPictureTaken()
)異步執行它。
因此,快速和骯臟的修復(為簡便起見,刪除了異常處理)如下:
在MainActivity.java中 :
public void onClick(View v) {
switch (v.getId()) {
case R.id.bOpenCamera:
take.openCam();
break;
case R.id.d_bTakePicture:
take.makeFolder("myTest");
take.captureImage();
break;
}
}
public void setImage(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(path, options);
d_image_pre1.setImageBitmap(bm);
}
在TakeApicture.java中 :
public void jpegCallBack() {
jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
getPicPath(data);
(MainActivity)context.setImage(currentPicPath);
}
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.