gpt4 book ai didi

java - 我如何通过触摸表面 View 来捕捉图像

转载 作者:太空狗 更新时间:2023-10-29 13:26:56 27 4
gpt4 key购买 nike

public class AndroidCamera extends Activity implements OnTouchListener, SurfaceHolder.Callback{

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;;
PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;



EditText txtData, Info,Age;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview);
Button buttonCapturePreview = (Button) findViewById(R.id.capturepreview);



txtData = (EditText) findViewById(R.id.editText1);
Info = (EditText) findViewById(R.id.Name);
Info.setHint("enter name");
Age = (EditText) findViewById(R.id.Age);
Age.setHint("Age");



getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);




rawCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
Log.d("Log", "onPictureTaken - raw");
}
};

shutterCallback = new ShutterCallback()
{
public void onShutter() {
Log.i("Log", "onShutter'd");
}
};

// onTouchEvent(null);

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

Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Asw");
imagesFolder.mkdirs();
Date d = new Date();
CharSequence s = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
File output = new File(imagesFolder, s.toString() + ".jpg");

Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;

try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();

Toast.makeText(AndroidCamera.this,
"Image saved: ",
Toast.LENGTH_LONG).show();

}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{}
Log.d("Log", "onPictureTaken - jpeg");
}
};

buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener()
{

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onClick(View v)
{
if(!previewing)
{
camera = Camera.open(0);
if (camera != null)
{

try {

camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
}
catch (IOException e)
{

e.printStackTrace();
}
}else txtData.setText("null");
}
}});



buttonCapturePreview.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{

captureImage();
}

private void captureImage()
{
camera.takePicture(shutterCallback, rawCallback, jpegCallback);

}
});
buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v)
{

if(camera != null && previewing)
{
camera.stopPreview();
camera.release();
camera = null;

previewing = false;
}
}});




@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.android_camera, menu);
return true;
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
// TODO Auto-generated method stub

}


@Override
public void surfaceCreated(SurfaceHolder holder)
{
// TODO Auto-generated method stub

}


@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// TODO Auto-generated method stub

}




public boolean onTouch(View v, MotionEvent event)
{

if (event.getAction() == MotionEvent.ACTION_MOVE)
{
camera.takePicture(shutterCallback, rawCallback, jpegCallback);

}
return super.onTouchEvent(event);

//return false;
}

我使用了一个按钮来捕捉图像。现在我想通过触摸表面 View 来捕捉和保存图像。我写了一些代码。它没有显示任何错误,但它也不起作用。你能告诉我哪里出错了吗?请帮忙

最佳答案

试试这个以编程方式拍摄屏幕截图...

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;

// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

关于java - 我如何通过触摸表面 View 来捕捉图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20488423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com